2
\$\begingroup\$

This just started occurring and seems to randomly happen during, and between, game sessions. A dynamic moving body collides with a static body and instead of bouncing off looses (almost completely) its Velocity along the collision's normal. This means the dynamic body starts moving along the static body's surface it just collided with, and is most noticeable on the rare occasions it does a 90 degree turn upon hitting a second wall right ahead.

This behaviour is very erratic, and almost seems to come and go between builds with the same code on my content (although it could just be chance), so I'm wondering if it could be an issue with the cocos2d libraries?

For reference, the way I'm setting up these bodies:

For both types it starts with:

PhysicsBody *pawnBody = PhysicsBody::create();
PawnBody->setDynamic(true);
pawnBody->setContactTestBitmask(0xFFFFFFFF);
setPhysicsBody(pawnBody);

Then the static body code does:

myBody->removeAllShapes();
PhysicsShapePolygon *colPolygon = PhysicsShapePolygon::create(&spriteVertices[0], 4,
                                                     PhysicsMaterial(0.f, 1.f, 0.f));
colPolygon->setContactTestBitmask(0xFFFFFFFF);
myBody->addShape(colPolygon, false);
...
getPhysicsBody()->setDynamic(false);

And the dynamic body code does:

getPhysicsBody()->removeAllShapes();
PhysicsShapeCircle *circle = PhysicsShapeCircle::create((getScale() * 
                                                   mySprite->getContentSize().width) / 2,
                                                   PhysicsMaterial(0.f, 1.f, 0.f));
circle->setContactTestBitmask(0xFFFFFFFF);
getPhysicsBody()->addShape(circle);
...
myBody->setVelocityLimit(moveSpeed);
myBody->setDynamic(true);

I believe they are both set up correctly to achieve fully elastic collisions, and I'm sure this was working at some point through a slightly different flow (they didn't share the initial build code). Has anyone experienced any similar behaviour?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

It sounds very similar to the problem you get if the velocities are slower than the minimum velocity threshold for elastic collisions. You haven't specified the scales you are using, but you are using the sprites sizes which presumably are fairly small.

To fix this you need to lower b2Settings::b2_velocityThreshold, setting it too low will prevent objects from coming to a rest but you can set it to 0 to see if it fixes your problem. If it does then find the lowest value that prevents the problem but still allows the system to come to a rest.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the reply. I'm actually using Chipmunk, not Box2D. I haven't actually looked to see if Chipmunk has a similar setting, but while the problem you mention does seem very similar, my bodies are all moving at a constant equal speed, which I ensure get's maintained on every update cycle. Can this problem still manifest itself in that case? Also, I wondered if it could be related to speed relative to axis, but the issue was occurring at varying degrees of collision angles, not just when the collision was near perpendicular. \$\endgroup\$
    – Zepee
    Commented Feb 19, 2015 at 12:18
  • \$\begingroup\$ If you're curious, I maintain the speed by applying a force in the Velocity direction after the collision system has resolved the collision and modified the Velocity vector already, and the inaccuracy was appearing before I applied the force. \$\endgroup\$
    – Zepee
    Commented Feb 19, 2015 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .