Citrus-Engine icon indicating copy to clipboard operation
Citrus-Engine copied to clipboard

unwanted 'jumps' when running on connected platforms in box2D

Open gsynuh opened this issue 12 years ago • 1 comments

Alright this is not a bug, however this is something some people (if not a lot) hate seeing, specially with the popularity of infinite runners.

the box2D hero doesn't (yet?) accept being a ball - when I set its shape as a circle, this jumping around effect goes away (apparently) So I thought I'd share my solution

Here's the related code

Shape :

_shape = Box2DShapeMaker.Circle(50/_box2D.scale, 50/_box2D.scale);

When object is a circle, it seems the collision angle is very different from when its a box... (we actually can add Math.PI when using a circle and it fixes it ) so here's a modified bit of the original hero in handleBeginContact. Sorry I'm using radians

if (contact.GetManifold().m_localPoint && !(collider is Sensor))
            {   
                var pi4:Number = Math.PI / 4;
                var collisionAngle:Number = new MathVector(contact.normal.x, contact.normal.y).angle + Math.PI;
                if (MathUtils.angleBetween(collisionAngle,pi4,3*pi4))
                {   
                    _groundContacts.push(collider.body.GetFixtureList());
                    _onGround = true;
                    updateCombinedGroundAngle();
                }
            }

pi4 and 3*pi4 is the equivalent of 45 and 135 degrees, Which are 225 and 315 mirrored on the x axis used in the original Hero (so the difference is I just removed Math.PI (to have the 'opposite' angle) from the original logic, combining the two can be written like that I guess

if (contact.GetManifold().m_localPoint && !(collider is Sensor))
            {   
                var pi4:Number = Math.PI / 4;
                var collisionAngle:Number = new MathVector(contact.normal.x, contact.normal.y).angle + (radius == 0 ? Math.PI : 0);
                if (MathUtils.angleBetween(collisionAngle,pi4,3*pi4))
                {   
                    _groundContacts.push(collider.body.GetFixtureList());
                    _onGround = true;
                    updateCombinedGroundAngle();
                }
            }

if no radius is set, then I add Math.PI to be compatible with the box hero (angles being 225/315 instead of 35/135

I know its ugly to check if the body is a ball or not like that - but even if we don't use it, that's a fair solution for people wanting to stop the hero from boucing off connected platforms

gsynuh avatar Sep 24 '13 14:09 gsynuh

If it solves the issue, I don't see any easons why we shouldn't use it ;)

The way we're using for detecting angle collision makes lots of things complex. We should think to an other/easiest way.

alamboley avatar Sep 24 '13 15:09 alamboley