0
\$\begingroup\$

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:

@Override
public boolean onSceneTouchEvent(final Scene pScene,
            final TouchEvent pSceneTouchEvent) {
     ballObject.getBodyball().setLinearVelocity(
                    new Vector2(pX, pY));
}

How to get exactly variables:pX, pY ?

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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\$ – sm4 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.