UWeb.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:8k
- unit UWeb;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TWeb = class(TTester)
- public
- m_bodies: array[0..3] of Tb2Body;
- m_joints: array[0..7] of Tb2Joint;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- procedure JointDestroyed(joint: Tb2Joint); override;
- end;
- implementation
- { TWeb }
- constructor TWeb.Create;
- var
- ground: Tb2Body;
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- jd: Tb2DistanceJointDef;
- p1, p2, d: TVector2;
- begin
- inherited;
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0);
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, -10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, -10.0);
- {$ENDIF}
- ground := m_world.CreateBody(bd);
- ground.CreateShape(sd);
- end;
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(0.5, 0.5);
- sd.density := 5.0;
- sd.friction := 0.2;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-5.0, 5.0);
- {$ELSE}
- SetValue(bd.position, -5.0, 5.0);
- {$ENDIF}
- m_bodies[0] := m_world.CreateBody(bd, False);
- m_bodies[0].CreateShape(sd, False);
- m_bodies[0].SetMassFromShapes;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(5.0, 5.0);
- {$ELSE}
- SetValue(bd.position, 5.0, 5.0);
- {$ENDIF}
- m_bodies[1] := m_world.CreateBody(bd, False);
- m_bodies[1].CreateShape(sd, False);
- m_bodies[1].SetMassFromShapes;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(5.0, 15.0);
- {$ELSE}
- SetValue(bd.position, 5.0, 15.0);
- {$ENDIF}
- m_bodies[2] := m_world.CreateBody(bd, False);
- m_bodies[2].CreateShape(sd, False);
- m_bodies[2].SetMassFromShapes;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-5.0, 15.0);
- {$ELSE}
- SetValue(bd.position, -5.0, 15.0);
- {$ENDIF}
- m_bodies[3] := m_world.CreateBody(bd);
- m_bodies[3].CreateShape(sd);
- m_bodies[3].SetMassFromShapes;
- jd := Tb2DistanceJointDef.Create;
- jd.frequencyHz := 4.0;
- jd.dampingRatio := 0.5;
- jd.body1 := ground;
- jd.body2 := m_bodies[0];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(-10.0, 10.0);
- jd.localAnchor2.SetValue(-0.5, -0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, -10.0, 10.0);
- SetValue(jd.localAnchor2, -0.5, -0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[0] := m_world.CreateJoint(jd, False);
- jd.body1 := ground;
- jd.body2 := m_bodies[1];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(10.0, 10.0);
- jd.localAnchor2.SetValue(0.5, -0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, 10.0, 10.0);
- SetValue(jd.localAnchor2, 0.5, -0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[1] := m_world.CreateJoint(jd, False);
- jd.body1 := ground;
- jd.body2 := m_bodies[2];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(10.0, 30.0);
- jd.localAnchor2.SetValue(0.5, 0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, 10.0, 30.0);
- SetValue(jd.localAnchor2, 0.5, 0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[2] := m_world.CreateJoint(jd, False);
- jd.body1 := ground;
- jd.body2 := m_bodies[3];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(-10.0, 30.0);
- jd.localAnchor2.SetValue(-0.5, 0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, -10.0, 30.0);
- SetValue(jd.localAnchor2, -0.5, 0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[3] := m_world.CreateJoint(jd, False);
- jd.body1 := m_bodies[0];
- jd.body2 := m_bodies[1];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(0.5, 0.0);
- jd.localAnchor2.SetValue(-0.5, 0.0);
- {$ELSE}
- SetValue(jd.localAnchor1, 0.5, 0.0);
- SetValue(jd.localAnchor2, -0.5, 0.0);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[4] := m_world.CreateJoint(jd, False);
- jd.body1 := m_bodies[1];
- jd.body2 := m_bodies[2];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(0.0, 0.5);
- jd.localAnchor2.SetValue(0.0, -0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, 0.0, 0.5);
- SetValue(jd.localAnchor2, 0.0, -0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[5] := m_world.CreateJoint(jd, False);
- jd.body1 := m_bodies[2];
- jd.body2 := m_bodies[3];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(-0.5, 0.0);
- jd.localAnchor2.SetValue(0.5, 0.0);
- {$ELSE}
- SetValue(jd.localAnchor1, -0.5, 0.0);
- SetValue(jd.localAnchor2, 0.5, 0.0);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[6] := m_world.CreateJoint(jd, False);
- jd.body1 := m_bodies[3];
- jd.body2 := m_bodies[0];
- {$IFDEF OP_OVERLOAD}
- jd.localAnchor1.SetValue(0.0, -0.5);
- jd.localAnchor2.SetValue(0.0, 0.5);
- {$ELSE}
- SetValue(jd.localAnchor1, 0.0, -0.5);
- SetValue(jd.localAnchor2, 0.0, 0.5);
- {$ENDIF}
- p1 := jd.body1.GetWorldPoint(jd.localAnchor1);
- p2 := jd.body2.GetWorldPoint(jd.localAnchor2);
- {$IFDEF OP_OVERLOAD}
- d := p2 - p1;
- jd.length := d.Length;
- {$ELSE}
- d := Subtract(p2, p1);
- jd.length := Length(d);
- {$ENDIF}
- m_joints[7] := m_world.CreateJoint(jd);
- end;
- end;
- procedure TWeb.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('This demonstrates a soft distance joint.');
- DrawText('Press: (b) to delete a body, (j) to delete a joint');
- end;
- procedure TWeb.Keyboard(key: Byte);
- var
- i: Integer;
- begin
- case key of
- 66{B}:
- begin
- for i := 0 to 3 do
- if Assigned(m_bodies[i]) then
- begin
- m_world.DestroyBody(m_bodies[i]);
- m_bodies[i] := nil;
- Break;
- end;
- end;
- 74{J}:
- begin
- for i := 0 to 7 do
- if Assigned(m_joints[i]) then
- begin
- m_world.DestroyJoint(m_joints[i]);
- m_joints[i] := nil;
- Break;
- end;
- end;
- end;
- end;
- procedure TWeb.JointDestroyed(joint: Tb2Joint);
- var
- i: Integer;
- begin
- for i := 0 to 7 do
- if m_joints[i] = joint then
- begin
- m_joints[i] := nil;
- Break;
- end;
- end;
- initialization
- RegisterTestEntry('Web', TWeb);
- end.