I'm beginner in Android programming with Andengine framework. And my question:

How to rotate an image around its own center in AndEngine?

I use Body and AnimatedSprite.


This is my code:

        final AnimatedSprite face;
        final Body body;
        face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion,
        body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
                BodyType.DynamicBody, FIXTURE_DEF);
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
                body, true, true));

And here in the ContactListener when have a collision with other object.

private ContactListener createContactListener() {
        ContactListener contactListener = null;
        contactListener = new ContactListener() {
            public void beginContact(Contact contact) {
                try {
                    Fixture x1 = null;
                    Fixture x2 = null;
                    x1 = contact.getFixtureA();
                    x2 = contact.getFixtureB();
                    if (x2.getBody().getUserData().equals("player")
                            && x1.getBody().getUserData().equals("monster")) {
                        Log.i("CONTACT", "BETWEEN PLAYER AND MONSTER!");
                } catch (Exception e) {
                    Log.d("ErrorMessage", e.getMessage());
            public void preSolve(Contact contact, Manifold oldManifold) {
            public void postSolve(Contact contact, ContactImpulse impulse) {
            public void endContact(Contact contact) {
        return contactListener;

I have set x1.getBody().setFixedRotation(true);. But position of this object isn't kept. What is wrong with my code?


1 Answer 1


Without physics you can use setRotation and also register rotation entity modifier to make it rotate from its current to the desired angle. When you add physics, you should no longer directly rotate an object around it's center, you should to use forces and impulses to give the body a spin. In your case:


This won't work if you set fixed rotation to true. You can use the following to set the rotation directly.

body.setTransform(positionVector, rotation);

However if you set the fixed rotation to true, you can still use the entity modifier:

face.registerEntityModifier(new RotationModifier(timeInSeconds, fromAngle, toAngle));

When solving collisions, you have to use the if both ways, because it is not guaranteed player will be x2. If you want to stop the object from moving and rotating, simply call


But you should do this in endContact(). Also you might want to call


It will remove the body from simulation. It should keep its position and rotation. It would no longer collide with anything.

  • \$\begingroup\$ In my case, i only use body.applyTorque(100);. But I want to keep position of this object when have a collision with other object. How do this work? Thanks. \$\endgroup\$
    – LazyCatIT
    Apr 24, 2014 at 3:34
  • \$\begingroup\$ @LazyCatIt You can set fixed rotation to true on collision. \$\endgroup\$ Apr 24, 2014 at 12:57
  • \$\begingroup\$ I have applied it, but doesn't work. Please see my code, i have updated. Thanks. \$\endgroup\$
    – LazyCatIT
    Apr 25, 2014 at 1:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .