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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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; } |