Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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(;
            // The narrower the angle, the more "focused" his moving view is
            if (angle < 30 && raycast.length() < 10) {

    // Update player position using mouse
    public void mouseMoved(int oldx, int oldy, int newx, int newy) {
        Point newVelocity = new Point(newx, newy).normalize();

    // 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;
share|improve this answer
thank you very much, this is very very helpful :) – Rangana Sampath Aug 20 '11 at 19:03

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.