Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to rotate my rectangular sprite with circle body. The problem is the body doest not attached to the centre of the sprite. This was the default behaviour of the body and sprite.

But I want that circle body attached in the bottom of the rectangle and when rotation happen at that time rectangular sprite always attached with the body at the bottom and during simulation it change its angle and position according to this only.

I try to write question in words if I don't able to understand you then know me then I post a picture of my problem here.

Although I know that cocos2d has setAnchorPoint method available so similar task become easily but what about my engine that right now I was using - AndEngine. So be relevant to this game platform.

Also thanks in advance for efforts you take.

share|improve this question

1 Answer 1

I found my solution by creating subclass of PhysicsConnector.

public class CustomPhysicsConnector extends PhysicsConnector {

    private float flagDisplacement;

    public CustomPhysicsConnector(IShape pShape, Body pBody,
                    boolean pUdatePosition, boolean pUpdateRotation,
                    float flagDisplacement) {
            super(pShape, pBody, pUdatePosition, pUpdateRotation);
            this.flagDisplacement = flagDisplacement;

    }

    @Override
    public void onUpdate(float pSecondsElapsed) {
            final IShape shape = this.mShape;
            final Body body = this.mBody;

            if (this.mUpdatePosition) {
                    final Vector2 position = body.getPosition();
                    final float pixelToMeterRatio = this.mPixelToMeterRatio;
                    shape.setPosition(position.x * pixelToMeterRatio
                                    - this.mShapeHalfBaseWidth, position.y
                                    * pixelToMeterRatio
                                    - (this.mShapeHalfBaseHeight + this.mShapeHalfBaseHeight
                                                    * flagDisplacement * 2f));
            }

            if (this.mUpdateRotation) {
                    final float angle = body.getAngle();
                    shape.setRotation(MathUtils.radToDeg(angle));
            }
    }

}

And now in CustomSprite class I have to implement in following manner.

setRotationCenter(getWidth() * 0.5f, getHeight() * 0.6f);
            angelBody = PhysicsFactory.createBoxBody(mPhysicsWorld, getX()
                            + getWidth() * 0.5f, getY() + getHeight() * 0.6f, pScale * 64f,
                            pScale * 60f, 0f, BodyType.StaticBody, fixtureDef);
angelBody.setUserData(Constants.USERDATA_ANGLE);
            mPhysicsWorld.registerPhysicsConnector(new CustomPhysicsConnector(this,
                            angelBody, true, true, 0.1f));

Now I can rotate and change position of body attached to the sprite because by default it is attached to the centre but using this it is possible to set it any position.

Here I posted answer for other users because I don't want other users waste time on same thing.

share|improve this answer

Your Answer

 
discard

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.