Friday, November 12, 2010

Bouncy - Silverlight Version

Heres a Silverlight version of the game BOUNCY. I made just to get the feel of Silverlight, Blend and C#.

The logic and coding is almost the same with a bit of differences from Actionscript 3.0.

You can check out the BOUNCY - AS 3.0 Tutorial from my earlier post for comparison.






Silverlight Code :
Class : MainPage

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Bouncy
{
public partial class MainPage : UserControl
{
double G;
double E;
double epsilon;
Ball ball;
double rightWallX;
double leftWallX;
public int score;

public MainPage()
{
// Required to initialize variables
InitializeComponent();
init();
CompositionTarget.Rendering += onEnterFrame;
}

void init(){
this.G = 0.27;
this.E = 0.65;
this.epsilon = 0.5;
ball = new Ball(this);
Canvas.SetLeft(ball, 275);
Canvas.SetTop(ball, 200);
LayoutRoot.Children.Add(ball);
score = 0;
this.scoreTxt.Text = score + "";
}

void onEnterFrame(object sender, EventArgs e){
//if(Math.Abs(ball.speedY)<0.01)
// collision - ground
if(Canvas.GetTop(ball)+ball.speedY > 300){
if(score != 0){
score = 0;
this.scoreTxt.Text = score + "";
}
if(Math.Abs(ball.speedY)>this.epsilon){
ball.speedY = -ball.speedY*E;
// add friction when on ground
ball.speedX *= 0.9;
}
else{
ball.speedY=0;
//Canvas.SetTop(ball, 300);
}
}
else if(Canvas.GetTop(ball) < 299){
ball.speedY += G;
}

// collision - right wall n left wall
if(Canvas.GetLeft(ball) > 510){
ball.speedX = -ball.speedX;
}
else if(Canvas.GetLeft(ball) < 40){
ball.speedX = -ball.speedX;
}

/*if(ball.speedY != 0){
ball.speedY += G;
}*/
if(ball.speedY == 0){
// add friction when on ground
ball.speedX *= 0.9;
}
Canvas.SetLeft(ball, Canvas.GetLeft(ball)+ball.speedX);
Canvas.SetTop(ball,Canvas.GetTop(ball)+ball.speedY);
}

public void increaseScore(){
score++;
scoreTxt.Text = score + "";
}
}
}


Class : Ball

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Bouncy
{
public partial class Ball : UserControl
{

public double speedX;
public double speedY;

MainPage par;

public Ball(MainPage p)
{
// Required to initialize variables
InitializeComponent();
speedX = speedY = 0;
par = p;
}

public void move(){
// move
Canvas.SetLeft(this,Canvas.GetLeft(this)+this.speedX);
Canvas.SetTop(this,Canvas.GetTop(this)+this.speedY);
}

private void Ellipse_MouseLeftButtonDown(object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
// TODO: Add event handler implementation here.
// increase score
par.increaseScore();
speedY = -6;
Random r = new Random();
speedX = r.NextDouble() * 14 - 7;
}
}
}

Thursday, November 11, 2010

Rickshaw Brawl done for Intel Level Up 2010

Hello all! Blogging after such a long time. The past month was really tight packed with lots of tasks to do, one of which was the INTEL LEVEL UP 2010. Well, for those of you who don't know what that is...Its a game development competition held by Intel every year. And fortunately one of the Idea that I submitted, got selected for the Demo Round. Credits to Sagar bhaiya for the awesome idea. That game is - RICKSHAW BRAWL.



I, along with my animator friend Ravi and his team, have been busy developing the game for the past 1 month. This has been my first experience with 3D Game development (actually finished a game :P) after thinking of jumping to 3D Game dev from Flash for a long time. And I must say, it was pretty good for a first one. :)

Moreover, as a boost-up, our Game Rickshaw Brawl has been selected among the TOP 10 games in the LEVEL UP - India Series and invited to be showcased at the NASSCOM Animation & Gaming Summit 2010 in Hyderabad. Cheers to the whole team for that !!
Well, its still a question if I'll be able to attend it or not :P Lets see.

The Game would be available for download in the coming days, probably on my new site. So keep watching the Blog for further announcements.

The final international results for the LEVEL UP will be out in December. So pray for us :)

And Happy Diwali !! (a little late here)

