UPolyShapes.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:5k
- unit UPolyShapes;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils, Math;
- const k_maxBodies = 256;
- type
- TPolyShapes = class(TTester)
- private
- procedure CreateBody(index: Int32);
- procedure DestroyBody;
- public
- bodyIndex: Int32;
- bodies: array[0..k_maxBodies - 1] of Tb2Body;
- sds: array[0..3] of Tb2PolygonDef;
- circleDef: Tb2CircleDef;
- constructor Create; override;
- destructor Destroy; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TPolyShapes }
- constructor TPolyShapes.Create;
- var
- w, b, s: Single;
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- begin
- inherited;
- // Ground body
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0);
- sd.friction := 0.3;
- sd.filter.categoryBits := $0001;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, -10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, -10.0);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(sd);
- end;
- sds[0] := Tb2PolygonDef.Create;
- sds[0].vertexCount := 3;
- {$IFDEF OP_OVERLOAD}
- sds[0].vertices[0].SetValue(-0.5, 0.0);
- sds[0].vertices[1].SetValue(0.5, 0.0);
- sds[0].vertices[2].SetValue(0.0, 1.5);
- {$ELSE}
- SetValue(sds[0].vertices[0], -0.5, 0.0);
- SetValue(sds[0].vertices[1], 0.5, 0.0);
- SetValue(sds[0].vertices[2], 0.0, 1.5);
- {$ENDIF}
- sds[0].density := 1.0;
- sds[0].friction := 0.3;
- sds[0].filter.categoryBits := $0002;
- //sds[0].maskBits := 0x0003;
- sds[1] := Tb2PolygonDef.Create;
- sds[1].vertexCount := 3;
- {$IFDEF OP_OVERLOAD}
- sds[1].vertices[0].SetValue(-0.1, 0.0);
- sds[1].vertices[1].SetValue(0.1, 0.0);
- sds[1].vertices[2].SetValue(0.0, 1.5);
- {$ELSE}
- SetValue(sds[1].vertices[0], -0.1, 0.0);
- SetValue(sds[1].vertices[1], 0.1, 0.0);
- SetValue(sds[1].vertices[2], 0.0, 1.5);
- {$ENDIF}
- sds[1].density := 1.0;
- sds[1].friction := 0.3;
- sds[1].filter.categoryBits := $0004;
- sds[2] := Tb2PolygonDef.Create;
- sds[2].vertexCount := 8;
- w := 1.0;
- b := w / (2.0 + Sqrt(2.0));
- s := Sqrt(2.0) * b;
- {$IFDEF OP_OVERLOAD}
- sds[2].vertices[0].SetValue(0.5 * s, 0.0);
- sds[2].vertices[1].SetValue(0.5 * w, b);
- sds[2].vertices[2].SetValue(0.5 * w, b + s);
- sds[2].vertices[3].SetValue(0.5 * s, w);
- sds[2].vertices[4].SetValue(-0.5 * s, w);
- sds[2].vertices[5].SetValue(-0.5 * w, b + s);
- sds[2].vertices[6].SetValue(-0.5 * w, b);
- sds[2].vertices[7].SetValue(-0.5 * s, 0.0);
- {$ELSE}
- SetValue(sds[2].vertices[0], 0.5 * s, 0.0);
- SetValue(sds[2].vertices[1], 0.5 * w, b);
- SetValue(sds[2].vertices[2], 0.5 * w, b + s);
- SetValue(sds[2].vertices[3], 0.5 * s, w);
- SetValue(sds[2].vertices[4], -0.5 * s, w);
- SetValue(sds[2].vertices[5], -0.5 * w, b + s);
- SetValue(sds[2].vertices[6], -0.5 * w, b);
- SetValue(sds[2].vertices[7], -0.5 * s, 0.0);
- {$ENDIF}
- sds[2].density := 1.0;
- sds[2].friction := 0.3;
- sds[2].filter.categoryBits := $0004;
- sds[3] := Tb2PolygonDef.Create;
- sds[3].vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- sds[3].vertices[0].SetValue(-0.5, 0.0);
- sds[3].vertices[1].SetValue(0.5, 0.0);
- sds[3].vertices[2].SetValue(0.5, 1.0);
- sds[3].vertices[3].SetValue(-0.5, 1.0);
- {$ELSE}
- SetValue(sds[3].vertices[0], -0.5, 0.0);
- SetValue(sds[3].vertices[1], 0.5, 0.0);
- SetValue(sds[3].vertices[2], 0.5, 1.0);
- SetValue(sds[3].vertices[3], -0.5, 1.0);
- {$ENDIF}
- sds[3].density := 1.0;
- sds[3].friction := 0.3;
- sds[3].filter.categoryBits := $0004;
- circleDef := Tb2CircleDef.Create;
- circleDef.radius := 0.5;
- circleDef.density := 1.0;
- bodyIndex := 0;
- end;
- destructor TPolyShapes.Destroy;
- begin
- sds[0].Free;
- sds[1].Free;
- sds[2].Free;
- sds[3].Free;
- circleDef.Free;
- inherited;
- end;
- procedure TPolyShapes.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Press 1-5 to drop stuff. D to destroy bodies.');
- end;
- procedure TPolyShapes.CreateBody(index: Int32);
- var
- bd: Tb2BodyDef;
- begin
- if Assigned(bodies[bodyIndex]) then
- begin
- m_world.DestroyBody(bodies[bodyIndex]);
- bodies[bodyIndex] := nil;
- end;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(RandomRange(-200, 200) / 100, 10.0);
- {$ELSE}
- SetValue(bd.position, RandomRange(-200, 200) / 100, 10.0);
- {$ENDIF}
- bd.angle := RandomRange(Round(-Pi) * 100, Round(Pi) * 100) / 100;
- if index = 4 then
- bd.angularDamping := 0.02;
- bodies[bodyIndex] := m_world.CreateBody(bd);
- if index < 4 then
- bodies[bodyIndex].CreateShape(sds[index], False)
- else
- bodies[bodyIndex].CreateShape(circleDef, False);
- bodies[bodyIndex].SetMassFromShapes;
- bodyIndex := (bodyIndex + 1) mod k_maxBodies;
- end;
- procedure TPolyShapes.DestroyBody;
- var
- i: Integer;
- begin
- for i := 0 to k_maxBodies - 1 do
- if Assigned(bodies[i]) then
- begin
- m_world.DestroyBody(bodies[i]);
- bodies[i] := nil;
- Exit;
- end;
- end;
- procedure TPolyShapes.Keyboard(key: Byte);
- begin
- case key of
- 49, 50, 51, 52, 53{1~5}:
- CreateBody(key - 49);
- 68{D}: DestroyBody;
- end;
- end;
- initialization
- RegisterTestEntry('Poly Shapes', TPolyShapes);
- end.