如何解决如何在 Delphi 中使用 Box2D
我使用 delphi rio 创建 Box2D
创建表单时,世界和重力创建如下
gravity := b2Vec2.Create(0.0,-10.0);
world := b2WorldWrapper.Create(gravity);
try
groundBodyDef := b2BodyDef.Create();
groundBodyDef.position.&Set(0.0,-10.0);
groundBody := world.CreateBody(@groundBodyDef);
groundBox := b2polygonShapeWrapper.Create();
groundBox.SetAsBox(50.0,10.0);
groundBody.CreateFixture(groundBox,0.0);
finally
end;
var
dynamicBox: b2polygonShapeWrapper;
fixtureDef: b2FixtureDef;
begin
bodyDef := b2BodyDef.Create();
bodyDef.&type := b2_DynamicBody;
bodyDef.position.&Set(0.0,4.0);
body := world.CreateBody(@bodyDef);
dynamicBox := b2polygonShapeWrapper.Create();
dynamicBox.SetAsBox(1.0,1.0);
fixtureDef := b2FixtureDef.Create();
fixtureDef.shape := dynamicBox;
fixtureDef.density := 1.0;
fixtureDef.friction := 1.0;
body.CreateFixture(@fixtureDef);
dynamicBox.Destroy;
groundBox.Destroy;
end;
并在单击 Button2 时圈出,RandomFloat() 创建随机浮点数“Result := 2.0 * Random() - 1.0;”
var
circle: b2CircleShapeWrapper;
fd: b2FixtureDef;
bd: b2BodyDef;
shape: b2CircleShapeWrapper;
p: b2Vec2;
radius: Float32;
begin
radius := 2.0;
shape := b2CircleShapeWrapper.Create;
shape.m_p := b2vec2.Create(0.0,0.0);
shape.m_radius := radius;
fd := b2FixtureDef.Create;
fd.shape := shape;
fd.density := 1.0;
fd.friction := 0.0;
p := b2Vec2.Create(RandomFloat(),3.0 + RandomFloat());
bd := b2BodyDef.Create;
bd.&type := b2_dynamicBody;
bd.position := p;
//bd.allowSleep := false;
body := world.CreateBody(@bd);
body.CreateFixture(@fd);
end;
procedure TfrmMain.PaintBoxPaint(Sender: TObject; Canvas: TCanvas);
var
rect: TRectF;
begin
Canvas.BeginScene;
try
Canvas.Fill.Color := b2Color2TColor(b2Color.Create(0.3,0.3,1));
rect := PaintBox.BoundsRect;
Canvas.FillRect(rect,[],DEFAULT_OPACITY);
g_debugDraw.Canvas := Canvas;
g_debugDraw.Canvas.Fill.Color := TAlphaColorRec.Yellow;
g_debugDraw.Canvas.Fill.Color := TAlphaColorRec.Aqua;
finally
Canvas.EndScene;
end;
end;
我搜索了互联网,但没有找到有关如何在 Delphi 中使用 Box2d 的信息。 有几个例子,但它们很复杂,没有解释,在他们的帮助下,不可能学习使用的基础知识。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。