1
\$\begingroup\$

I need to mimic jumping behavior for the player in my game. Player consists of two fixtures with circle and rectangle shapes. Rectangle I use to detect ground and it is a sensor. Is some point for jumping I do this:

float impulseY = body->GetMass() * PLAYER_JUMPING_VEOCITY / PTM_RATIO * std::sin(PLAYER_JUMPING_ANGLE * PI / 180);
body->ApplyLinearImpulse(b2Vec2(0, impulseY), body->GetWorldCenter(), true);

and player does not jump. But when I do this:

body->SetLinearVelocity(b2Vec2(0, PLAYER_JUMPING_VEOCITY / PTM_RATIO * std::sin(PLAYER_JUMPING_ANGLE * PI / 180)));

my player jumps. Also when I change the rectangle shape to be normal (not sensor) shape, its works again. Why? Just in case here are the parameters of my rectangular sensor:

b2PolygonShape boxShape;
boxShape.SetAsBox(width * 0.5/2/PTM_RATIO, height * 0.2/2/PTM_RATIO, b2Vec2(0, -height * 0.4 /PTM_RATIO), 0);

b2FixtureDef boxFixtureDef;
boxFixtureDef.friction = 0;
boxFixtureDef.restitution = 0;
boxFixtureDef.density = 1;
boxFixtureDef.isSensor = true;
boxFixtureDef.userData = static_cast<void*>(PLAYER_GROUP);
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I think I got the reason. When I detect jumping time with sensor rectangle, the body at that time sill has velocity towards down. And when I give him impulse towards up direction it decreases its speed towards down (as it turns the impulse was small) and I think that it does not jump. But in case I apply linear velocity, it just starts to move up the the given velocity. Also in case I change the rectangle to be not sensor, I think it collides with ground and its Y velocity becomes 0. Hence is this case when I apply linear impulse it jumps up. Makes sense? :)

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

Did you try applying an hardcoded (big) impulse instead ? Or check that the mass is not 0 for example (maybe you need to call some recalculate() function to set the mass from density, for sensors).

\$\endgroup\$

You must log in to answer this question.

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