UTimeOfImpact.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:6k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UTimeOfImpact;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TTimeOfImpact = class(TTester)
- public
- m_body1, m_body2: Tb2body;
- m_shape1: Tb2Shape;
- m_shape2: Tb2PolygonShape;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- end;
- TTimeOfImpact2 = class(TTester)
- public
- down, up: Tb2Body;
- bullets: array[0..2] of Tb2Body;
- bullet2: Tb2Body;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- end;
- implementation
- { TTimeOfImpact }
- constructor TTimeOfImpact.Create;
- var
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- begin
- inherited;
- sd := Tb2PolygonDef.Create;
- sd.density := 0.0;
- sd.SetAsBox(0.1, 10.0, MakeVector(10.0, 0.0), 0.0);
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 20.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 20.0);
- {$ENDIF}
- bd.angle := 0.0;
- m_body1 := m_world.CreateBody(bd);
- m_shape1 := m_body1.CreateShape(sd);
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(0.25, 0.25);
- sd.density := 1.0;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(9.6363468, 28.050615);
- {$ELSE}
- SetValue(bd.position, 9.6363468, 28.050615);
- {$ENDIF}
- bd.angle := 1.6408679;
- m_body2 := m_world.CreateBody(bd);
- m_shape2 := Tb2PolygonShape(m_body2.CreateShape(sd));
- m_body2.SetMassFromShapes;
- end;
- procedure TTimeOfImpact.Step(var settings: TSettings; timeStep: Float);
- const
- clVertices: RGBA = (0.5, 0.7, 0.9, 1.0);
- clCoreVertices: RGBA = (0.5, 0.9, 0.7, 1.0);
- var
- i: Integer;
- sweep1, sweep2: Tb2Sweep;
- toi: Float;
- xf2: Tb2XForm;
- vertices: Tb2PolyVertices;
- begin
- settings.pause := True;
- inherited;
- settings.pause := False;
- {$IFDEF OP_OVERLOAD}
- sweep1.c0.SetValue(0.0, 20.0);
- {$ELSE}
- SetValue(sweep1.c0, 0.0, 20.0);
- {$ENDIF}
- sweep1.a0 := 0.0;
- sweep1.c := sweep1.c0;
- sweep1.a := sweep1.a0;
- sweep1.t0 := 0.0;
- sweep1.localCenter := m_body1.GetLocalCenter;
- {$IFDEF OP_OVERLOAD}
- sweep2.c0.SetValue(9.6363468, 28.050615);
- sweep2.c := sweep2.c0 + MakeVector(-0.075121880, 0.27358246);
- {$ELSE}
- SetValue(sweep2.c0, 9.6363468, 28.050615);
- sweep2.c := Add(sweep2.c0, MakeVector(-0.075121880, 0.27358246));
- {$ENDIF}
- sweep2.a0 := 1.6408679;
- sweep2.a := sweep2.a0 - 10.434675;
- sweep2.t0 := 0.0;
- sweep2.localCenter := m_body2.GetLocalCenter;
- toi := b2TimeOfImpact(m_shape1, m_shape2, sweep1, sweep2);
- DrawText(Format('toi := %g', [toi]));
- {$IFDEF OP_OVERLOAD}
- sweep2.GetXForm(xf2, toi);
- {$ELSE}
- GetXForm(sweep2, xf2, toi);
- {$ENDIF}
- for i := 0 to m_shape2.GetVertexCount - 1 do
- vertices[i] := b2Mul(xf2, m_shape2.m_vertices[i]);
- m_debugDrawer.DrawPolygon(vertices, m_shape2.GetVertexCount, clVertices);
- for i := 0 to m_shape2.GetVertexCount - 1 do
- vertices[i] := b2Mul(xf2, m_shape2.m_coreVertices[i]);
- m_debugDrawer.DrawPolygon(vertices, m_shape2.GetVertexCount, clCoreVertices);
- end;
- { TTimeOfImpact2 }
- constructor TTimeOfImpact2.Create;
- var
- i: Integer;
- pd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- cd: Tb2CircleDef;
- begin
- inherited;
- pd := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- pd.SetAsBox(10, 0.1, MakeVector(-10, -5), 0);
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-10, -5);
- {$ELSE}
- SetValue(bd.position, -10, -5);
- {$ENDIF}
- down := m_world.CreateBody(bd, False);
- down.CreateShape(pd, False);
- pd.SetAsBox(10, 0.1, MakeVector(-10, 0), 0);
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-10, 0);
- {$ELSE}
- SetValue(bd.position, -10, 0);
- {$ENDIF}
- up := m_world.CreateBody(bd);
- up.CreateShape(pd);
- bd := Tb2BodyDef.Create;
- bd.isBullet := True;
- cd := Tb2CircleDef.Create;
- cd.radius := 0.25;
- cd.density := 15.0;
- cd.restitution := 1.5;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-20, -3.0);
- {$ELSE}
- SetValue(bd.position, -20, -3.0);
- {$ENDIF}
- for i := 0 to 2 do
- begin
- bullets[i] := m_world.CreateBody(bd, False);
- bullets[i].SetLinearVelocity(MakeVector(0.0, 0.0));
- bullets[i].CreateShape(cd, False);
- bullets[i].SetMassFromShapes;
- end;
- bd.Free;
- cd.Free;
- //////////////////////////////////////
- pd := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- pd.SetAsBox(10, 5, MakeVector(10, -5), 0);
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(10, -5);
- {$ELSE}
- SetValue(bd.position, 10, -5);
- {$ENDIF}
- down := m_world.CreateBody(bd);
- down.CreateShape(pd, False);
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(20, 20);
- {$ELSE}
- SetValue(bd.position, 20, 20);
- {$ENDIF}
- bd.isBullet := True;
- cd := Tb2CircleDef.Create;
- cd.radius := 0.25;
- cd.density := 15.0;
- bullet2 := m_world.CreateBody(bd);
- bullet2.SetLinearVelocity(MakeVector(0.0, -500.0));
- bullet2.CreateShape(cd);
- bullet2.SetMassFromShapes;
- end;
- procedure TTimeOfImpact2.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Switch Time of Impact and Position Correction options and reset the scene.');
- if m_world.m_continuousPhysics then
- DrawText('TOI ON')
- else
- DrawText('TOI OFF');
- if m_world.m_positionCorrection then
- DrawText('Position Correction ON')
- else
- DrawText('Position Correction OFF');
- end;
- initialization
- RegisterTestEntry('Time of Impact', TTimeOfImpact);
- RegisterTestEntry('Time of Impact2', TTimeOfImpact2);
- end.