Sunday, August 1, 2010

Box2D Experiment

I have been exploring BOX2D physic engine for past few days. Its really great to work with once you understand its basic working. I made up this small demo showing some of the basic features of BOX2D.

Enjoy the physics :D

Thursday, July 29, 2010

My first experience with PAPERVISION 3D!

Hello all !

I am back with something new..and this time its PAPERVISION 3D - The awesome 3D library for flash. This is what I created to explore it.

Saturday, July 3, 2010

My first tutorial - How to make a simple ball game in AS 3.0

Here it is guyzz !! The much not !(awaited) tutorial by me on my blog. This tutorial is basically for all those people who are passionate about game development, specially in Flash, but did not knew how to start off. Doing this game tutorial will expose you to the basic flow of a game and how the elements of the game interact with each other.
Well without much talk lets start off the business.


What are we going to develop ?

In this tutorial we'll go through the procedure to develop a basic game similar to my game BOUNCY.


What are the pre-requisites ?

  1. Adobe Flash CS3 or higher

  2. Basic knowledge of Actionscript 3.0

  3. Basic understanding of OOPS concepts


But how how how ?

So here starts the real game development. This tutorial is divided into 2 parts:
PART 1 and PART 2.

PART 1


The first part will discuss the basic ball movement and collisions.

Fire up your Flash IDE and open a new AS 3.0 file

Create 2 Movieclips for Ball and Ground each. You can name them anything you like.

Draw simple shapes for the 2 movieclips (circle for ball and rectangle bar for ground). Finally drag them
to your stage from the library and name them ball_mc and ground_mc.

Now create 2 Actionscript files : Game.as and Ball.as and SAVE YOUR WORK.

Game.as Code :
Open the Game.as file and put the following code :

/*
Game.as
*/

package{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.Point;

public class Game extends MovieClip{
var GRAVITY:Number = 0.5;
// elasticity coefficient
var E:Number = 0.7;
var EPSILON:Number = 0.7;

// ctor
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
}

function gameLoop(e:Event):void{
var radius = ball_mc.width/2;
var forecast_x = ball_mc.x+ball_mc.speed_x;
var forecast_y = ball_mc.y+radius+ball_mc.speed_y;

// relection of ball from left and right sides
if((forecast_x-radius < ground_mc.x-ground_mc.width/2) || (forecast_x+radius > ground_mc.x+ground_mc.width/2)){
ball_mc.speed_x = -(ball_mc.speed_x*E);
}

// calculate the contact point
var pnt:Point = new Point(ball_mc.x, forecast_y);
// convert it to stage cordinate system
pnt = this.localToGlobal(pnt);
// check collision of ground and ball contact point
if(ground_mc.hitTestPoint(pnt.x, pnt.y)){
ball_mc.speed_y = -(ball_mc.speed_y*E);
// if y-speed is very small...stop it
if(Math.abs(ball_mc.speed_y) < EPSILON){
ball_mc.speed_y = 0;
// add friction as ball rolls on ground
ball_mc.speed_x *= 0.85;
}
}
else{
ball_mc.speed_y += GRAVITY;
}
// move the ball after making all calculations
ball_mc.move();
}
}//eof class
}//eof package

