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 › collision breaks down
Page Index Toggle Pages: 1
collision breaks down (Read 533 times)
collision breaks down
Feb 8th, 2008, 6:42pm
 
(excuse my english)
Hey, I´m new to this...At first; I brutaly feld in love with Processing, although im studiying fine arts,It changed
my opinion about programming...
My first more complex sketch, especially for me, tries to deal with physics and collision.
two balls which can be controlled separately collide and reflect...i managed to fix the boundary collisions, but after
the two balls collide, the one that should reflect, reflects but doesnt behave like before...in some cases.

A hint?
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


float xdirection = 0.98;
float ydirection = 0.98;
float xspeed = 1;
float yspeed = 1;
float xdirection2 = 0.98;
float ydirection2 = 0.98;
float xspeed2 = 1;
float yspeed2 = 1;
int r1=10;
int r2=20;
float velocityX,velocityY;
boolean hit =  false;
boolean hitx =  false;
boolean hity =  false;
boolean  bang=false;
float rot = 0.01;



punkt p;
punkt2 p2;

void setup(){
 size(600,600);
 smooth();
 noStroke();
 p = new punkt(50,50);
 p2 = new punkt2(50,100);
 noCursor();

}
void draw(){
 background(255);
 stroke(150);
 line(mouseX,mouseY,p.xnow,p.ynow);
 fill(0);
 ellipse(mouseX,mouseY,10,10);
 p.move();



 p.display();
 p2.display2();
 p2.move2();
 kollision();

 if(keyPressed){
   if(key=='a'){
     p.Vektor(mouseX,mouseY);
     p.xprev = p.xprev - p.dx;
     p.yprev = p.yprev - p.dy;
   }
 }
 
 p.kol_boundary();
 
 if(keyPressed){
   if(key=='s'){
     p2.Vektor2(mouseX,mouseY);
     p2.xprev2 = p2.xprev2 - p2.dx2;
     p2.yprev2 = p2.yprev2 - p2.dy2;
   }
 }
 pushMatrix();
 translate(p.xnow,p.ynow);
 rectMode(CORNER);
 rot+=0.1;
 //if(rot>10){
 //rot=0.05;}
 rotate(rot);
 rect(0,-3.5,20,5);
 popMatrix();
 noStroke();
}

class punkt{
 float xprev,yprev,xnow,ynow;
 float vx,vy,dx,dy;
 punkt(float x,float y){
   xprev = xnow = x;
   yprev = ynow = y;
 }

 void display(){
   fill(0);
   ellipse(xnow,ynow,r1*2,r1*2);
   stroke(255, 128, 0);
   line(p.xnow, p.ynow, p2.xnow2, p2.ynow2);
   noStroke();
 }

 void move(){
   float tempX = xnow;
   float tempY = ynow;


   velocityX = xdirection * xspeed;
   velocityY = ydirection * yspeed;

   if((!hitx)&&(p.xnow>(width-r1))){
     //xdirection*=-1;
     hitx=true;
     velocityX*=-1;
     p.xnow -=(((p.xnow -(velocityX* (p.xnow-p.xprev))))-(width-r1));
   }
   else if(!(p.xnow>(width-r1))){
     hitx=false;

   }
   if((!hitx)&&(p.xnow<r1)){
     hitx=true;
     velocityX*=-1;
     p.xnow = (p.xnow + (velocityX* (p.xnow-p.xprev)))+(p.xnow+r1) ;
     //xprev=10;
     //xnow += (((xnow +(velocityX/ (xnow-xprev))))+(r1-xnow));

   }
   else if(!(p.xnow<r1)){
     hitx=false;
   }

   if((!hity)&&(p.ynow>(height-r1))){
     hity=true;
     velocityY*=-1;
     p.ynow -=  (((p.ynow - (velocityY * (p.ynow-p.yprev))))-(height-r1));
     //yprev=height-10;
   }
   else if(!(p.ynow>(height-r1))){
     hity=false;
   }

   if((!hity)&&(p.ynow<r1)){
     //ynow=ynow-r1;
     velocityY*=-1;
     //yprev=10;
     p.ynow = (p.ynow + (velocityY* (p.ynow-p.yprev)))+(p.ynow+r1) ;
     //ynow+=(((ynow + (velocityY / (ynow-yprev))))+(r1-ynow));
   }
   else if(!(p.ynow<r1)){
     hity=false;
   }
   xnow = xnow + (velocityX* (xnow-xprev)) ;
   ynow = ynow + (velocityY * (ynow-yprev)) ;
   xprev = tempX;
   yprev = tempY;
 }

