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 use a DigitalOnScreenControl (knob with a four-directional arrow control) to move the entity and the entity which is bound to a physicsHandler.


From the DigitalOnScreenControl, I know which direction I want my sprite to move. Inside its overridden onControlChange function, I call a function animateSprite that checks which direction I chose. Based on the direction, I animate my sprite differently.

Problem: I want to automatically move the sprite to a specific location on the scene, say at coordinates (207, 305). My sprite is at (100, 305, which means it has to move down by 107 pixels.

How do I tell the physicsHandler to move the sprite down by 107 pixels? My animateSprite method will take care of animating the sprite's downward motion.

share|improve this question

why dont you extend the PhysicsHandler class yourself and add automatic movement to the sprite then call the onUpdate(). For example :

public class Handler extends PhysicsHandler {

public Handler(IEntity pEntity) {


protected void onUpdate(float pSecondsElapsed, IEntity pEntity) {
    // TODO move the pEntity down. eg: pEntity.sety(300)
    super.onUpdate(pSecondsElapsed, pEntity);
share|improve this answer

I assume you are using Box2D extension from AndEngine. What you are looking for is MouseJoint. There is a working example of MouseJoint in the AndEngineExamples projects that you probably already have. The class you are looking for is PhysicsMouseJointExample.

MouseJoint is used to send the body in continuous movement to a target location. Basically you create MouseJoint as any other joint and then use its method setTarget().

share|improve this answer

I'm not familiar with andengine, but it uses box2d so there should be some method similar to:

PhysicsBody.setTransform(float x, float y, float angle)

Which sets the body's position to x, y and rotation to angle.

share|improve this answer
For a while, I tried to find a solution in this direction. I am able to setPosition(x,y), but then the sprite jumps. I need a smooth movement. (A bit like a ModeModifier will do.) – shailenTJ Feb 6 '13 at 12:50

Im using this listener to move my sprite around the screen. The "*10" it's like the "velocity".

        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
            playerBody.setLinearVelocity(new Vector2(pValueX*10  , pValueY*10));

Edit: The listener is inside the ScreenControl instance.

share|improve this answer
Well this code snippet will not do what I want exactly. I cannot move the sprite from its current position (X,Y) (in pixels) to (X',Y') by supplying the values X'and Y' since I know them. Essentially, the sprite is bound to the DigitalOnScreenControl but I want to move it for the special case where the joystick should not be used to do that. – shailenTJ Feb 6 '13 at 10:09

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.