UDistanceTest.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:3k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UDistanceTest;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils, OpenGL;
- type
- TDistanceTest = class(TTester)
- public
- m_body1, m_body2: Tb2Body;
- m_shape1, m_shape2: Tb2Shape;
- constructor Create; override;
- destructor Destroy; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TDistanceTest }
- constructor TDistanceTest.Create;
- var
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- begin
- inherited;
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(1.0, 1.0);
- sd.density := 0.0;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 10.0);
- {$ENDIF}
- m_body1 := m_world.CreateBody(bd);
- m_shape1 := m_body1.CreateShape(sd);
- end;
- begin
- sd := Tb2PolygonDef.Create;
- sd.vertexCount := 3;
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(-1.0, 0.0);
- sd.vertices[1].SetValue(1.0, 0.0);
- sd.vertices[2].SetValue(0.0, 15.0);
- {$ELSE}
- SetValue(sd.vertices[0], -1.0, 0.0);
- SetValue(sd.vertices[1], 1.0, 0.0);
- SetValue(sd.vertices[2], 0.0, 15.0);
- {$ENDIF}
- sd.density := 1.0;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 10.0);
- {$ENDIF}
- m_body2 := m_world.CreateBody(bd);
- m_shape2 := m_body2.CreateShape(sd);
- m_body2.SetMassFromShapes;
- end;
- m_world.SetGravity(MakeVector(0.0, 0.0));
- m_world.m_positionCorrection := False;
- end;
- destructor TDistanceTest.Destroy;
- begin
- m_world.m_positionCorrection := True;
- inherited;
- end;
- procedure TDistanceTest.Step(var settings: TSettings; timeStep: Float);
- var
- x1, x2: TVector2;
- distance: Float;
- begin
- settings.pause := True;
- inherited;
- settings.pause := False;
- distance := b2Distance(x1, x2, m_shape1, m_shape2, m_body1.m_xf, m_body2.m_xf);
- DrawText('Use WASD/QE to move object.');
- DrawText(Format('distance = %g', [distance]));
- DrawText(Format('iterations = %d', [g_GJK_Iterations]));
- glPointSize(4.0);
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POINTS);
- glVertex2f(x1.x, x1.y);
- glVertex2f(x2.x, x2.y);
- glEnd;
- glPointSize(1.0);
- glColor3f(1.0, 1.0, 0.0);
- glBegin(GL_LINES);
- glVertex2f(x1.x, x1.y);
- glVertex2f(x2.x, x2.y);
- glEnd;
- end;
- procedure TDistanceTest.Keyboard(key: Byte);
- var
- p: TVector2;
- a: Float;
- begin
- p := m_body2.GetPosition;
- a := m_body2.GetAngle;
- case key of
- 65{A}: p.x := p.x - 0.1;
- 68{D}: p.x := p.x + 0.1;
- 83{S}: p.y := p.y - 0.1;
- 87{W}: p.y := p.y + 0.1;
- 81{Q}: a := a + 0.1 * Pi;
- 69{E}: a := a - 0.1 * Pi;
- end;
- m_body2.SetXForm(p, a);
- end;
- initialization
- RegisterTestEntry('Distance Test', TDistanceTest);
- end.