NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
December 10, 2019, 09:44:05 pm

To live close to great minds is the best kind of education. ..."John Buchan (1875~1940 Scotticsh historian, Governor General of Canada)"

 Pages: [1]   Go Down
 Author Topic: Ball line collision and response. multiple collisions at once  (Read 14685 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
slayer 64
Newbie

Offline

Posts: 1

 « Embed this message on: September 02, 2009, 05:42:48 am » posted from:Waterbury,Vermont,United States

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));

{
//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;
}
}
}
}
 Logged
lookang
Hero Member

Offline

Posts: 1792

http://weelookang.blogspot.com

 « Embed this message Reply #1 on: September 02, 2009, 09:44:55 am » posted from:Singapore,,Singapore

http://www.um.es/fem/EjsWiki/Main/ExamplesMultipleCollisions by Francisco Esquembre is possibly a suitable example for u to study and discuss on.

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• 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!
 multiplecollision.PNG (8.51 KB, 414x433 - viewed 524 times.) « Last Edit: September 02, 2009, 09:47:57 am by lookang » Logged
book reviews
Newbie

Offline

Posts: 2

 « Embed this message Reply #2 on: September 13, 2009, 08:03:46 am » posted from:Jakarta,Jakarta Raya,Indonesia

Hem.... i think i can use that technique to complete my game project. Thanks for that usefull information
 Logged
Clotaire
Newbie

Offline

Posts: 1

 « Embed this message Reply #3 on: November 10, 2010, 05:58:01 pm » posted from:ChÃ¢tenay-malabry,Ile-de-France,France

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?
 Logged
lookang
Hero Member

Offline

Posts: 1792

http://weelookang.blogspot.com

 « Embed this message Reply #4 on: November 10, 2010, 08: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.
Code:
//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;
 Logged
 Pages: [1]   Go Up
To live close to great minds is the best kind of education. ..."John Buchan (1875~1940 Scotticsh historian, Governor General of Canada)"