2
\$\begingroup\$

I have a 32x32 sprite and I make some to be platform like this

for (int i = 0; i < 15; i++) {
    attachChild(new Ground(32 * i, 200, regPlatform, vertexBufferObjectManager, physicsWorld));
}

And I have another sprite (32x32 also) moving over those platforms (A player object)

attachChild(new Player(10, 10, regPlayer, vertexBufferObjectManager, physicsWorld));

And the physicsWorld connects the player like this (Where move=1 when user click right button, and move=2 when user click left button)

physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false) {
    if (move == 1) {
        body.setLinearVelocity(new Vector2(8, body.getLinearVelocity().y));
    } else if (move == 2) {
        body.setLinearVelocity(new Vector2(-8, body.getLinearVelocity().y));
    }
}

The Player moves smoothly form left to right and reverse. But sometimes, it is blocked. If the player move back a bit, and back again where it is blocked, it isn't blocked any more at the same place.

Anyone has any idea to fix this? Maybe it is a bug of andengine and box2d and I need some trick here? Please help me. (The two sprites above are just normal png with 32x32px)

Player image:

enter image description here

Platform image:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Is it jamming itself into the other body? \$\endgroup\$ – Vaughan Hilts Apr 9 '13 at 17:24
  • \$\begingroup\$ I find a thread about box2d problem but not sure about it www.box2d.org/forum/viewtopic.php?f=8&t=2730 \$\endgroup\$ – MashiMaro Apr 9 '13 at 18:48
4
\$\begingroup\$

AndEngine's physics extension is a port of Box2D. And Box2D's faq says this about tile based environments that should be "smooth":

Using many boxes for your terrain may not work well because box-like characters can get snagged on internal corners. A future update to Box2D should allow for smooth motion over edge chains. In general you should avoid using a rectangular character because collision tolerances will still lead to undesirable snagging.

For more information see this post: http://box2d.org/forum/viewtopic.php?f=3&t=3048

Your description sounds like this problem.

\$\endgroup\$
  • \$\begingroup\$ Thanks Seth Battin for the answer. It is exactly my problem. I've read the post and have the solution:<br/>1. if you're using simple square/rectangular tiles everywhere, is to just merge any horizontally adjacent tiles into longer rectangles. This takes very little effort code-wise and should solve the problem.<br/>2. Most professional games using modern physics engines use smooth shapes, like capsules, for characters. Using box shapes for characters really only works well in systems that use pixel based collision with integral math (i.e. older console games). \$\endgroup\$ – MashiMaro Apr 10 '13 at 6:41

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.