I'm beginner in Android programming. And i use AndEngine GLS2 with Body and AnimatedSprite.

I have a object ball and i want to move it to follow my hand. This is description for my idea: Vector

And my code:

public boolean onSceneTouchEvent(final Scene pScene,
            final TouchEvent pSceneTouchEvent) {
                    new Vector2(pX, pY));

How to get exactly variables:pX, pY ?


This is a simple math. First you find the vector between those two points. Then create a vector (use vector pool) from it. Let's say your touch coordinates are [p1x, p1y] and the up coordinates are [p2x, p2y]:

dx = p2x - p1x;
dy = p2y - p1y;

Vector2 v = Vector2Pool.obtain(dx, dy);

You can normalize it to get the same speed everytime. Simply call:


Then you can set desired speed by:


Where scalar is a constant to make it go faster or slower. See the Vector2 class for more.

  • \$\begingroup\$ How to set speed limit for ball object? It seems to run very fast. Thanks! \$\endgroup\$ – LazyCatIT May 8 '14 at 3:00
  • \$\begingroup\$ I've updated the answer. \$\endgroup\$ – MartinTeeVarga May 8 '14 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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