Hey all,
I'm starting on a top-down game with Box2D as physics engine. A few months ago I had a first stab at it and I recall it worked quite well (unfortunately I can't access my old code 🙁. )
Now, however, when my dynamic bodies collide, they tend to get pushed back with a disproportionately large amount of force! Say, box A hits box B very slowly, both upon collision both will get propelled away from eachother very fast. I do not know why this is happening.
In my entity constructor, I create my physics body as such:
My main loop looks something like this:
I move my object by applying a linear impulse (this code is called every tick):
I recorded a video so you get more of an idea what it looks like: https://www.youtube.com/watch?v=rY6Zq5g ... e=youtu.be .
I thought this would probably be something more people ran into, but a quite elaborate search on google turns up with nothing. Does anyone know what I am doing wronge? I'm using SFML for drawing and keyboard input, my platform is Windows 7, 64 bit and I compile with MinGW.
I'm starting on a top-down game with Box2D as physics engine. A few months ago I had a first stab at it and I recall it worked quite well (unfortunately I can't access my old code 🙁. )
Now, however, when my dynamic bodies collide, they tend to get pushed back with a disproportionately large amount of force! Say, box A hits box B very slowly, both upon collision both will get propelled away from eachother very fast. I do not know why this is happening.
In my entity constructor, I create my physics body as such:
Code: Select all
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = start_pos;
this->physics_body = this->physics_world->CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f, 0.5f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.9f;
this->physics_body->CreateFixture(&fixtureDef);
My main loop looks something like this:
Code: Select all
accumulator = 0;
this->tick_rate = 60;
while (true) {
double dt_desired = (1.0)/(this->tick_rate);
sf::Time dt = clock.restart();
accumulator += dt.asSeconds();
while (accumulator >= dt_desired) {
this->tick(dt_desired);
this->physics_world->Step(dt_desired, 8, 3);
accumulator -= dt_desired;
}
}
I move my object by applying a linear impulse (this code is called every tick):
Code: Select all
b2Vec2 move = p->GetCurAction().move_vec;
if (move == b2Vec2_zero)
return;
float max_speed = 5.f;
float max_impulse = 3.f;
if (move.Length() > 1.f)
move.Normalize();
b2Vec2 desired_vel = b2Vec2(move.x * max_speed, move.y * max_speed);
b2Vec2 cur_vel = this->physics_body->GetLinearVelocity();
b2Vec2 diff = desired_vel - cur_vel;
if (diff.Length() > max_impulse) {
diff.Normalize();
diff *= max_impulse;
}
this->physics_body->ApplyLinearImpulse(diff, b2Vec2_zero, true);
I recorded a video so you get more of an idea what it looks like: https://www.youtube.com/watch?v=rY6Zq5g ... e=youtu.be .
I thought this would probably be something more people ran into, but a quite elaborate search on google turns up with nothing. Does anyone know what I am doing wronge? I'm using SFML for drawing and keyboard input, my platform is Windows 7, 64 bit and I compile with MinGW.