URestitution.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit URestitution;
  2. interface
  3. {$I ......SourcePhysics2D.inc}
  4. uses
  5.    Windows, UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
  6. type
  7.    TRestitution = class(TTester)
  8.    private
  9.       k_restitution: Float;
  10.    public
  11.       body: Tb2Body;
  12.       constructor Create; override;
  13.       procedure Step(var settings: TSettings; timeStep: Float); override;
  14.       procedure Keyboard(key: Byte); override;
  15.    end;
  16. implementation
  17. { TCCD }
  18. constructor TRestitution.Create;
  19. var
  20.    bd: Tb2BodyDef;
  21.    sd, sd_bottom, sd_left, sd_right: Tb2PolygonDef;
  22. begin
  23.    inherited;
  24.    k_restitution := 1.0;
  25.    begin
  26.       bd := Tb2BodyDef.Create;
  27.       {$IFDEF OP_OVERLOAD}
  28.       bd.position.SetValue(0.0, 20.0);
  29.       {$ELSE}
  30.       SetValue(bd.position, 0.0, 20.0);
  31.       {$ENDIF}
  32.       body := m_world.CreateBody(bd);
  33.       sd := Tb2PolygonDef.Create;
  34.       sd.density := 0.0;
  35.       sd.restitution := k_restitution;
  36.       sd.SetAsBox(0.1, 10.0, MakeVector(-10.0, 0.0), 0.0);
  37.       body.CreateShape(sd, False);
  38.       sd.SetAsBox(0.1, 10.0, MakeVector(10.0, 0.0), 0.0);
  39.       body.CreateShape(sd, False);
  40.       sd.SetAsBox(0.1, 10.0, MakeVector(0.0, -10.0), 0.5 * Pi);
  41.       body.CreateShape(sd, False);
  42.       sd.SetAsBox(0.1, 10.0, MakeVector(0.0, 10.0), -0.5 * Pi);
  43.       body.CreateShape(sd);
  44.    end;
  45.    begin
  46.       sd_bottom := Tb2PolygonDef.Create;
  47.       sd_bottom.SetAsBox(1.5, 0.15);
  48.       sd_bottom.density := 4.0;
  49.       sd_left := Tb2PolygonDef.Create;
  50.       sd_left.SetAsBox(0.15, 2.7, MakeVector(-1.45, 2.35), 0.2);
  51.       sd_left.density := 4.0;
  52.       sd_right := Tb2PolygonDef.Create;
  53.       sd_right.SetAsBox(0.15, 2.7, MakeVector(1.45, 2.35), -0.2);
  54.       sd_right.density := 4.0;
  55.       bd := Tb2BodyDef.Create;
  56.       {$IFDEF OP_OVERLOAD}
  57.       bd.position.SetValue(0.0, 15.0);
  58.       {$ELSE}
  59.       SetValue(bd.position, 0.0, 15.0);
  60.       {$ENDIF}
  61.       body := m_world.CreateBody(bd);
  62.       body.CreateShape(sd_bottom);
  63.       body.CreateShape(sd_left);
  64.       body.CreateShape(sd_right);
  65.       body.SetMassFromShapes;
  66.    end;
  67. end;
  68. procedure TRestitution.Step(var settings: TSettings; timeStep: Float);
  69. begin
  70.    inherited;
  71.    DrawText(Format('Use +/- to control restitution. Present %f', [k_restitution]));
  72. end;
  73. procedure TRestitution.Keyboard(key: Byte);
  74. var
  75.    s: Tb2Shape;
  76. begin
  77.    case key of
  78.       187{+}:
  79.          if k_restitution < 2.0 then
  80.             k_restitution := k_restitution + 0.1;
  81.       189{-}:
  82.          if k_restitution > 0.0 then
  83.             k_restitution := k_restitution - 0.1;
  84.    else
  85.       Exit;
  86.    end;
  87.    s := body.GetShapeList;
  88.    while Assigned(s) do
  89.    begin
  90.       s.m_restitution := k_restitution;
  91.       s := s.m_next;
  92.    end;
  93. end;
  94. initialization
  95.    RegisterTestEntry('Restitution Test', TRestitution);
  96. end.