0
\$\begingroup\$

i'm going to describe what need to be done... "there are circular areas marked in a map, large areas a person can easily move in to them and out of them" what i'm trying to do is deciding weather the person is moving in to or moving out of that circular area. i can get the user location and see weather it falls inside the circle(weather the person is inside the circle) by pure mathematics theories(theories of circle), but how can we decide weather the person in moving in to the circle or moving out of the circle? please give me some advice guys?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You can do this by using the dot product of the velocity of your player.

First, you need to store your player's velocity and use it to update his position. Then, for each trigger in your level you will test if the user is moving towards the center of the trigger (using the dot product between the velocity and the trigger):

Sprite { Point position; Point velocity; Texture texture; } class Level implements MouseListener, KeyListener { Sprite player; List<LevelTrigger> triggers; ... void update(double deltaTime) { //Update player's position player.getPosition().X += player.getVelocity().X * SPEED * deltaTime; player.getPosition().Y += player.getVelocity().Y * SPEED * deltaTime; // Get player velocity and normalize its components Point velocity = player.getVelocity(); velocity = velocity.normalize(); for (LevelTrigger trigger : triggers) { // Get a vector that points to the trigger's center Point raycast = trigger.getCenter().substract(velocity); // Get the angle between the raycast and the velocity double angle = Math.acos(velocity.dot(reycast)); // The narrower the angle, the more "focused" his moving view is if (angle < 30 && raycast.length() < 10) { trigger.fireIsArriving(); } } } // Update player position using mouse public void mouseMoved(int oldx, int oldy, int newx, int newy) { Point newVelocity = new Point(newx, newy).normalize(); player.setVelocity(newVelocity); } // Update player position using keyboard public void keyPressed(int key, char c) { if (key == Input.KEY_W) { player.getVelocity().X += 1; } else if (key == Input.KEY_S) { player.getVelocity().X -= 1; } else if (key == Input.KEY_A) { player.getVelocity().Y -= 1; } else if (key == Input.KEY_D) { player.getVelocity().Y += 1; } } } 
\$\endgroup\$
0
1
\$\begingroup\$

Store the position of the person at this frame and the position of the person at the last frame.

Measure the distance to the circle center at this frame, and the distance to the circle center at the last frame.

If the distance to the circle center at this frame is less than at the last frame, then the person is moving toward the circle, otherwise the person is moving away from the circle.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.