- #1
Vold
- 8
- 0
Homework Statement
Determine if two circular objects (both moving or one stationary), collide and if so, determine their final velocity.
The two objects are being rendered using Graphics's Java library class, updating their position 60 times per second. Their position (x,y) is determined based on the Width and Length of the frame, and their velocity is given in pixels (eg: -1 , 2; the object moves 1 pixel to the right and 2 pixels upwards (Y-Axis is inverted), the objects have a mass and a radius.
Homework Equations
Here.
Note: In the code below there is a modification to the equation (theta of the other object is replaced by Phi - 2PI), because I was told that the equation wasn't accurate (I may be mistaked though, regardless this does not fix the problem either way).
The Attempt at a Solution
Here is the class that tries to do the math but fails miserably. Sometimes the objects collide properly but sometimes very awkwardly. I think that my main issue is the inverted Y-Axis.
Java:
import com.game.src.gameObjects.GameObject;
import java.util.LinkedList;
public class Physics {
private Controller1 c1;
private Controller2 c2;
private LinkedList<GameObject> gameObjects; public Physics(Controller1 c1, Controller2 c2){
this.c1 = c1;
this.c2 = c2;
gameObjects = new LinkedList<>();
}
public void tick(){
gameObjects.add(c1.getShip());
gameObjects.add(c1.getWeapon1());
gameObjects.add(c1.getWeapon2());
gameObjects.add(c1.getWeapon3());
gameObjects.addAll(c2.getAsteroids());
collisions(gameObjects);
gameObjects.clear();
}
public void collisions(LinkedList<GameObject> modules){
double m1Vxf;
double m1Vyf;
double m2Vxf;
double m2Vyf;
for(int i = 0; i < modules.size(); i++){
for (int j = 0; j < modules.size(); j++){
GameObject m1 = modules.get(i);
GameObject m2 = modules.get(j);
if(m1.intersects(m2)){
if (m1.getFaction() != m2.getFaction()){
m1Vxf = (( ( Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * (Math.cos(m1.theta()-m1.phi(m2))) * (m1.getMass() - m2.getMass()) + 2 * m2.getMass() * Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * (Math.cos(Math.PI/2 - m1.phi(m2))) ) * Math.cos(m1.phi(m2))) / (m1.getMass()+m2.getMass()) + Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * Math.sin(m1.theta()-m1.phi(m2))*Math.cos(m1.phi(m2)+Math.PI/2) );
m1Vyf = (( ( Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * (Math.cos(m1.theta()-m1.phi(m2))) * (m1.getMass()- m2.getMass()) + 2 * m2.getMass() * Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * (Math.sin(Math.PI/2-m1.phi(m2))) ) * Math.sin(m1.phi(m2))) / (m1.getMass()+m2.getMass()) + Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * Math.sin(m1.theta()-m1.phi(m2))*Math.sin(m1.phi(m2)+Math.PI/2) );
m2Vxf = (( ( Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * (Math.cos(m2.theta() - m2.phi(m1))) * (m2.getMass()- m1.getMass()) + 2 * m1.getMass() * Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * (Math.cos(Math.PI/2 - m2.phi(m1))) ) * Math.cos(m2.phi(m1)))/ (m2.getMass()+m1.getMass()) + Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * Math.sin(m2.theta() - m2.phi(m1))*Math.cos( m2.phi(m1)+Math.PI/2) );
m2Vyf = (( ( Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * (Math.cos(m2.theta() - m2.phi(m1))) * (m2.getMass()- m1.getMass()) + 2 * m1.getMass() * Math.sqrt( Math.pow(m1.getVelX(),2) + Math.pow(m1.getVelY(),2) ) * (Math.sin(Math.PI/2 - m2.phi(m1))) ) * Math.sin(m2.phi(m1)))/ (m2.getMass()+m1.getMass()) + Math.sqrt( Math.pow(m2.getVelX(),2) + Math.pow(m2.getVelY(),2) ) * Math.sin(m2.theta() - m2.phi(m1))* Math.sin( m2.phi(m1)+Math.PI/2) );
m1.setVelX(m1Vxf);
m1.setVelY(m1Vyf);
m2.setVelX(m2Vxf);
m2.setVelY(m2Vyf);
//Temporary solution to the objects getting stuck () they only collide once).
m2.setFaction(m1.getFaction());
}
}
}
}
}}
Here are the methods used by the previous class, which are all in the GameObject abstract class.
Java:
public boolean intersects(GameObject m){
if( Math.pow(this.getXr() - m.getXr(), 2 ) + Math.pow(this.getYr() - m.getYr() , 2 ) <= Math.pow(this.getRadius() + m.getRadius(), 2) )
return true;
else
return false;
}public double theta(){
//si this.y - m.y = 0 y this.x-m.x < 0 -> 0.
if(this.getVelY() == 0 && this.getVelX() < 0){
return Math.PI;
//si this.y - m.y > 0 y this.x-m.x = 0 -> 90.
}else if(this.getVelY() > 0 && this.getVelX() == 0){
return 3*Math.PI/2;
//si this.y - m.y = 0 y this.x-m.x > 0 -> 180.
}else if(this.getVelY() == 0 && this.getVelX() > 0){
return 0;
//si this.y - m.y < 0 y this.x-m.x = 0 -> 270.
}else if(this.getVelY() < 0 && this.getVelX() == 0){
return Math.PI/2;
}else if(this.getVelY() > 0 && this.getVelX() < 0){
return Math.PI + Math.atan2(this.getVelY() , this.getVelX()) ;
}else if(this.getVelY() > 0 && this.getVelX() > 0){
return 2*Math.PI - Math.atan2(this.getVelY() , this.getVelX()) ;
}else if(this.getVelY() < 0 && this.getVelX() > 0){
return Math.atan2(this.getVelY() , this.getVelX() );
}else if(this.getVelY() < 0 && this.getVelX() < 0){
return Math.PI - Math.atan2(this.getVelY() , this.getVelX()) ;
}else
return Math.atan2(this.getVelY() , this.getVelX() );
}
public double phi(GameObject m){
//si this.y - m.y = 0 y this.x-m.x < 0 -> 0.
if(this.getYr() - m.getYr() == 0 && this.getXr() - m.getXr() < 0){
return 0;
//si this.y - m.y > 0 y this.x-m.x = 0 -> 90.
}else if(this.getYr() - m.getYr() > 0 && this.getXr() - m.getXr() == 0){
return Math.PI/2;
//si this.y - m.y = 0 y this.x-m.x > 0 -> 180.
}else if(this.getYr() - m.getYr() == 0 && this.getXr() - m.getXr() > 0){
return Math.PI;
//si this.y - m.y < 0 y this.x-m.x = 0 -> 270.
}else if(this.getYr() - m.getYr() < 0 && this.getXr() - m.getXr() == 0){
return 3*Math.PI/2;
}else if(this.getYr() - m.getYr() > 0 && this.getXr() - m.getXr() < 0){
return Math.atan2((this.getYr() - m.getYr()) , this.getXr() - m.getXr() );
}else if(this.getYr() - m.getYr() > 0 && this.getXr() - m.getXr() > 0){
return Math.PI - Math.atan2((this.getYr() - m.getYr()) , this.getXr() - m.getXr()) ;
}else if(this.getYr() - m.getYr() < 0 && this.getXr() - m.getXr() > 0){
return Math.PI + Math.atan2((this.getYr() - m.getYr()) , this.getXr() - m.getXr() );
}else if(this.getYr() - m.getYr() < 0 && this.getXr() - m.getXr() < 0){
return 2*Math.PI - Math.atan2(this.getYr() - m.getYr() , this.getXr() - m.getXr() );
}else
return Math.atan2((this.getYr() - m.getYr()) , this.getXr() - m.getXr() );
public double getXr() {
return getX()+ getRadius();
}
public double getYr() {
return getY()+ getRadius();
}
This problem is driving me mad, please help me. Thanks in advance. :)
Last edited: