Link to home
Start Free TrialLog in
Avatar of vroksjab
vroksjab

asked on

Colliding balls and reflection angles

Hi!

I'm writing a game(c#) based on the old one called "Jezzball". My problem is the ball physics.

I have a class for the balls

class Ball
{
      Point pos;
      Vector speed;
      int diameter;
      . . .
      static public void Collide(Ball a, Ball b)
      {
            //How do I calculate the new angles and velocities
      }
}

struct Vector
{
      float x,y;
      . . .
      public static double DotProduct( Vector v1, Vector v2)
      {
            return v1.x*v2.x + v1.y*v2.y;
      }
}

I know when the balls collide and then the static Collide-method is called passing the two colliding balls as parameters.
So how do i calculate the ball-reflection angles. I've read something about the Dot(or was i Cross) product but I don't know more about it than the methoid above . . .

I havn't read anything about vectors in school yet so please explain the calculations.
Avatar of ozo
ozo
Flag of United States of America image

In the center of gravity frame, see if the balls approach close enough to collide.
If so, reflect their velocities across the point of contact
Avatar of nielsboldt
nielsboldt

 static public void Collide(Ball a, Ball b)
     {
          //How do I calculate the new angles and velocities
         ///we compute the  contact normal first
         vec2 normal = (a.position - b.position).normalized()
         ///then the contact velocity is given by
         vec2 cVel = dot( a.speed - b.speed , normal );
         ///we can use a restituition coefficient e to model a collision
        /// e is between 0 and 1 where 1 means an elastic collision and 0 an nonelastic collision
       ///and we alter the velocity as
      a.speed += -0.5 *(1+e) *cVel * normal;
      b.speed -= -0.5 *(1+e) * cVel * normal;

     ///all calculations are done in the world frame  
     }
This assumes that abs(a.position - b.position) <= a.diameter+b.diameter
yeah, i know: He wrote that he knew when the balls where colliding so no need to treat this.

IHowever it should be abs(a.position - b.position) <= 0.5*(a.diameter+b.diameter)
did we forget mass somewhere?
Avatar of vroksjab

ASKER

As I said I don't know much about vectors . . . What is the code for the Vector.Normalized() method . . . It would also be nice if someone wrote anything about the masses as well . . . but it is not critical
    public static Vector normalized( Vector v)
     {
         float s = sqrt(dot(v,v));
         v.x /= s;  v.y /= s;
         return v;
     }

if we have a.mass and b.mass, then
 
    float cVel = dot( a.speed - b.speed , normal );
    Vector aspeed=a.speed;
     a.speed += b.mass/(a.mass+b.mass) * ((1+e) * -cVel * normal + (1-e)*b.speed);
      b.speed += a.mass/(a.mass+b.mass) * ((1+e) * cVel * normal + (1-e)*aspeed);

(though the usage of "speed" and "Vel" are swaped from their meanings)
ASKER CERTIFIED SOLUTION
Avatar of nielsboldt
nielsboldt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tired this solution and if a ball in motion strikes a ball at rest it imparts 2x the velocity to the ball at rest.