We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › billard balls
Page Index Toggle Pages: 1
billard balls (Read 769 times)
billard balls
May 9th, 2007, 4:03pm
 
hi, i want to simulate balls moving in an area and making to move each other when hitting.
i wrote a ball class for this with draw,update, hitBy! method. but this one doesn't give very good results.
i have changed the code, and this is just the fundamental stuff for the moving and hitting part.


class Ball {

 float x,y;
 float ax,ay;
 int sz=10;

 Ball() {
   x= (int) random(width);
   y= (int) random(height);

 }

 void show() {
   noStroke();
   ellipse(x,y,sz,sz);
 }

 void update() {
     x= (x+ax+width)%width;
     y= (y+ay+height)%height;
     ax*= 0.97;
     ay*= 0.97;
     if(abs(ax)+abs(ay) < 0.1){
     ax= 0;
     ay= 0;
     }

  void hitBy(Ball b) {

  float d= dist(b.x,b.y,x,y);
  float dd=sz+b.sz;
  if(d< dd){
    float w= atan2(y-b.y,x-b.x);
    float vx= cos(w);
    float vy= sin(w);
   //richtigstellung
// so that is to have just not to have the balls //intersecting
   float fac= dd/ d;
   b.x= b.x+ vx*fac;
   b.y= b.y+ vy*fac;
// the hit
     ax+= vx*b.ax;
     ay+= vy*b.ay;
// what shall i do with b itself?
   }
 }
}

i guess someone of you already made stuff like this and can tell me how to handle it in general
Re: billiard balls physics
Reply #1 - May 9th, 2007, 9:25pm
 
Keith Peters has a great tutorial on billiard ball physics in his book Actionscript Animation (http://friendsofed.com/book.html?isbn=1590597915)
Re: billard balls
Reply #2 - May 9th, 2007, 11:58pm
 
You should sit down and get to grips with vector math first. It's faster than doing it all in trigonometry and once you get your head around it you can do some really cool stuff.

Most of what I've learned recently has come from this page on vectors for Flash and from picking apart Flade, a Flash physics engine that uses verlet integration.

Verlet integration simply means that you do away with velocity and store where you were and where you are now. The beauty of doing this is that you have the data to calculate your trajectory on hand at all times. Plus you can wind back the clock when you collide with an object because you know where you were last frame.

I've put together a simple verlet integration demo below with a bit of vector math as well. There is no need for cos, sin or atan2 unless you are rotating something.

Code:

float damping = 0.96;
float gravityX = 0.0;
float gravityY = 0.0;
Particle p;

void setup(){
size(400, 400);
p = new Particle(200, 200);
smooth();
}

void draw(){
background(255);
p.verlet();
p.collision();
p.draw();
if(mousePressed){
p.getVector(mouseX, mouseY);
p.xPrev -= p.dx;
p.yPrev -= p.dy;
}
}

class Particle{
// Verlet stuff
float xNow, yNow, xPrev, yPrev;
// Vector math stuff
float dx, dy, vx, vy, len;
// Constructor
Particle(float x, float y){
xNow = xPrev = x;
yNow = yPrev = y;
}
void draw(){
ellipse(xNow, yNow, 20, 20);
}
// velocity free movement calculation - plus you can calculate your direction of motion
void verlet(){
float tempX = xNow;
float tempY = yNow;
xNow += damping * (xNow - xPrev) + gravityX;
yNow += damping * (yNow - yPrev) + gravityY;
xPrev = tempX;
yPrev = tempY;
}
void collision(){
// more research for you sonny
}
// Work out some simple vector math
void getVector(float x, float y){
vx = x - xNow;
vy = y - yNow;
len = dist(x, y, xNow, yNow);
// dx is equal to cos(atan2(vy, vx));
// dy is equal to sin(atan2(vy, vx));
// the arc trigonometry function against the trigonometry function cancel each other out
dx = vx / len;
dy = vy / len;
}
}
Re: billard balls
Reply #3 - May 10th, 2007, 12:27pm
 
hi st33d. thanks for the link.i gonna work trough it. yet i wasn't able to open the .fla sourcefiles with a simple editor. all i used made kind of mud of it. do you know a good one?
Re: billard balls
Reply #4 - May 10th, 2007, 6:53pm
 
Just thought I'd chime in with st33d:  whenever atan() is immediately followed by sin/cos, a normalized vector is probably what was wanted.  You could solve that entire collision response with trig but it'll be messy.  You'll need the collision normal to project velocity onto, then the result is remaining (tangent) velocity minus projected velocity.  That's about 3 vector steps (V'=V-2(N.V)N), but maybe ten times that many with trig.
Page Index Toggle Pages: 1