# NTNUJAVA Virtual Physics Laboratory

## Information about this web site => Request for physics Simulations => Topic started by: slayer 64 on September 02, 2009, 06:42:48 am

Title: Ball line collision and response. multiple collisions at once
Post by: slayer 64 on September 02, 2009, 06:42:48 am
i use a program called game maker. (http://www.yoyogames.com/make) i read 2D collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg15#msg15) and created that simulation in game maker. you can find it here. (http://gmc.yoyogames.com/index.php?showtopic=446075) 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));

{
//normalize
dx/=dis;
dy/=dis;

//calculate the component of velocity in the direction
vp1=hspeed*dx+vspeed*dy;

if vp1!=0
{

//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;
}
}
}
}
Title: Re: Ball line collision and response. multiple collisions at once
Post by: lookang 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.
Title: Re: Ball line collision and response. multiple collisions at once
Post by: book reviews 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
Title: Re: Ball line collision and response. multiple collisions at once
Post by: Clotaire 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?
Title: Re: Ball line collision and response. multiple collisions at once
Post by: lookang 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;