JScriptChan
Junior Member
Offline
Posts: 57
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; } }