 void kol_boundary(){
 }

 void Vektor(float x,float y){
   vx = x - xnow;
   vy = y - ynow;
   dx = cos(atan2(vy,vx));
   dy = sin(atan2(vy,vx));
 }
}

void kollision(){
 float d = dist(p.xnow,p.ynow,p2.xnow2,p2.ynow2);
 //println(d);
 if((d<r1+r2)){
   float incidenceVectorDist = dist((p.xprev), (p.yprev), p.xnow,p.ynow);
   //println(incidenceVectorDist);
   float incidenceVectorX = (p.xprev-p.xnow);
   float incidenceVectorY = (p.yprev-p.ynow);
   incidenceVectorX /= incidenceVectorDist;
   incidenceVectorY /= incidenceVectorDist;
   float normalVectorX = (p.xnow-p2.xnow2);
   float normalVectorY = (p.ynow-p2.ynow2);
   normalVectorX /= d;
   normalVectorY /= d;
   float dot = incidenceVectorX*normalVectorX + incidenceVectorY*normalVectorY;
   float reflectionVectorX = normalVectorX*2*dot - incidenceVectorX;
   float reflectionVectorY = normalVectorY*2*dot - incidenceVectorY;
   xdirection = reflectionVectorX;
   xdirection = reflectionVectorY;
   p.xnow = p2.xnow2+(r1+r2)*normalVectorX;
   p.ynow = p2.ynow2+(r1+r2)*normalVectorY;
   bang=true;
 }
}

class punkt2{
 float xprev2,yprev2,xnow2,ynow2;
 float vx2,vy2,dx2,dy2;
 punkt2(float x,float y){
 xprev2 = xnow2 = x;
 yprev2 = ynow2 = y;
 }
 
 void display2(){
   fill(0);
   ellipse(xnow2,ynow2,r2*2,r2*2);
     }
     
  void move2(){
    float tempX2 = xnow2;
    float tempY2 = ynow2;
 
    xnow2 = xnow2 + (xspeed2 * xdirection2 * (xnow2-xprev2)) ;
    ynow2 = ynow2 + (xspeed2 * xdirection2 * (ynow2-yprev2)) ;
   
    xprev2 = tempX2;
    yprev2 = tempY2;
    }
     
 void Vektor2(float x,float y){
   vx2 = x - xnow2;
   vy2 = y - ynow2;
   dx2 = sin(atan2(vx2,vy2));
   dy2 = cos(atan2(vx2,vy2));}

}

-------------------
thanks
Re: collision breaks down
Reply #1 - Feb 9th, 2008, 9:02pm
 
I've had a go at this sort of thing too:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Exhibition;action=display;num=1201461240

Although I use a stronger version of the above code now because it is flawed. I needed to go through your code to make sure it is impossible to divide by zero.

Your code seems to behave fine. I probably don't know how to break it.

Try writing your code to deal with another ball that cannot move. One that never has velocity. This broke my own ball collider and helped me figure out how to make it stronger.
Re: collision breaks down
Reply #2 - Feb 9th, 2008, 10:11pm
 
ok i´ll try it...somehow
i used the same link, you mentioned, as a reference
for the reflection thing...
Page Index Toggle Pages: 1