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 › About collision detection in 2D ,Help!
Page Index Toggle Pages: 1
About collision detection in 2D ,Help! (Read 1923 times)
About collision detection in 2D ,Help!
Aug 3rd, 2009, 5:48am
 
A few days ago I tried to simulate a scene of "wheel"s' interaction,but my code doesn't work out properly in the case of more than 3 wheels.I think its lack of speed status preservation may be the cause.Could you be kind enough to point out what's wrong?Thank you.
Codes are give below.

wheels wity;
wheel w1,w2,w3;
PFont ft;
void setup()
{
 size(500,500);
 w1=new wheel(50,50,50);
 w2=new wheel(50,150,50);
 w3=new wheel(50,250,50);
 wity=new wheels();
 wity.setwheel(w1);
 wity.setwheel(w2);
 wity.setwheel(w3);
 ft=loadFont("Albertus-Medium-48.vlw");
 textFont(ft,20);
}

void draw()
{
 background(200);
 w1.update();
 w2.update();
 w3.update();
}
class wheel
{
 float omega;
 float dx,dy;
 float theta;
 PVector center=new PVector();
 float radii;
 float flag;
 PVector d=new PVector(); //relative position vector
 PVector dm=new PVector();//motion vector
 PVector normd=new PVector();//normalized vector

   wheel(float x,float y,float _radii)
 {
   center.x=x;
   center.y=y;
   radii=_radii;
 }

 void displayatorigin()
 {

   ellipse(0,0,radii,radii);
   pushStyle();
   fill(0, 102, 153);
   text("oh",2,2);
   popStyle();// here encountered a situation where the ellipse covers the text
 }

 void spin()
 {
   pushMatrix();
   translate(center.x,center.y);
   rotate(theta+=omega);
   displayatorigin();
   popMatrix();
 }

 void movedbycursor()
 {
   dx=mouseX-pmouseX;
   dy=mouseY-pmouseY;
   center.x+=dx;
   center.y+=dy;

 }

 void movedbyothers()
 {

   if (this.cursorin()==true)// i is passive then continue
       return;


   for(int j=1;j<=wity.ws.length-1;j++)
   {
     if (wity.ws[j].flag==this.flag) continue;//not itself then continue

       d=PVector.sub(this.center,wity.ws[j].center);
     if(d.mag()<55)
     {
       dm.x=wity.ws[j].dx;
       dm.y=wity.ws[j].dy;
       normd=d.get();
       if(dm.dot(normd)>0)
       {
         normd.normalize();

         this.dx+=dm.dot(normd)*normd.x;
         this.dy+=dm.dot(normd)*normd.y;
         this.omega+=mag(dm.x-dm.dot(normd)*normd.x,dm.dot(normd)*normd.y);
       }
     }


     center.x+=dx;// Here ,dx not specified!!!
     center.y+=dy;
   }
   dx=0;
   dy=0;
 }

 boolean cursorin()//cursor seems to be an built in element
 {
   if (dist(center.x,center.y,mouseX,mouseY)<radii/2)
     return true;
   else return false;
 }

   void update()
   {
     spin();
     if (cursorin()==true)
     movedbycursor();
     else
     movedbyothers();
   }

}

class wheels
{
   wheel[] ws=new wheel[1];

   void setwheel(wheel w)
   {
     ws=(wheel[])append(ws,w);
     w.flag=ws.length-1;
   }

}
Re: About collision detection in 2D ,Help!
Reply #1 - Aug 3rd, 2009, 3:20pm
 
I think you may need to offer more explanation of what's going wrong.  I could get it to run with 4 wheels, but I've no idea if it worked as you intended.  (BTW might be nice to create them in an array then we can change the number of wheels easily by changing the length of the array.)
Re: About collision detection in 2D ,Help!
Reply #2 - Aug 3rd, 2009, 5:20pm
 
Thank you and my case is that if I have wheel A,B and C,and when I push wheel A to boost wheel B and at the same time let B boost C,the collision effect between B and C doen't exist,say,A-->B-->C fails.Maybe a tree to convey velocity is in need?Or multiple thread?
Re: About collision detection in 2D ,Help!
Reply #3 - Aug 7th, 2009, 8:11am
 
Maybe I shall go to see JBox2D:)
Re: About collision detection in 2D ,Help!
Reply #4 - Feb 19th, 2010, 8:26pm
 
PFont ft;
MM wheelinter;
void setup()
{
 size(600,600);
 ft=loadFont("Albertus-Medium-48.vlw");
 textFont(ft,20);
 wheelinter=new MM();
}

