1
\$\begingroup\$

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.

Update

This is my code:

        final AnimatedSprite face;
        final Body body;
        face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion,
                this.getVertexBufferObjectManager());
        body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
                BodyType.DynamicBody, FIXTURE_DEF);
        face.setUserData(body);
        face.animate(200);
        face.applyTorque(100);
        face.setUserData("monster");
        this.mScene.registerTouchArea(face);
        this.mScene.attachChild(face);
        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() {
            @Override
            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")) {
                        x1.getBody().setFixedRotation(true);
                        hitsound.play();
                        mEngine.vibrate(100);
                        Log.i("CONTACT", "BETWEEN PLAYER AND MONSTER!");
                    }
                } catch (Exception e) {
                    Log.d("ErrorMessage", e.getMessage());
                }
            }
            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {
            }
            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
            }
            @Override
            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?

\$\endgroup\$
0
\$\begingroup\$

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:

body.applyTorque(force);

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

body.setLinearVelocity(0);
body.setAngularVelocity(0); 

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

body.setActive(false);

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

\$\endgroup\$
  • \$\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 '14 at 3:34
  • \$\begingroup\$ @LazyCatIt You can set fixed rotation to true on collision. \$\endgroup\$ – sm4 Apr 24 '14 at 12:57
  • \$\begingroup\$ I have applied it, but doesn't work. Please see my code, i have updated. Thanks. \$\endgroup\$ – LazyCatIT Apr 25 '14 at 1:34

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.