UApplyForce.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
- unit UApplyForce;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TApplyForce = class(TTester)
- public
- m_body: Tb2Body;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TApplyForce }
- constructor TApplyForce.Create;
- const
- k_restitution = 0.4;
- var
- bd: Tb2BodyDef;
- ground: Tb2Body;
- sd, sd1, sd2: Tb2PolygonDef;
- xf1, xf2: Tb2XForm;
- begin
- inherited;
- m_world.SetGravity(MakeVector(0.0, 0.0));
- frmMain.editGravityX.Text := '0';
- frmMain.editGravityY.Text := '0';
- begin
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 20.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 20.0);
- {$ENDIF}
- ground := m_world.CreateBody(bd);
- sd := Tb2PolygonDef.Create;
- sd.density := 0.0;
- sd.restitution := k_restitution;
- sd.SetAsBox(0.2, 20.0, MakeVector(-20.0, 0.0), 0.0);
- ground.CreateShape(sd, False);
- sd.SetAsBox(0.2, 20.0, MakeVector(20.0, 0.0), 0.0);
- ground.CreateShape(sd, False);
- sd.SetAsBox(0.2, 20.0, MakeVector(0.0, -20.0), 0.5 * Pi);
- ground.CreateShape(sd, False);
- sd.SetAsBox(0.2, 20.0, MakeVector(0.0, 20.0), -0.5 * Pi);
- ground.CreateShape(sd);
- end;
- begin
- {$IFDEF OP_OVERLOAD}
- xf1.R.SetValue(0.3524 * Pi);
- {$ELSE}
- SetValue(xf1.R, 0.3524 * Pi);
- {$ENDIF}
- xf1.position := b2Mul(xf1.R, MakeVector(1.0, 0.0));
- sd1 := Tb2PolygonDef.Create;
- sd1.vertexCount := 3;
- sd1.vertices[0] := b2Mul(xf1, MakeVector(-1.0, 0.0));
- sd1.vertices[1] := b2Mul(xf1, MakeVector(1.0, 0.0));
- sd1.vertices[2] := b2Mul(xf1, MakeVector(0.0, 0.5));
- sd1.density := 2.0;
- {$IFDEF OP_OVERLOAD}
- xf2.R.SetValue(-0.3524 * Pi);
- {$ELSE}
- SetValue(xf2.R, -0.3524 * Pi);
- {$ENDIF}
- xf2.position := b2Mul(xf2.R, MakeVector(-1.0, 0.0));
- sd2 := Tb2PolygonDef.Create;
- sd2.vertexCount := 3;
- sd2.vertices[0] := b2Mul(xf2, MakeVector(-1.0, 0.0));
- sd2.vertices[1] := b2Mul(xf2, MakeVector(1.0, 0.0));
- sd2.vertices[2] := b2Mul(xf2, MakeVector(0.0, 0.5));
- sd2.density := 2.0;
- bd := Tb2BodyDef.Create;
- bd.angularDamping := 2.0;
- bd.linearDamping := 0.1;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 1.05);
- {$ELSE}
- SetValue(bd.position, 0.0, 1.05);
- {$ENDIF}
- bd.angle := Pi;
- m_body := m_world.CreateBody(bd);
- m_body.CreateShape(sd1);
- m_body.CreateShape(sd2);
- m_body.SetMassFromShapes;
- end;
- end;
- procedure TApplyForce.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Use WAD to control the plane.');
- end;
- procedure TApplyForce.Keyboard(key: Byte);
- var
- f, p: TVector2;
- begin
- case key of
- 87{W}:
- begin
- f := m_body.GetWorldVector(MakeVector(0.0, -70.0));
- p := m_body.GetWorldPoint(MakeVector(0.0, 2.0));
- m_body.ApplyForce(f, p);
- end;
- 65{A}: m_body.ApplyTorque(20.0);
- 68{D}: m_body.ApplyTorque(-20.0);
- end;
- end;
- initialization
- RegisterTestEntry('Apply Force', TApplyForce);
- end.