UVerticalStack.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UVerticalStack;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TVerticalStack = class(TTester)
- public
- m_bullet: Tb2Body;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TVerticalStack }
- constructor TVerticalStack.Create;
- const
- xs: array[0..4] of Float = (0.0, -10.0, -5.0, 5.0, 10.0);
- var
- i, j: Integer;
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- ground: Tb2Body;
- body: Tb2Body;
- begin
- inherited;
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0, MakeVector(0.0, -10.0), 0.0);
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 0.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 0.0);
- {$ENDIF}
- ground := m_world.CreateBody(bd);
- ground.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(20.0, 10.0), 0.0);
- ground.CreateShape(sd);
- end;
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(0.5, 0.5);
- sd.density := 1.0;
- sd.friction := 0.3;
- bd := Tb2BodyDef.Create;
- // For this test we are using continuous physics for all boxes.
- // This is a stress test, you normally wouldn't do this for
- // performance reasons.
- //bd.isBullet = true;
- bd.allowSleep := True;
- for j := 0 to 4 do
- begin
- for i := 0 to 11 do
- begin
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(xs[j], 0.752 + 1.54 * i);
- {$ELSE}
- SetValue(bd.position, xs[j], 0.752 + 1.54 * i);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- end;
- end;
- bd.Free;
- sd.Free;
- end;
- procedure TVerticalStack.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Press: B to launch a bullet.');
- end;
- procedure TVerticalStack.Keyboard(key: Byte);
- var
- sd: Tb2CircleDef;
- bd: Tb2BodyDef;
- begin
- if key = 66{B} then
- begin
- if Assigned(m_bullet) then
- begin
- m_world.DestroyBody(m_bullet);
- m_bullet := nil;
- end;
- sd := Tb2CircleDef.Create;
- sd.density := 20.0;
- sd.radius := 0.25;
- sd.restitution := 0.05;
- bd := Tb2BodyDef.Create;
- bd.isBullet := True;
- bd.allowSleep := False;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-31.0, 5.0);
- {$ELSE}
- SetValue(bd.position, -31.0, 5.0);
- {$ENDIF}
- m_bullet := m_world.CreateBody(bd);
- m_bullet.CreateShape(sd);
- m_bullet.SetMassFromShapes;
- m_bullet.SetLinearVelocity(MakeVector(400.0, 0.0));
- end;
- end;
- initialization
- RegisterTestEntry('Vertical Stack', TVerticalStack);
- end.