URestitution.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
- unit URestitution;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- Windows, UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TRestitution = class(TTester)
- private
- k_restitution: Float;
- public
- body: Tb2Body;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TCCD }
- constructor TRestitution.Create;
- var
- bd: Tb2BodyDef;
- sd, sd_bottom, sd_left, sd_right: Tb2PolygonDef;
- begin
- inherited;
- k_restitution := 1.0;
- begin
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 20.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 20.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- sd := Tb2PolygonDef.Create;
- sd.density := 0.0;
- sd.restitution := k_restitution;
- sd.SetAsBox(0.1, 10.0, MakeVector(-10.0, 0.0), 0.0);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(10.0, 0.0), 0.0);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(0.0, -10.0), 0.5 * Pi);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(0.0, 10.0), -0.5 * Pi);
- body.CreateShape(sd);
- end;
- begin
- sd_bottom := Tb2PolygonDef.Create;
- sd_bottom.SetAsBox(1.5, 0.15);
- sd_bottom.density := 4.0;
- sd_left := Tb2PolygonDef.Create;
- sd_left.SetAsBox(0.15, 2.7, MakeVector(-1.45, 2.35), 0.2);
- sd_left.density := 4.0;
- sd_right := Tb2PolygonDef.Create;
- sd_right.SetAsBox(0.15, 2.7, MakeVector(1.45, 2.35), -0.2);
- sd_right.density := 4.0;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 15.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 15.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- body.CreateShape(sd_bottom);
- body.CreateShape(sd_left);
- body.CreateShape(sd_right);
- body.SetMassFromShapes;
- end;
- end;
- procedure TRestitution.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText(Format('Use +/- to control restitution. Present %f', [k_restitution]));
- end;
- procedure TRestitution.Keyboard(key: Byte);
- var
- s: Tb2Shape;
- begin
- case key of
- 187{+}:
- if k_restitution < 2.0 then
- k_restitution := k_restitution + 0.1;
- 189{-}:
- if k_restitution > 0.0 then
- k_restitution := k_restitution - 0.1;
- else
- Exit;
- end;
- s := body.GetShapeList;
- while Assigned(s) do
- begin
- s.m_restitution := k_restitution;
- s := s.m_next;
- end;
- end;
- initialization
- RegisterTestEntry('Restitution Test', TRestitution);
- end.