My objects accelerate in the positive x direction and I'm not sure why.
The objects should accelerate towards each other their respect to their mass
This is the code
Edit: Link to my scenario: http://www.greenfoot.org/scenarios/11828
public void act()//in mass1 object class
{
setLocation(getX()+velocity.getX(),getY()+velocity.getY());
myWorld world = (myWorld)getWorld();
Mass2 mass2 = world.getMass2();
vector grav = gravitateTo(mass2, mass2.mass);
velocity.addTo(grav);
}
public double angleTo(Actor a)//in mover class now
{
return Math.atan2(a.getY()-this.getY(), a.getX()-this.getY());
}
public double distanceTo(Actor a)
{
double dx = a.getX()-this.getX();
double dy = a.getY()-this.getX();
return Math.sqrt(dx*dx+dy*dy);
}
public vector gravitateTo(Actor a, double a_mass)
{
vector grav = new vector(1,0);
double dist = this.distanceTo(a);
double angle = angleTo(a);
double length = getFG(a_mass, dist);
grav.setX(length*Math.cos(angle));
grav.setY(length*Math.sin(angle));
return grav;
}
public double getFG(double m1, double r)
{
double FG = G*((m1)/(r*r));
return FG;
}