void draw()
{
 background(200);
 wheelinter.changecolor();
}

void mouseDragged()
{
 wheelinter.moved();
}
class MM
{
 PVector d=new PVector();
 PVector dm=new PVector();
 PVector normd=new PVector();
 wheel[] wheels=new wheel[3];
 float [][] movelen=new float[2][3];
 MM()
 {
   for(int i=0;i<wheels.length;i++)
   {
     wheels[i]=new wheel(100,100*i+100,50);
     wheels[i].name=nf(i,1);
   }
 }

 void display()
 {
   for(int i=0;i<wheels.length;i++)
   {
     wheels[i].spin();
   }
 }

 void cd()
 {
   for(int i=0;i<wheels.length;i++)
   {
     if (wheels[i].cursorin()==true)
         continue;
     for(int j=0;j<wheels.length;j++)
     {
       line(wheels[i].center.x,wheels[i].center.y,wheels[j].center.x,wheels[j].center.y);
       if (j==i) continue;
         d=PVector.sub(wheels[i].center,wheels[j].center);
       if(d.mag()<55)
       {
         dm.x=wheels[j].dx;
         dm.y=wheels[j].dy;
         normd=d.get();
         if(dm.dot(normd)>0)
         {
           normd.normalize();

           wheels[i].dx+=dm.dot(normd)*normd.x;
           wheels[i].dy+=dm.dot(normd)*normd.y;
           wheels[i].rotv+=mag(dm.x-dm.dot(normd)*normd.x,dm.dot(normd)*normd.y);
         }
       }
     }
     movelen[0][i]=wheels[i].dx;
     movelen[1][i]=wheels[i].dy;
   }

   for(int i=0;i<wheels.length;i++)
   {
     wheels[i].dx=movelen[0][i];
     wheels[i].dy=movelen[1][i];
     wheels[i].movedbyothers();

   }

   for(int i=0;i<wheels.length;i++)
   {
     wheels[i].dx=0;
     wheels[i].dy=0;
   }
 }

 void changecolor()
 {
   for(int i=0;i<wheels.length;i++)
   {
     if (wheels[i].cursorin()==true)
       fill(88);
     else fill(200);
     wheels[i].spin();
   }
 }
 void moved()
 {

   for(int i=0;i<wheels.length;i++)
   {
     if (wheels[i].cursorin()==true)
     {
       wheels[i].movedbycursor();
     }
     cd();
   }
 }
}
class wheel
{
 float rotv;
 float dx,dy;
 float theta;
 String name;
 wheel(){
 };
 PVector center=new PVector();
 float radii;
 wheel(float x,float y,float _radii)
 {
   center.x=x;
   center.y=y;
   radii=_radii;
 }
 void display()
 {

   ellipse(center.x,center.y,radii,radii);
 }

 void displayatorigin()
 {

   ellipse(0,0,radii,radii);
   pushStyle();
   fill(0, 102, 153);
   text(name,2,2);
   popStyle();
 }

 void spin()
 {
   pushMatrix();
   translate(center.x,center.y);

   rotate(theta+=0.02);
   displayatorigin();
   popMatrix();
 }

 void movedbycursor()
 {
   dx=mouseX-pmouseX;
   dy=mouseY-pmouseY;
   center.x+=dx;
   center.y+=dy;

 }

 void movedbyothers()
 {
   center.x+=dx;
   center.y+=dy;
 }

 boolean cursorin()
 {
   if (dist(center.x,center.y,mouseX,mouseY)<radii/2)
     return true;
   else return false;
 }
}
Re: About collision detection in 2D ,Help!
Reply #5 - Feb 19th, 2010, 8:34pm
 
Hi.Try to drag the button with the tag of '2'(the lowest) to push upward that with the tag of '1' and at the same time make it push upward that with the tag of '0'(the toppest).In short,'2' pushes '1' and '1' pushes '0'.Queer situation appears that '1' just run into '0' instead of push it upward.Any ideas? Smiley
Re: About collision detection in 2D ,Help!
Reply #6 - Feb 20th, 2010, 1:40am
 
You've said it before: jbox2d, definitely! :-)
Re: About collision detection in 2D ,Help!
Reply #7 - Feb 20th, 2010, 10:29pm
 
I haven't tracked down the root of the problem yet but I have an observation:

The wheel being pushed only detects a collision with numbers after itself.
Page Index Toggle Pages: 1