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

Delphi/CppBuilder

开发平台:

Delphi

  1. unit USensorTest;
  2. interface
  3. {$I ......SourcePhysics2D.inc}
  4. uses
  5.    UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
  6. type
  7.    TSensorTest = class(TTester)
  8.    public
  9.       m_sensor: Tb2Shape;
  10.       constructor Create; override;
  11.       procedure Step(var settings: TSettings; timeStep: Float); override;
  12.    end;
  13. implementation
  14. { TSensorTest }
  15. constructor TSensorTest.Create;
  16. var
  17.    i: Integer;
  18.    bd: Tb2BodyDef;
  19.    sd: Tb2PolygonDef;
  20.    cd: Tb2CircleDef;
  21.    ground, body: Tb2Body;
  22. begin
  23.    inherited;
  24.    begin
  25.       bd := Tb2BodyDef.Create;
  26.       {$IFDEF OP_OVERLOAD}
  27.       bd.position.SetValue(0.0, -10.0);
  28.       {$ELSE}
  29.       SetValue(bd.position, 0.0, -10.0);
  30.       {$ENDIF}
  31.       ground := m_world.CreateBody(bd);
  32.       sd := Tb2PolygonDef.Create;
  33.       sd.SetAsBox(50.0, 10.0);
  34.       ground.CreateShape(sd);
  35.       cd := Tb2CircleDef.Create;
  36.       cd.isSensor := True;
  37.       cd.radius := 5.0;
  38.       {$IFDEF OP_OVERLOAD}
  39.       cd.localPosition.SetValue(0.0, 20.0);
  40.       {$ELSE}
  41.       SetValue(cd.localPosition, 0.0, 20.0);
  42.       {$ENDIF}
  43.       m_sensor := ground.CreateShape(cd);
  44.    end;
  45.    begin
  46.       cd := Tb2CircleDef.Create;
  47.       cd.radius := 1.0;
  48.       cd.density := 1.0;
  49.       bd := Tb2BodyDef.Create;
  50.       for i := 0 to 6 do
  51.       begin
  52.          {$IFDEF OP_OVERLOAD}
  53.          bd.position.SetValue(-10.0 + 3.0 * i, 20.0);
  54.          {$ELSE}
  55.          SetValue(bd.position, -10.0 + 3.0 * i, 20.0);
  56.          {$ENDIF}
  57.          body := m_world.CreateBody(bd, False);
  58.          body.CreateShape(cd, False);
  59.          body.SetMassFromShapes;
  60.       end;
  61.       cd.Free;
  62.       bd.Free;
  63.    end;
  64. end;
  65. procedure TSensorTest.Step(var settings: TSettings; timeStep: Float);
  66. var
  67.    i: Integer;
  68.    other: Tb2Body;
  69.    center, d: TVector2;
  70. begin
  71.    inherited;
  72.    // Traverse the contact results. Apply a force on shapes that overlap the sensor.
  73.    for i := 0 to m_pointCount - 1 do
  74.       with m_points[i] do
  75.       begin
  76.          if state = e_contactRemoved then
  77.             Continue;
  78.          if shape1 = m_sensor then
  79.             other := shape2.GetBody
  80.          else if shape2 = m_sensor then
  81.             other := shape1.GetBody
  82.          else
  83.             Continue;
  84.          center := m_sensor.GetBody.GetWorldPoint(Tb2CircleShape(m_sensor).m_localPosition);
  85.          {$IFDEF OP_OVERLOAD}
  86.          d := center - position;
  87.          if d.SqrLength < FLT_EPSILON * FLT_EPSILON then
  88.             Continue;
  89.          d.Normalize;
  90.          other.ApplyForce(100.0 * d, position);
  91.          {$ELSE}
  92.          d := Subtract(center, position);
  93.          if SqrLength(d) < FLT_EPSILON * FLT_EPSILON then
  94.             Continue;
  95.          Normalize(d);
  96.          other.ApplyForce(Multiply(d, 100.0), position);
  97.          {$ENDIF}
  98.       end;
  99. end;
  100. initialization
  101.    RegisterTestEntry('Sensor Test', TSensorTest);
  102. end.