00:00
00:00
Newgrounds Background Image Theme

ItsJaron just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As3: Billiard Game Physics

4,010 Views | 6 Replies
New Topic Respond to this Topic

As3: Billiard Game Physics 2012-03-22 16:59:05


AS3: Billiard Game Physics
---------------
AS3: Main
---------------

Note: Before start reading the actual tutorial, I just want to leave clear that this will NOT teach you how to create a billiard game, only it physics and mechanics.

Check out the example I made below:
DEMO
Move the white ball with the arrow keys.

There are two necessary bases for create something like my demo example: Mass and Impulse.

Mass will measure how fast a ball will be able to speed up after collide. Impulse will create the collision based upon the balls's masses.

The impulse equation we are going to be using is:

J = - (1 + e) * ((v * n) + (v' * n'))
          ---------------------------------
                     1 + 1
                     --  --
                     M   m

Where:
e = coefficient of restitution
v = ball A velocity
v' = ball B velocity
n = ball A normal vector
n' = ball B normal vector
M = ball A mass
m = ball B mass

If you are interested to know how to find out the equation above, just go here.

Before writing the function for colliding and separating the circles, here are the objects I added to each circle:

px : Number --> x coordinate
py : Number --> y coordinate
r : Number --> radii
vx : Number --> x velocity
vy : Number --> y velocity
m : Number --> mass

Then each frame I desaccelerate the ball's velocities with friction. That's all.

Ok. Finally for the function!

Since I already have the variables in the symbols objects, I only added two parameters, a and b (circle A and circle B respectively). Also, I made it a boolean function, this means that if true, the circles collide, if false, ignore.

function collideCircCirc(a:*,b:*):Boolean
{
    // distances x,y
    var dx:Number = a.px-b.px; 
    var dy:Number = a.py-b.py; 

    // distance squared, radii sum
    var dl2:Number = dx*dx+dy*dy; 
    var dr:Number = a.r+b.r; 

    // if distance > pow radii, ignore
    if(dl2>dr*dr) return false; 

    // mass a,b
    var am:Number = a.m; 
    var bm:Number = b.m; 

    // inverse sum of mass
    var m:Number = 1/(am + bm); 

    // distance non-squared, inverse of distance
    var dl:Number = Math.sqrt(dl2); 
    var idl:Number = 1/dl 

    // normal vectors
    var nx:Number = dx*idl; 
    var ny:Number = dy*idl; 

    // separation vector
    var sd:Number = (dr-dl)*m; 

    // separation x,y
    var sx:Number = nx*sd; 
    var sy:Number = ny*sd; 

    // separating circles
    a.px += sx*am;
    a.py += sy*am;
    b.px -= sx*bm;
    b.py -= sy*bm;

    // velocity x,y
    var vx:Number = a.vx-b.vx;
    var vy:Number = a.vy-b.vy;

    // dot product
    var dot:Number = vx*nx+vy*ny;

    // if dot is positive, collide
    if(dot>0) return true;


    // impulse and x,y	
    var j:Number = -(1+e)*dot*m;
    var jx:Number = nx*j;
    var jy:Number = ny*j;

    // velocity change based upon the impulse and mass
    a.vx += jx*am;
    a.vy += jy*am;
    b.vx -= jx*bm;
    b.vy -= jy*bm;

    return true;
}

Then you can update each frame with:

collideCircCirc(ball1,ball2);

or you can use an array and check for each circle removing the circle you are currently using.

If someone doesn't even know what I talked about (like Impulse, Mass, Vectors, Velocities, Normal Vector, etc...) I ask you or googling it or asking for your physics teacher.

I hope it helps someone.
Any doubts, just ask!

Response to As3: Billiard Game Physics 2012-03-22 19:12:36


Thanks Spysociety, really interesting tutorial.

Response to As3: Billiard Game Physics 2013-05-22 05:31:21


Hi, please let us have the source file from your example.. that's what I exactly need for my game and I just cant find any other tutorials or source exactly like your example. I tried to apply what you have written but its too hard.
any help is very much appreciated . thanks

Response to As3: Billiard Game Physics 2013-05-22 06:59:53


At 5/22/13 05:31 AM, haste777 wrote: Hi, please let us have the source file from your example.. that's what I exactly need for my game and I just cant find any other tutorials or source exactly like your example. I tried to apply what you have written but its too hard.
any help is very much appreciated . thanks

Why do you need source? you have nice example here just make your own code with his help, isn't that enough already?


RangeError: Error #1125: The index 4 is out of range 4.

Response to As3: Billiard Game Physics 2013-05-23 08:51:01


At 5/22/13 06:59 AM, nitokov wrote:
At 5/22/13 05:31 AM, haste777 wrote: Hi, please let us have the source file from your example.. that's what I exactly need for my game and I just cant find any other tutorials or source exactly like your example. I tried to apply what you have written but its too hard.
any help is very much appreciated . thanks
Why do you need source? you have nice example here just make your own code with his help, isn't that enough already?

Did you even look at the code or the explanation of the mechanics involved? If you did, you most likely wouldn't say that this is "enough already". Anyone who can build billiard balls mechanics from this post would have been able to build it on their own in the first place. The code is barely legible and very inflexible, and there is pretty much no explanation of the physical process whatsoever. Anyway I'm not gonna keep beating on this, because the thread is actually over a year old, but the point is that this guide is very lacking in quality of code and explanation of code.


BBS Signature

Response to As3: Billiard Game Physics 2013-05-23 20:58:54


At 5/23/13 08:51 AM, Toast wrote:
At 5/22/13 06:59 AM, nitokov wrote:
At 5/22/13 05:31 AM, haste777 wrote: Hi, please let us have the source file from your example.. that's what I exactly need for my game and I just cant find any other tutorials or source exactly like your example. I tried to apply what you have written but its too hard.
any help is very much appreciated . thanks
Why do you need source? you have nice example here just make your own code with his help, isn't that enough already?
Did you even look at the code or the explanation of the mechanics involved? If you did, you most likely wouldn't say that this is "enough already". Anyone who can build billiard balls mechanics from this post would have been able to build it on their own in the first place. The code is barely legible and very inflexible, and there is pretty much no explanation of the physical process whatsoever. Anyway I'm not gonna keep beating on this, because the thread is actually over a year old, but the point is that this guide is very lacking in quality of code and explanation of code.

in some point the tutorial is good, but ur correct that its lacking in quality of explanation.. maybe because it doesnt have a visual explanation. And the code is just there, I dont know where to put it or how to apply it in my own.

Response to As3: Billiard Game Physics 2013-05-23 21:02:01


At 5/23/13 08:58 PM, haste777 wrote: in some point the tutorial is good, but ur correct that its lacking in quality of explanation.. maybe because it doesnt have a visual explanation. And the code is just there, I dont know where to put it or how to apply it in my own.

http://tonypa.pri.ee/vectors/start.html