0
\$\begingroup\$

I am using AndEngine for Android. I created a body, but it is not behind the sprite as I wanted.

                                Body.setTransform(Sprite.getX()/32, Sprite.getY()/32, 0);

This is what I am currently using and this is how it appears.

enter image description here

My body is the white circular borders, and my sprite is the red circle.

and When I remove /32 from the code the body doesn't even appear.

Extra code;

                    FIXTURE_DEF = PhysicsFactory.createFixtureDef(
                            1,     // density
                            0.75f,  // elasticity
                            0.5f, // friction
                            false); // isSensor

the Body

                Body = PhysicsFactory.createCircleBody(this.physicsWorld, Sprite, BodyType.KinematicBody, this.FIXTURE_DEF);
                Body.setUserData("eSprite");
                this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(Sprite, Body, true, true)

my sprite is 65px ..

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

I'm not an 'andengine' user so I can't give you the correct code.

However it seems that your problem is that you are targeting the sprite x & y position which is wrong.

You must target the sprite x + his center x, and the sprite y + his center y.

Body.setTransform((Sprite.getX() + Sprite.getWidth()/2) /32, (Sprite.getY()+Sprite.getHeight()/2)/32, 0);

I guess 32 is the indice to convert values for your box2d world. if you don't know the correct way to do it you should use this code :

 indice_w = box2d_width / render_width;
 indice_h = box2d_height / render_height;

render_width is camera viewport used to render the sprite;

box2d_width is the viewport of your box2d world;

Thus you can do this code :

Body.setTransform((Sprite.getX() + Sprite.getWidth()/2) * indice_w, (Sprite.getY()+Sprite.getHeight()/2) * indice_h, 0);

I hope this will help you.

\$\endgroup\$

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.