This site requires JavaScript, please enable it in your browser!
Greenfoot back
jlee2014
jlee2014 wrote ...

2014/4/23

How to make this code make object go left not right?

jlee2014 jlee2014

2014/4/23

#
How would you make the object with this code go to the left of the screen and not the right?
    private void applyGravity(Seagull3 other)
    {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector force = new Vector (dx, dy);
        double distance = Math.sqrt (dx*dx + dy*dy);
        double strength = GRAVITY * this.mass * other.mass / (distance * distance);
        double acceleration = strength / this.mass;
        force.setLength (acceleration);
        addForce (force);
    }
    
davmac davmac

2014/4/23

#
this method does not specifically make an object go either left or right; it makes the object move towards another object. If you want it move in the other direction, invert dx and dy - in other words, change lines 3 and 4 to:
    double dx = this.getExactX() - other.getExactX();  
    double dy = this.getExactY() - other.getExactY();
You need to login to post a reply.