UPhysicsDebug.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UPhysicsDebug;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, UPhysics2D, UPhysics2DTypes, StdCtrls, ExtCtrls;
- type
- TfrmDebug = class(TForm)
- ListBox1: TListBox;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- frmDebug: TfrmDebug;
- implementation
- {$R *.dfm}
- var
- world: Tb2World;
- groundBody: Tb2Body;
- procedure TfrmDebug.FormCreate(Sender: TObject);
- var
- worldAABB: Tb2AABB;
- gravity: TVector2;
- groundDef, bodyDef: Tb2BodyDef;
- groundShapeDef, shapeDef: Tb2PolygonDef;
- body: Tb2Body;
- timeStep: Float;
- iterations: Int32;
- i: Integer;
- position: TVector2;
- angle: Float;
- begin
- //ReportMemoryLeaksOnShutdown := True;
- SetValue(worldAABB.lowerBound, -100, -100);
- SetValue(worldAABB.upperBound, 100, 100);
- //worldAABB.lowerBound.SetValue(0 ,0);
- //worldAABB.upperBound.SetValue(img.Width, img.Height);
- SetValue(gravity, 0, -10);
- world := Tb2World.Create(worldAABB, gravity, True);
- groundDef := Tb2BodyDef.Create;
- SetValue(groundDef.position, 0, -10);
- groundBody := world.CreateBody(groundDef);
- groundShapeDef := Tb2PolygonDef.Create;
- groundShapeDef.SetAsBox(50.0, 10.0);
- // Add the ground shape to the ground body.
- groundBody.CreateShape(groundShapeDef);
- // Define the dynamic body. We set its position and call the body factory.
- bodyDef := Tb2BodyDef.Create;
- SetValue(bodyDef.position, 0.0, 4.0);
- body := world.CreateBody(bodyDef);
- // Define another box shape for our dynamic body.
- shapeDef := Tb2PolygonDef.Create;
- shapeDef.SetAsBox(1.0, 1.0);
- // Set the box density to be non-zero, so it will be dynamic.
- shapeDef.density := 1.0;
- // Override the default friction.
- shapeDef.friction := 0.3;
- // Add the shape to the body.
- body.CreateShape(shapeDef);
- // Now tell the dynamic body to compute it's mass properties base
- // on its shape.
- body.SetMassFromShapes();
- // 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;
- iterations := 10;
- ListBox1.Items.BeginUpdate;
- for i := 0 to 59 do
- begin
- {$IFDEF DEBUG_LOG}
- DEBUG.Add('i ' + IntToStr(i));
- {$ENDIF}
- // 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, iterations);
- // Now print the position and angle of the body.
- position := body.GetPosition;
- angle := body.GetAngle;
- ListBox1.Items.Add(Format('%d x: %f y: %f a: %f', [i, position.x, position.y, angle]));
- end;
- ListBox1.Items.EndUpdate;
- world.Free;
- {$IFDEF DEBUG_LOG}
- DEBUG.SaveToFile('Log.txt');
- {$ENDIF}
- end;
- end.