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

Delphi/CppBuilder

开发平台:

Delphi

  1. unit UShapeEditing;
  2. interface
  3. {$I ......SourcePhysics2D.inc}
  4. uses
  5.    UMain,
  6.    UPhysics2DTypes,
  7.    UPhysics2D,
  8.    SysUtils,
  9.    OpenGL;
  10. type
  11.    TShapeEditing = class(TTester)
  12.    public
  13.       m_body: Tb2Body;
  14.       m_shape1, m_shape2: Tb2Shape;
  15.       constructor Create; override;
  16.       procedure Step(var settings: TSettings; timeStep: Float); override;
  17.       procedure Keyboard(key: Byte); override;
  18.    end;
  19. implementation
  20. { TDistanceTest }
  21. constructor TShapeEditing.Create;
  22. var
  23.    sd: Tb2PolygonDef;
  24.    bd, bodydef: Tb2BodyDef;
  25.    ground: Tb2Body;
  26. begin
  27.    inherited;
  28.    sd := Tb2PolygonDef.Create;
  29.    sd.SetAsBox(50.0, 10.0);
  30.    bd := Tb2BodyDef.Create;
  31.    {$IFDEF OP_OVERLOAD}
  32.    bd.position.SetValue(0.0, -10.0);
  33.    {$ELSE}
  34.    SetValue(bd.position, 0.0, -10.0);
  35.    {$ENDIF}
  36.    ground := m_world.CreateBody(bd);
  37.    ground.CreateShape(sd);
  38.    bodydef := Tb2BodyDef.Create;
  39.    {$IFDEF OP_OVERLOAD}
  40.    bodydef.position.SetValue(0.0, 10.0);
  41.    {$ELSE}
  42.    SetValue(bodydef.position, 0.0, 10.0);
  43.    {$ENDIF}
  44.    m_body := m_world.CreateBody(bodydef);
  45.    sd := Tb2PolygonDef.Create;
  46.    sd.SetAsBox(4.0, 4.0, MakeVector(0.0, 0.0), 0.0);
  47.    sd.density := 10.0;
  48.    m_shape1 := m_body.CreateShape(sd);
  49.    m_body.SetMassFromShapes;
  50. end;
  51. procedure TShapeEditing.Step(var settings: TSettings; timeStep: Float);
  52. begin
  53.    inherited;
  54.  DrawText('Press C create a shape, D destroy a shape.');
  55. end;
  56. procedure TShapeEditing.Keyboard(key: Byte);
  57. var
  58.    sd: Tb2CircleDef;
  59. begin
  60.    inherited;
  61.    case key of
  62.       67{C}:
  63.          if not Assigned(m_shape2) then
  64.          begin
  65.             sd := Tb2CircleDef.Create;
  66.             sd.radius := 3.0;
  67.             sd.density := 10.0;
  68.             {$IFDEF OP_OVERLOAD}
  69.             sd.localPosition.SetValue(0.5, -4.0);
  70.             {$ELSE}
  71.             SetValue(sd.localPosition, 0.5, -4.0);
  72.             {$ENDIF}
  73.             m_shape2 := m_body.CreateShape(sd);
  74.             m_body.SetMassFromShapes;
  75.             m_body.WakeUp;
  76.          end;
  77.       68{D}:
  78.          if Assigned(m_shape2) then
  79.          begin
  80.             m_body.DestroyShape(m_shape2);
  81.             m_shape2 := nil;
  82.             m_body.SetMassFromShapes;
  83.             m_body.WakeUp;
  84.          end;
  85.    end;
  86. end;
  87. initialization
  88.    RegisterTestEntry('Shape Editing', TShapeEditing);
  89. end.