uses
System.SysUtils, Box2D.Collision, Box2D.Common,
Box2D.Dynamics,
Box2D.Rope, Box2DTypes;
var
gravity: b2Vec2;
world: b2WorldWrapper;
groundBodyDef: b2BodyDef;
groundBody: b2BodyWrapper;
groundBox: b2PolygonShapeWrapper;
bodyDef: b2BodyDef;
body: b2BodyWrapper;
dynamicBox: b2PolygonShapeWrapper;
fixtureDef: b2FixtureDef;
timeStep, angle: Single;
velocityIterations, positionIterations, i: Integer;
position: b2Vec2;
begin
try
// Define the gravity vector.
gravity := b2Vec2.Create(0.0, -10.0);
// Construct a world object, which will hold and simulate the rigid bodies.
world := b2WorldWrapper.Create(gravity);
// Define the ground body.
groundBodyDef := b2BodyDef.Create;
groundBodyDef.position.&Set(0.0, -10.0);
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
groundBody := world.CreateBody(@groundBodyDef);
// Define the ground box shape.
groundBox := b2PolygonShapeWrapper.Create;
// The extents are the half-widths of the box.
groundBox.SetAsBox(50.0, 10.0);
// Add the ground fixture to the ground body.
groundBody.CreateFixture(groundBox, 0.0);
// Define the dynamic body. We set its position and call the body factory.
bodyDef := b2BodyDef.Create;
bodyDef.&type := b2_dynamicBody;
bodyDef.position.&Set(0.0, 4.0);
body := world.CreateBody(@bodyDef);
// Define another box shape for our dynamic body.
dynamicBox := b2PolygonShapeWrapper.Create;
dynamicBox.SetAsBox(1.0, 1.0);
// Define the dynamic body fixture.
fixtureDef := b2FixtureDef.Create;
fixtureDef.shape := dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density := 1.0;
// Override the default friction.
fixtureDef.friction := 0.3;
// Add the shape to the body.
body.CreateFixture(@fixtureDef);
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
timeStep := 1.0 / 60.0;
velocityIterations := 6;
positionIterations := 2;
// This is our little game loop.
for I := 0 to 59 do
begin
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.Step(timeStep, velocityIterations, positionIterations);
// Now print the position and angle of the body.
position := body.GetPosition^;
angle := body.GetAngle;
WriteLn(Format('%4.2f %4.2f %4.2f', [position.x, position.y, angle]));
end;
// When the world destructor is called, all bodies and joints are freed.
// This can create orphaned pointers, so be careful about your world
// management.
world.Destroy;
ReadLn;
Jumat, 30 April 2021
Cara menggunakan box2D pada delphi console
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar