UCompoundShapes.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:5k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UCompoundShapes;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils, Math;
- type
- TCompoundShapes = class(TTester)
- public
- constructor Create; override;
- end;
- implementation
- { TCompoundShapes }
- constructor TCompoundShapes.Create;
- var
- i: Integer;
- bd: Tb2BodyDef;
- sd: Tb2PolygonDef;
- body: Tb2Body;
- sd1, sd2: Tb2CircleDef;
- pd1, pd2, sd_bottom, sd_left, sd_right: Tb2PolygonDef;
- xf1, xf2: Tb2XForm;
- begin
- inherited;
- begin
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, -10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, -10.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0);
- body.CreateShape(sd);
- end;
- begin
- sd1 := Tb2CircleDef.Create;
- sd1.radius := 0.5;
- {$IFDEF OP_OVERLOAD}
- sd1.localPosition.SetValue(-0.5, 0.5);
- {$ELSE}
- SetValue(sd1.localPosition, -0.5, 0.5);
- {$ENDIF}
- sd1.density := 2.0;
- sd2 := Tb2CircleDef.Create;
- sd2.radius := 0.5;
- {$IFDEF OP_OVERLOAD}
- sd2.localPosition.SetValue(0.5, 0.5);
- {$ELSE}
- SetValue(sd2.localPosition, 0.5, 0.5);
- {$ENDIF}
- sd2.density := 0.0; // massless
- bd := Tb2BodyDef.Create;
- for i := 0 to 9 do
- begin
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(RandomRange(-10, 10) / 100 + 5.0, 1.05 + 2.5 * i);
- {$ELSE}
- SetValue(bd.position, RandomRange(-10, 10) / 100 + 5.0, 1.05 + 2.5 * i);
- {$ENDIF}
- bd.angle := RandomRange(Round(-Pi * 100), Round(Pi * 100)) / 100;
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd1, False);
- body.CreateShape(sd2, False);
- body.SetMassFromShapes;
- end;
- sd1.Free;
- sd2.Free;
- bd.Free;
- end;
- begin
- pd1 := Tb2PolygonDef.Create;
- pd1.SetAsBox(0.25, 0.5);
- pd1.density := 2.0;
- pd2 := Tb2PolygonDef.Create;
- pd2.SetAsBox(0.25, 0.5, MakeVector(0.0, -0.5), 0.5 * Pi);
- pd2.density := 2.0;
- bd := Tb2BodyDef.Create;
- for i := 0 to 9 do
- begin
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(RandomRange(-10, 10) / 100 - 5.0, 1.05 + 2.5 * i);
- {$ELSE}
- SetValue(bd.position, RandomRange(-10, 10) / 100 - 5.0, 1.05 + 2.5 * i);
- {$ENDIF}
- bd.angle := RandomRange(Round(-Pi * 100), Round(Pi * 100)) / 100;
- body := m_world.CreateBody(bd, False);
- body.CreateShape(pd1, False);
- body.CreateShape(pd2, False);
- body.SetMassFromShapes;
- end;
- bd.Free;
- pd1.Free;
- pd2.Free;
- end;
- begin
- {$IFDEF OP_OVERLOAD}
- xf1.R.SetValue(0.3524 * Pi);
- {$ELSE}
- SetValue(xf1.R, 0.3524 * Pi);
- {$ENDIF}
- xf1.position := b2Mul(xf1.R, MakeVector(1.0, 0.0));
- pd1 := Tb2PolygonDef.Create;
- pd1.vertexCount := 3;
- pd1.vertices[0] := b2Mul(xf1, MakeVector(-1.0, 0.0));
- pd1.vertices[1] := b2Mul(xf1, MakeVector(1.0, 0.0));
- pd1.vertices[2] := b2Mul(xf1, MakeVector(0.0, 0.5));
- pd1.density := 2.0;
- {$IFDEF OP_OVERLOAD}
- xf2.R.SetValue(-0.3524 * Pi);
- {$ELSE}
- SetValue(xf2.R, -0.3524 * Pi);
- {$ENDIF}
- xf2.position := b2Mul(xf2.R, MakeVector(-1.0, 0.0));
- pd2 := Tb2PolygonDef.Create;
- pd2.vertexCount := 3;
- pd2.vertices[0] := b2Mul(xf2, MakeVector(-1.0, 0.0));
- pd2.vertices[1] := b2Mul(xf2, MakeVector(1.0, 0.0));
- pd2.vertices[2] := b2Mul(xf2, MakeVector(0.0, 0.5));
- pd2.density := 2.0;
- bd := Tb2BodyDef.Create;
- for i := 0 to 9 do
- begin
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(RandomRange(-10, 10) / 100, 2.05 + 2.5 * i);
- {$ELSE}
- SetValue(bd.position, RandomRange(-10, 10) / 100, 2.05 + 2.5 * i);
- {$ENDIF}
- bd.angle := 0.0;
- body := m_world.CreateBody(bd, False);
- body.CreateShape(pd1, False);
- body.CreateShape(pd2, False);
- body.SetMassFromShapes;
- end;
- bd.Free;
- pd1.Free;
- pd2.Free;
- end;
- begin
- sd_bottom := Tb2PolygonDef.Create;
- sd_bottom.SetAsBox(1.5, 0.15);
- sd_bottom.density := 4.0;
- sd_left := Tb2PolygonDef.Create;
- sd_left.SetAsBox(0.15, 2.7, MakeVector(-1.45, 2.35), 0.2);
- sd_left.density := 4.0;
- sd_right := Tb2PolygonDef.Create;
- sd_right.SetAsBox(0.15, 2.7, MakeVector(1.45, 2.35), -0.2);
- sd_right.density := 4.0;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 2.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 2.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- body.CreateShape(sd_bottom);
- body.CreateShape(sd_left);
- body.CreateShape(sd_right);
- body.SetMassFromShapes;
- end;
- end;
- initialization
- RegisterTestEntry('Compound Shapes', TCompoundShapes);
- end.