Wild Goat
Wild Goat

Reputation: 3589

Move point away from another point

I am working on Simulation, where one objectA follows objectB and objectB is running away.

I have implemented method which move my point closer to another point, but I'm stuck how to implement 'run away' action.

Lets say I have a screen 100x100.

Here it is my method which represents follow method.

public MutableDouble2D MovePointTowards(Double2D pointA, Double2D pointB, double distance)
{

    MutableDouble2D vector = new MutableDouble2D();
    vector.x = pointB.x - pointA.x;
    vector.y = pointB.y - pointA.y;

    double length = Math.sqrt(vector.x * vector.x + vector.y * vector.y);

    MutableDouble2D unitVector = new MutableDouble2D();
    unitVector.x = vector.x / length;
    unitVector.y = vector.y / length;

    MutableDouble2D newPoint = new MutableDouble2D();
    newPoint.x = pointA.x + unitVector.x * distance;
    newPoint.y = pointA.y + unitVector.y * distance;

    return newPoint;
}

Could you guys help me to develop 'run away' action? Thanks!

Upvotes: 0

Views: 1504

Answers (2)

Parakram Majumdar
Parakram Majumdar

Reputation: 684

I'm going to call them Lion and Goat. I'm assuming you are running some loop as follows :

while( true )
{
  lionPos = moveTowards( goatPos, lionPos, d );
  goatPos = someFunction();
}

If d is a constant, then lion chases the goat with constant velocity. If you want the goat to run away with constant velocity, then someFunction is nothing but

moveTowards( goatPos, lionPos, d );

Again, changing d will change the velocity. If you put distance-dependent velocities you should be able to see interesting simulations. Try putting constant vel for the Lion, but ( 1/R^2 ) vel for the Goat. Then the Lion will catch up when it's far, but as soon as it comes close enough, the Goat will run away faster.

If you want the Goat to intelligently run away from the Lion (not necessarily directly the opposite direction), there is some interesting literature on that. One neat discussion is here.

Upvotes: 1

PeskyGnat
PeskyGnat

Reputation: 2464

It would be almost what you already have, instead of displacing Point A to get newPoint, just displace Point B by that same vector.

Upvotes: 0

Related Questions