Author Topic: Ball line collision and response. multiple collisions at once  (Read 15487 times)

slayer 64

  • Newbie
  • *
  • Posts: 1
Ball line collision and response. multiple collisions at once
« on: September 02, 2009, 06:42:48 am »
i use a program called game maker. i read 2D collision and created that simulation in game maker. you can find it here. i've used what i found and made a ball collide with a line. but when the ball collides with more than 1 line, it cuts through one of the lines every time. when the ball collides with more than 1 ball it jumps back a little too far, leaving a gap when the 2 balls should be touching perfectly.

can you make an example like 2D collision but will handle multiple balls and lines?
here is my code, its game maker code but i think you can understand it.
thank you.

var denominator,position,x1,y1,x2,y2,i;

//collide with lines
for(i=0;i<ds_list_size(obj_lines.lines)-2;i+=2)
{
  x1=ds_list_find_value(obj_lines.lines,i);
  y1=ds_list_find_value(obj_lines.lines,i+1);
  x2=ds_list_find_value(obj_lines.lines,i+2);
  y2=ds_list_find_value(obj_lines.lines,i+3);

  dx=x2-x1;
  dy=y2-y1;

  denominator=sqr(dx)+sqr(dy);

  if denominator>0
  {
  position=median(0,1,((x-x1)*dx+(y-y1)*dy)/denominator);
 
  //find tangent point on line
  px=x1+position*dx;
  py=y1+position*dy;
 
  //get distance from ball to tanget point
  dx=px-x;
  dy=py-y;
  dis=sqrt(sqr(dx)+sqr(dy));
 
  if dis<=radius+obj_lines.radius
  {
  //normalize
  dx/=dis;
  dy/=dis;
 
  //calculate the component of velocity in the direction
  vp1=hspeed*dx+vspeed*dy;
 
  if vp1!=0
  {
  dt=(radius+obj_lines.radius-dis)/vp1;
 
  //move the ball back so they just touch
  x-=hspeed*dt;
  y-=vspeed*dt;
 
  //projection of the velocities in these axes
  va1=(hspeed*dx+vspeed*dy);
  vb1=(-hspeed*dy+vspeed*dx);
 
  //bounce. 1 would keep be a perfect slide against the line
  b=1;
 
  //new velocities in these axes
  vaP1=va1+b*(0-va1);
 
  hspeed=vaP1*dx-vb1*dy; 
  vspeed=vaP1*dy+vb1*dx;
 
  //we move the balls back in time so we need to move them forward
  x+=hspeed*dt;
  y+=vspeed*dt;
  }
  }
  }
}

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
Re: Ball line collision and response. multiple collisions at once
« Reply #1 on: September 02, 2009, 10:44:55 am »
http://www.um.es/fem/EjsWiki/Main/ExamplesMultipleCollisions by Francisco Esquembre is possibly a suitable example for u to study and discuss on.

Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
Press the Alt key and the left mouse button to drag the applet off the browser and onto the desktop. This work is licensed under a Creative Commons Attribution 2.5 Taiwan License
  • Please feel free to post your ideas about how to use the simulation for better teaching and learning.
  • Post questions to be asked to help students to think, to explore.
  • Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!
« Last Edit: September 02, 2009, 10:47:57 am by lookang »

book reviews

  • Newbie
  • *
  • Posts: 2
    • Book Reviews
Re: Ball line collision and response. multiple collisions at once
« Reply #2 on: September 13, 2009, 09:03:46 am »
Hem.... i think i can use that technique to complete my game project. Thanks for that usefull information

Clotaire

  • Newbie
  • *
  • Posts: 1
Re: Ball line collision and response. multiple collisions at once
« Reply #3 on: November 10, 2010, 06:58:01 pm »
http://www.um.es/fem/EjsWiki/Main/ExamplesMultipleCollisions by Francisco Esquembre is possibly a suitable example for u to study and discuss on.

I check your code when there are some overlap, you ask the program to give the ball a new random velocity. it is right?

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
Re: Ball line collision and response. multiple collisions at once
« Reply #4 on: November 10, 2010, 09:55:50 pm »
http://www.um.es/fem/EjsWiki/Main/ExamplesMultipleCollisions by Francisco Esquembre is possibly a suitable example for u to study and discuss on.

I check your code when there are some overlap, you ask the program to give the ball a new random velocity. it is right?
no. it is not random at all.
//System.out.println ("Collision between "+collision1 +" and "+collision2);
//System.out.println ("at x[1]= "+x[collision1] +" and x[2] = "+x[collision2]);
double deltax = x[collision2]-x[collision1];
double deltay = y[collision2]-y[collision1];
double distance = Math.sqrt(deltax*deltax+deltay*deltay);

double rx=deltax/distance, ry=deltay/distance; // Unit vector joining centers
double sx=-ry, sy=rx; // Vector ortogonal to the previous one

double vr1=(vx[collision1]*rx+vy[collision1]*ry), vs1=(vx[collision1]*sx+vy[collision1]*sy); // Projections for disk 1
double vr2=(vx[collision2]*rx+vy[collision2]*ry), vs2=(vx[collision2]*sx+vy[collision2]*sy); // Projections for disk 2

double vr1d=( 2*mass[collision2]*vr2 + (mass[collision1]-mass[collision2])*vr1 )/(mass[collision1]+mass[collision2]); // New velocity for disk 1
double vr2d=( 2*mass[collision1]*vr1 + (mass[collision2]-mass[collision1])*vr2 )/(mass[collision1]+mass[collision2]); // New velocity for disk 2
// Undo the projections
vx[collision1]=vr1d*rx+vs1*sx; vy[collision1]=vr1d*ry+vs1*sy;
vx[collision2]=vr2d*rx+vs2*sx; vy[collision2]=vr2d*ry+vs2*sy;