NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
November 26, 2014, 04:44:29 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Ask questions. Follow-up questions. ...Wisdom
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: Ball line collision and response. multiple collisions at once  (Read 10106 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
slayer 64
Newbie
*
Offline 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));
 
  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;
  }
  }
  }
}
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1729


http://weelookang.blogspot.com


WWW
«
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
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!


* multiplecollision.PNG (8.51 KB, 414x433 - viewed 135 times.)
« Last Edit: September 02, 2009, 09:47:57 am by lookang » Logged
book reviews
Newbie
*
Offline 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 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 Offline

Posts: 1729


http://weelookang.blogspot.com


WWW
«
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
  Print  
Ask questions. Follow-up questions. ...Wisdom
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
collision between twp particles in one dimension(along a line)
Examples for beginner who want to learn EJS
Fu-Kwun Hwang 0 9379 Last post October 10, 2007, 03:13:55 pm
by Fu-Kwun Hwang
Astroblaster balls (The Multiple Collision Accelerator!)
Kinematics
Fu-Kwun Hwang 1 11728 Last post June 02, 2011, 08:19:19 pm
by Fu-Kwun Hwang
multiple collisions with gravity
Question related to Physics or physics related simulation
frecka 1 7293 Last post January 24, 2009, 06:06:36 pm
by Fu-Kwun Hwang
Astroblaster balls (The Multiple Collision Accelerator!)
kinematics
ahmedelshfie 0 3204 Last post May 27, 2010, 11:54:33 pm
by ahmedelshfie
2D collision with multiple balls at the same time
Request for physics Simulations
PL_kolek 12 13854 Last post April 19, 2013, 03:27:17 pm
by tomoisgood0444
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.051 seconds with 22 queries.since 2011/06/15