Code Explanation :


  1. This class is the Document class of our game. Just incase you dont know how to attach a Document class :
    [ To attach a Document class : Click anywhere on empty stage and open property panel. Put the class name in the Document Class: text box. ]

    We declare 3 variables :
    GRAVITY : As the name suggests, it is the amount to add to ball's y-speed while in air.
    E : This variable defines the elasticity co-efficient for the collision between ball and ground.
    EPSILON : Its the smallest amount of y-speed that ball can attain before coming to rest.


  2. Next we define the class constructor. Here we add an event listener for every frame (MouseEvent.ENTER_FRAME).

    The listener function is called gameLoop().


  3. Now comes the core function of our Game class..gameLoop().
    The first 3 lines calculates few things. The radius of ball, the ball's next x coordinate (current x + x-speed) and similarly its next y coordinate (current x + x-speed + radius).

    Notice that we add radius while calculating forecast_y so that we get
    the bottom contact point instead of center of ball.



  4. // relection of ball from left and right sides
    if((forecast_x-radius < ground_mc.x-ground_mc.width/2) || (forecast_x+radius > ground_mc.x+ground_mc.width/2)){
    ball_mc.speed_x = -(ball_mc.speed_x*E);
    }

    We then check the collision of ball(Ball_mc) with the left and right boundaries. In this case we take the boundaries as the end of our ground. The collision is a simple check that if the x coordinate of the leftmost point of the ball goes , beyond the left and right extremes of the ground, we reverse the x-speed of the ball. Simple...isn't it ?




  5. // calculate the contact point
    var pnt:Point = new Point(ball_mc.x, forecast_y);
    // convert it to stage cordinate system
    pnt = this.localToGlobal(pnt);

    To calculate the collision between ball and ground we check if the lowermost point of the ball is hitting the ground or not.
    The function that serves our purpose is hitTestPoint(p:Point) which takes in a Point class object.

    But the thing to notice here is that this function needs coordinates in the global space i.e. in the Stage coordinate system. So we transform the point (ball_mc.x,forecast_y) to stage coordinate system through the function localToGlobal().
    [ localToGlobal() : This function takes in a Point Class object and return the cordinates in the coordinate system of the calling object. ]

    We get the point to check in variable pnt.




  6. // check collision of ground and ball contact point
    if(ground_mc.hitTestPoint(pnt.x, pnt.y)){
    ball_mc.speed_y = -(ball_mc.speed_y*E);
    // if y-speed is very small...stop it
    if(Math.abs(ball_mc.speed_y) < EPSILON){
    ball_mc.speed_y = 0;
    // add friction as ball rolls on ground
    ball_mc.speed_x *= 0.85;
    }
    }
    else{
    ball_mc.speed_y += GRAVITY;
    }

    This is actual code where we check the collision between the ground and ball's lowest point that we now have in pnt. The hitTestPoint() called from ground_mc takes in the coordinates of the point to check for collision.

    If the hitTestPoint() returns true (Collision occured), we simply reverse the ball's y-speed after multiplying it with E (cooefficient of restitution).

    Note here that as E is less than 1, multiplying it to the y-speed decreases it. After several such bounces the y-speed becomes lesser and lesser and the ball kind of vibrates on the ground due to very small y-speed.
    The situation is shown below. To see more clearly, RClick on the swf, Zoomin on the contact point.



    To correct this problem, we put a restriction on the bouncing through an if condition. We check if the magnitude of y-speed of the ball has reached a predefined amount defined by EPSILON (a small number) variable.
    When the y-speed becomes less than EPSILON, we stop the ball.

    Also we add friction to the ball when the ball stops by just decreasing the x-speed of ball (by multiplying with cooefficient of friction).



  7. Finally, we call the ball's move() in order to move it. The move() function of ball will be defined later in this part.


Ball.as Code :
Open the Ball.as file and put the following code :

/*
Ball.as
*/

package{
import flash.display.Sprite;
import flash.events.*;

public class Ball extends Sprite{

var speed_x:Number;
var speed_y:Number;

// ctor
public function Ball(){
speed_x = 0;
speed_y = 0;
}

function move():void{
this.x += speed_x;
this.y += speed_y;
}

}//eof class
}//eof package


Code Explanation :



  1. This is a very simple class for the ball in our game.

    Firstly we declare 2 variables : speed_x and speed_y. Their use is simply what they mean. Not much here.

    Next is the constructor which initialises the two speeds to 0.



  2. Lastly, we define a function called move() which was called from within the Game.as as we saw before. This function just updates the position of the ball by adding the speeds to the current position.

    Dats all !


This is all we need to do to embed collision and basic movement of ball in our game.

Now save your work and run your flash movie (CTRL + Enter).
You should see something like this :



What to expect in PART 2 ?
Well, PART 2 is not much of a tutorial. Its a small part of the game that is left as an exercise for you guyzz. The only thing remaining in our game is to code the ball's mouse interaction. Our ball (ball_mc) should bounce when we click on it.

The readers are recommended to write this code themselves. And incase you are not able to do it, don't panic. I'll be posting the solution snippet in the coming days. Till then try to play with the codes given here and make it more of a complete game by putting elements like :

  1. Scoring system

  2. Game Menus

  3. Better graphics




Download the source code here

Your comments are most welcome. They'll help me to write better tutorials in future.

Happy Flashing !!