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 have recently achieved to render box2d Objects, and texture with sprites at where the box2d objects exists. But now I am facing another problem.

When I rotate the Box2D object, the sprite gets rotated at the same value, and the texture should be rendered over the box2d Objects. But on the actual, as on the photo texture does not fully fit with the objects.

Box seems to have transformed its shape, which results rect area reaching outside of texture. I suppose I am either rotating the box2d objects in a wrong way or not using some kind of flags to keep proportion of box2d objects...

Below is the source :

@exec()

for( Iterator<Body> iter = mWorld.getBodies() ; iter.hasNext() ; ){
        Body body = iter.next( ) ;
        if( body.getUserData( ) != null ) {
            Box2DUserData data = (Box2DUserData) body.getUserData( ) ;
            if( data.spinFlag ) {
                body.setFixedRotation(false);
                body.setAwake(true);
                body.setActive(true);

                data.rot += 0.1f ;
                body.setTransform(body.getPosition(), data.rot);
            }
        }
    } 

@draw()

for( Iterator<Body> iter = mWorld.getBodies() ; iter.hasNext() ; ){
        Body body = iter.next( ) ;
        if( body.getUserData( ) != null ) {
            Box2DUserData data = (Box2DUserData) body.getUserData( ) ;
                Sprite sprite = data.sprite;
                Vector2 position = body.getPosition();
                position.x -= ( mCamera.position.x - VIEW_PORT.x / 2 + data.sizeX ) ;
                position.y -= ( mCamera.position.y - VIEW_PORT.y / 2 + data.sizeY ) ; //+ Gdx.graphics.getHeight() / 2 ;
                position.x *= UNIT_X ;
                position.y *= UNIT_Y ;

                sprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());
                sprite.setBounds( position.x, position.y, ( float )( UNIT_X * data.sizeX * 2 ), ( float )( UNIT_Y * data.sizeY * 2 ) );
                sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
                if( data.animationFlag ) {
                    data.animate( ) ;
                    sprite.setRegion( data.textureRect.left + data.animationCount * data.textureRect.right, data.textureRect.top, data.textureRect.right, data.textureRect.bottom ) ;
                }
                else {
                    sprite.setRegion( data.textureRect.left, data.textureRect.top, data.textureRect.right, data.textureRect.bottom ) ;
                }
                sprite.draw(spriteBatch);
        }
    }

Thanks

share|improve this question
    
Keep in mind that rotation values from Box2D are in radians and start to the right. Eg. your sprites have to look to the right when they aren't rotated, otherwise you'll have to add a rotation offset to the value calculated by Box2D. –  bummzack Feb 29 '12 at 8:02
    
Thanks. But it was my bad, the problem was due to the setting of camera. My camera view port was not set to the ratio of android, and due to this the sprite did not fit box2d object area. Sorry, but thanks for your help. –  minami Mar 2 '12 at 4:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.