I want to start the simulation, but it doesn't work\n PhysEngine.hpp
#include <box2d/box2d.h>
float ticks = 30.0f;
b2Vec2 gravity(0.0, -10.0);
b2World phys_world(gravity);
float timeStep = 1.0f / ticks;
int32 velocityIterations = 8;
int32 positionIterations = 3;
class PhysicObjectStatic {
public:
b2BodyDef bodyDef;
b2Body *body;
b2PolygonShape shape;
void init(float x, float y, float width, float height);
};
class PhysicObjectDynamic {
public:
b2BodyDef bodyDef;
b2Body* body;
b2PolygonShape dynamicBox;
b2FixtureDef fixtureDef;
void init(float x, float y, float width, float height);
};
void PhysicObjectDynamic::init(float x, float y, float width, float height) {
body = phys_world.CreateBody(&bodyDef);
dynamicBox.SetAsBox(width, height);
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x, y);
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
void PhysicObjectStatic::init(float x, float y, float width, float height) {
bodyDef.position.Set(x, y);
body = phys_world.CreateBody(&bodyDef);
shape.SetAsBox(width, height);
body->CreateFixture(&shape, 0.0f);
}
void simulate(b2Body *body) {
phys_world.Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = body->GetPosition();
float angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
main.cpp
...
PhysObjectDynamic object;
object.init(0.0f, 0.0f, 10.0f, 10.0f);
...
while (true) {
simulate(object.body);
}
...
And after launching the application, it displays the following
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
I tried to add force in function simulate and before phys_world.Step, but it didn`t work. I excepted something like this, like on box2d docs:
0.00 4.00 0.00\n 0.00 3.99 0.00\n 0.00 3.98 0.00\n ... 0.00 1.25 0.00\n 0.00 1.13 0.00\n 0.00 1.01 0.00\n