How to set LinearVelocity exactly in onSceneTouchEvent?

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:

And my code:

@Override
public boolean onSceneTouchEvent(final Scene pScene,
final TouchEvent pSceneTouchEvent) {
ballObject.getBodyball().setLinearVelocity(
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:

v.nor();


Then you can set desired speed by:

v.mul(scalar);


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

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