PDA

View Full Version : Math & Physics

Sleeping_Troll
07-08-2009, 03:05 PM
I have the following collision detection and resolution script:

function collisions(){
var i,q,Proximity,VelocityXa,VelocityYa,VelocityZa,VelocityXb,VelocityYb,VelocityZb,Xdisplacement,Ydispl acement,Zdisplacement,Xvector,Yvector,Zvector,E,ResultantTotal,Xresultant,Yresultant,Zresultant;
for(i=0;i<numOrbs;i++){
Perspective=(Zeye-Zscreen)/(Zeye+Orb[i].PositionZ);
OrbProximityFactor=4*Orb[i].size*Orb[i].size;
for(q=(i+1);q<numOrbs;q++){
Proximity=(Orb[i].PositionX-Orb[q].PositionX)*(Orb[i].PositionX-Orb[q].PositionX)+(Orb[i].PositionY-Orb[q].PositionY)*(Orb[i].PositionY-Orb[q].PositionY)+(Orb[i].PositionZ-Orb[q].PositionZ)*(Orb[i].PositionZ-Orb[q].PositionZ);
if (Proximity<=OrbProximityFactor){
Proximity=Math.sqrt(Proximity);
VelocityXi=Orb[i].VelocityX;
VelocityYi=Orb[i].VelocityY;
VelocityZi=Orb[i].VelocityZ;
VelocityXq=Orb[q].VelocityX;
VelocityYq=Orb[q].VelocityY;
VelocityZq=Orb[q].VelocityZ;
Xdisplacement=Orb[q].PositionX-Orb[i].PositionX;
Ydisplacement=Orb[q].PositionY-Orb[i].PositionY;
Zdisplacement=Orb[q].PositionZ-Orb[i].PositionZ;
if (Math.abs(Proximity)>0.0001){
Xvector=Xdisplacement/Proximity;Yvector=Ydisplacement/Proximity;Zvector=Zdisplacement/Proximity;
E = Math.sqrt(VelocityXi*VelocityXi+VelocityYi*VelocityYi+VelocityZi*VelocityZi);
if (Math.abs(E)>0.0001){
TotalResultant = (Xvector*VelocityXi+Yvector*VelocityYi+Zvector*VelocityZi);
Xresultant=TotalResultant*Xvector;
Yresultant=TotalResultant*Yvector;
Zresultant=TotalResultant*Zvector;
VelocityXi -= Xresultant;
VelocityYi -= Yresultant;
VelocityZi -= Zresultant;
VelocityXq += Xresultant;
VelocityYq += Yresultant;
VelocityZq += Zresultant;
}
E = Math.sqrt(VelocityXq*VelocityXq+VelocityYq*VelocityYq+VelocityZq*VelocityZq);
if (Math.abs(E) >0.0001){
TotalResultant = (Xvector*VelocityXq+Yvector*VelocityYq+Zvector*VelocityZq);
Xresultant=TotalResultant*Xvector;
Yresultant=TotalResultant*Yvector;
Zresultant=TotalResultant*Zvector;
VelocityXi += Xresultant;
VelocityYi += Yresultant;
VelocityZi += Zresultant;
VelocityXq -= Xresultant;
VelocityYq -= Yresultant;
VelocityZq -= Zresultant;
}
Orb[i].VelocityX=VelocityXi;Orb[i].VelocityY=VelocityYi;Orb[i].VelocityZ=VelocityZi;
Orb[q].VelocityX=VelocityXq;Orb[q].VelocityY=VelocityYq;Orb[q].VelocityZ=VelocityZq;
}
}
}
}
moveOrbs();
}

It uses Pythagorus applied to the diametric plane of a cube to determine proximity in space. (I don't think there is a problem here) "E" represents the Energies resolution, if it is less than .0001 (Meaning the particles are very close in energy and velocity) Adhesion will occur between particles (No problem here either). The problem seems to be in my math for values >.0001 collisions do not seem to resolve energies the way they should. This script is running at http://OrbZOrbZ.com , collisions are not critical to the site but would certainly enhance it greatly. First solution gets free ad space on the site!

Dunna
07-16-2009, 01:53 AM
When you say that "the collisions do not seem to resolve energies the way they should", in what way exactly?