Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Math & Physics

  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Math & Physics

    I have the following collision detection and resolution script:
    Code:
    function collisions(){
    	var i,q,Proximity,VelocityXa,VelocityYa,VelocityZa,VelocityXb,VelocityYb,VelocityZb,Xdisplacement,Ydisplacement,Zdisplacement,Xvector,Yvector,Zvector,E,ResultantTotal,Xresultant,Yresultant,Zresultant;
    	for(i=0;i<numOrbs;i++){
    		Perspective=(Zeye-Zscreen)/(Zeye+Orb[i].PositionZ);
    		Orb[i].size=OrbBaseRadius*Perspective;
    		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!
    Last edited by Sleeping_Troll; 07-08-2009 at 02:33 PM.

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    When you say that "the collisions do not seem to resolve energies the way they should", in what way exactly?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •