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