UBiped.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:38k
- unit UBiped;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain,
- UPhysics2DTypes,
- UPhysics2D,
- SysUtils,
- Math;
- type
- TBipedDef = class
- public
- HeadCircle: Tb2CircleDef;
- LFootDef, RFootDef, LCalfDef, RCalfDef, LThighDef, RThighDef,
- PelvisDef, StomachDef, ChestDef, NeckDef, HeadDef,
- LUpperArmDef, RUpperArmDef, LForearmDef, RForearmDef, LHandDef, RHandDef: Tb2BodyDef;
- LFootPoly, RFootPoly, LCalfPoly, RCalfPoly, LThighPoly, RThighPoly,
- PelvisPoly, StomachPoly, ChestPoly, NeckPoly,
- LUpperArmPoly, RUpperArmPoly, LForearmPoly, RForearmPoly, LHandPoly, RHandPoly: Tb2PolygonDef;
- LAnkleDef, RAnkleDef, LKneeDef, RKneeDef, LHipDef, RHipDef,
- LowerAbsDef, UpperAbsDef, LowerNeckDef, UpperNeckDef,
- LShoulderDef, RShoulderDef, LElbowDef, RElbowDef, LWristDef, RWristDef: Tb2RevoluteJointDef;
- constructor Create;
- destructor Destroy; override;
- procedure SetMotorTorque(value: Float);
- procedure SetMotorSpeed(value: Float);
- procedure SetDensity(value: Float);
- procedure SetFriction(value: Float);
- procedure SetRestitution(value: Float);
- procedure SetLinearDamping(value: Float);
- procedure SetAngularDamping(value: Float);
- procedure EnableLimit;
- procedure DisableLimit;
- procedure SetLimit(value: Boolean);
- procedure EnableMotor;
- procedure DisableMotor;
- procedure SetMotor(value: Boolean);
- procedure SetGroupIndex(value: int16);
- procedure IsFast(value: Boolean);
- procedure DefaultVertices;
- procedure DefaultPositions;
- procedure DefaultJoints;
- end;
- TBiped = class
- private
- m_world: Tb2World;
- LFoot, RFoot, LCalf, RCalf, LThigh, RThigh, Pelvis, Stomach,
- Chest, Neck, Head, LUpperArm, RUpperArm, LForearm, RForearm, LHand, RHand: Tb2Body;
- LAnkle, RAnkle, LKnee, RKnee, LHip, RHip, LowerAbs, UpperAbs, LowerNeck, UpperNeck,
- LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist: Tb2RevoluteJoint;
- constructor Create(world: Tb2World; const position: TVector2);
- destructor Destroy; override;
- end;
- TBipedTest1 = class(TTester)
- public
- m_biped: TBiped;
- constructor Create; override;
- destructor Destroy; override;
- end;
- TBipedTest2 = class(TTester)
- public
- m_biped: TBiped;
- constructor Create; override;
- destructor Destroy; override;
- end;
- implementation
- const
- k_scale = 3.0;
- var
- Biped_count: Int16;
- { TBipedDef }
- constructor TBipedDef.Create;
- var
- pFirst: Integer;
- i: Integer;
- begin
- HeadCircle := Tb2CircleDef.Create;
- pFirst := Integer(Self) + SizeOf(Pointer);
- for i := 1 to 17 do
- PInteger(pFirst + SizeOf(Pointer) * i)^ := Integer(Tb2BodyDef.Create);
- for i := 18 to 33 do
- PInteger(pFirst + SizeOf(Pointer) * i)^ := Integer(Tb2PolygonDef.Create);
- for i := 34 to 49 do
- PInteger(pFirst + SizeOf(Pointer) * i)^ := Integer(Tb2RevoluteJointDef.Create);
- SetMotorTorque(2.0);
- SetMotorSpeed(0.0);
- SetDensity(20.0);
- SetRestitution(0.0);
- SetLinearDamping(0.0);
- SetAngularDamping(0.005);
- Dec(Biped_count);
- SetGroupIndex(Biped_count);
- EnableMotor;
- EnableLimit;
- DefaultVertices;
- DefaultPositions;
- DefaultJoints;
- LFootPoly.friction := 0.85;
- RFootPoly.friction := 0.85;
- end;
- destructor TBipedDef.Destroy;
- type
- PObject = ^TObject;
- var
- pFirst: Integer;
- i: Integer;
- begin
- HeadCircle.Free;
- pFirst := Integer(Self) + SizeOf(Pointer);
- for i := 1 to 49 do
- PObject(pFirst + SizeOf(Pointer) * i)^.Free;
- inherited;
- end;
- procedure TBipedDef.SetMotorTorque(value: Float);
- begin
- LAnkleDef.maxMotorTorque := value;
- RAnkleDef.maxMotorTorque := value;
- LKneeDef.maxMotorTorque := value;
- RKneeDef.maxMotorTorque := value;
- LHipDef.maxMotorTorque := value;
- RHipDef.maxMotorTorque := value;
- LowerAbsDef.maxMotorTorque := value;
- UpperAbsDef.maxMotorTorque := value;
- LowerNeckDef.maxMotorTorque := value;
- UpperNeckDef.maxMotorTorque := value;
- LShoulderDef.maxMotorTorque := value;
- RShoulderDef.maxMotorTorque := value;
- LElbowDef.maxMotorTorque := value;
- RElbowDef.maxMotorTorque := value;
- LWristDef.maxMotorTorque := value;
- RWristDef.maxMotorTorque := value;
- end;
- procedure TBipedDef.SetMotorSpeed(value: Float);
- begin
- LAnkleDef.motorSpeed := value;
- RAnkleDef.motorSpeed := value;
- LKneeDef.motorSpeed := value;
- RKneeDef.motorSpeed := value;
- LHipDef.motorSpeed := value;
- RHipDef.motorSpeed := value;
- LowerAbsDef.motorSpeed := value;
- UpperAbsDef.motorSpeed := value;
- LowerNeckDef.motorSpeed := value;
- UpperNeckDef.motorSpeed := value;
- LShoulderDef.motorSpeed := value;
- RShoulderDef.motorSpeed := value;
- LElbowDef.motorSpeed := value;
- RElbowDef.motorSpeed := value;
- LWristDef.motorSpeed := value;
- RWristDef.motorSpeed := value;
- end;
- procedure TBipedDef.SetDensity(value: Float);
- begin
- LFootPoly.density := value;
- RFootPoly.density := value;
- LCalfPoly.density := value;
- RCalfPoly.density := value;
- LThighPoly.density := value;
- RThighPoly.density := value;
- PelvisPoly.density := value;
- StomachPoly.density := value;
- ChestPoly.density := value;
- NeckPoly.density := value;
- HeadCircle.density := value;
- LUpperArmPoly.density := value;
- RUpperArmPoly.density := value;
- LForearmPoly.density := value;
- RForearmPoly.density := value;
- LHandPoly.density := value;
- RHandPoly.density := value;
- end;
- procedure TBipedDef.SetFriction(value: Float);
- begin
- end;
- procedure TBipedDef.SetRestitution(value: Float);
- begin
- LFootPoly.restitution := value;
- RFootPoly.restitution := value;
- LCalfPoly.restitution := value;
- RCalfPoly.restitution := value;
- LThighPoly.restitution := value;
- RThighPoly.restitution := value;
- PelvisPoly.restitution := value;
- StomachPoly.restitution := value;
- ChestPoly.restitution := value;
- NeckPoly.restitution := value;
- HeadCircle.restitution := value;
- LUpperArmPoly.restitution := value;
- RUpperArmPoly.restitution := value;
- LForearmPoly.restitution := value;
- RForearmPoly.restitution := value;
- LHandPoly.restitution := value;
- RHandPoly.restitution := value;
- end;
- procedure TBipedDef.SetLinearDamping(value: Float);
- begin
- LFootDef.linearDamping := value;
- RFootDef.linearDamping := value;
- LCalfDef.linearDamping := value;
- RCalfDef.linearDamping := value;
- LThighDef.linearDamping := value;
- RThighDef.linearDamping := value;
- PelvisDef.linearDamping := value;
- StomachDef.linearDamping := value;
- ChestDef.linearDamping := value;
- NeckDef.linearDamping := value;
- HeadDef.linearDamping := value;
- LUpperArmDef.linearDamping := value;
- RUpperArmDef.linearDamping := value;
- LForearmDef.linearDamping := value;
- RForearmDef.linearDamping := value;
- LHandDef.linearDamping := value;
- RHandDef.linearDamping := value;
- end;
- procedure TBipedDef.SetAngularDamping(value: Float);
- begin
- LFootDef.angularDamping := value;
- RFootDef.angularDamping := value;
- LCalfDef.angularDamping := value;
- RCalfDef.angularDamping := value;
- LThighDef.angularDamping := value;
- RThighDef.angularDamping := value;
- PelvisDef.angularDamping := value;
- StomachDef.angularDamping := value;
- ChestDef.angularDamping := value;
- NeckDef.angularDamping := value;
- HeadDef.angularDamping := value;
- LUpperArmDef.angularDamping := value;
- RUpperArmDef.angularDamping := value;
- LForearmDef.angularDamping := value;
- RForearmDef.angularDamping := value;
- LHandDef.angularDamping := value;
- RHandDef.angularDamping := value;
- end;
- procedure TBipedDef.EnableLimit;
- begin
- SetLimit(True);
- end;
- procedure TBipedDef.DisableLimit;
- begin
- SetLimit(False);
- end;
- procedure TBipedDef.SetLimit(value: Boolean);
- begin
- LAnkleDef.enableLimit := value;
- RAnkleDef.enableLimit := value;
- LKneeDef.enableLimit := value;
- RKneeDef.enableLimit := value;
- LHipDef.enableLimit := value;
- RHipDef.enableLimit := value;
- LowerAbsDef.enableLimit := value;
- UpperAbsDef.enableLimit := value;
- LowerNeckDef.enableLimit := value;
- UpperNeckDef.enableLimit := value;
- LShoulderDef.enableLimit := value;
- RShoulderDef.enableLimit := value;
- LElbowDef.enableLimit := value;
- RElbowDef.enableLimit := value;
- LWristDef.enableLimit := value;
- RWristDef.enableLimit := value;
- end;
- procedure TBipedDef.EnableMotor;
- begin
- SetMotor(True);
- end;
- procedure TBipedDef.DisableMotor;
- begin
- SetMotor(False);
- end;
- procedure TBipedDef.SetMotor(value: Boolean);
- begin
- LAnkleDef.enableMotor := value;
- RAnkleDef.enableMotor := value;
- LKneeDef.enableMotor := value;
- RKneeDef.enableMotor := value;
- LHipDef.enableMotor := value;
- RHipDef.enableMotor := value;
- LowerAbsDef.enableMotor := value;
- UpperAbsDef.enableMotor := value;
- LowerNeckDef.enableMotor := value;
- UpperNeckDef.enableMotor := value;
- LShoulderDef.enableMotor := value;
- RShoulderDef.enableMotor := value;
- LElbowDef.enableMotor := value;
- RElbowDef.enableMotor := value;
- LWristDef.enableMotor := value;
- RWristDef.enableMotor := value;
- end;
- procedure TBipedDef.SetGroupIndex(value: int16);
- begin
- LFootPoly.filter.groupIndex := value;
- RFootPoly.filter.groupIndex := value;
- LCalfPoly.filter.groupIndex := value;
- RCalfPoly.filter.groupIndex := value;
- LThighPoly.filter.groupIndex := value;
- RThighPoly.filter.groupIndex := value;
- PelvisPoly.filter.groupIndex := value;
- StomachPoly.filter.groupIndex := value;
- ChestPoly.filter.groupIndex := value;
- NeckPoly.filter.groupIndex := value;
- HeadCircle.filter.groupIndex := value;
- LUpperArmPoly.filter.groupIndex := value;
- RUpperArmPoly.filter.groupIndex := value;
- LForearmPoly.filter.groupIndex := value;
- RForearmPoly.filter.groupIndex := value;
- LHandPoly.filter.groupIndex := value;
- RHandPoly.filter.groupIndex := value;
- end;
- procedure TBipedDef.IsFast(value: Boolean);
- begin
- end;
- procedure TBipedDef.DefaultVertices;
- begin
- begin // feet
- LFootPoly.vertexCount := 5;
- RFootPoly.vertexCount := 5;
- {$IFDEF OP_OVERLOAD}
- LFootPoly.vertices[0] := k_scale * MakeVector(0.033, 0.143);
- LFootPoly.vertices[1] := k_scale * MakeVector(0.023, 0.033);
- LFootPoly.vertices[2] := k_scale * MakeVector(0.267, 0.035);
- LFootPoly.vertices[3] := k_scale * MakeVector(0.265, 0.065);
- LFootPoly.vertices[4] := k_scale * MakeVector(0.117, 0.143);
- {$ELSE}
- LFootPoly.vertices[0] := Multiply(MakeVector(0.033, 0.143), k_scale);
- LFootPoly.vertices[1] := Multiply(MakeVector(0.023, 0.033), k_scale);
- LFootPoly.vertices[2] := Multiply(MakeVector(0.267, 0.035), k_scale);
- LFootPoly.vertices[3] := Multiply(MakeVector(0.265, 0.065), k_scale);
- LFootPoly.vertices[4] := Multiply(MakeVector(0.117, 0.143), k_scale);
- {$ENDIF}
- RFootPoly.vertices := LFootPoly.vertices;
- end;
- begin // calves
- LCalfPoly.vertexCount := 4;
- RCalfPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- LCalfPoly.vertices[0] := k_scale * MakeVector(0.089, 0.016);
- LCalfPoly.vertices[1] := k_scale * MakeVector(0.178, 0.016);
- LCalfPoly.vertices[2] := k_scale * MakeVector(0.205, 0.417);
- LCalfPoly.vertices[3] := k_scale * MakeVector(0.095, 0.417);
- {$ELSE}
- LCalfPoly.vertices[0] := Multiply(MakeVector(0.089, 0.016), k_scale);
- LCalfPoly.vertices[1] := Multiply(MakeVector(0.178, 0.016), k_scale);
- LCalfPoly.vertices[2] := Multiply(MakeVector(0.205, 0.417), k_scale);
- LCalfPoly.vertices[3] := Multiply(MakeVector(0.095, 0.417), k_scale);
- {$ENDIF}
- RCalfPoly.vertices := LCalfPoly.vertices;
- end;
- begin // thighs
- LThighPoly.vertexCount := 4;
- RThighPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- LThighPoly.vertices[0] := k_scale * MakeVector(0.137, 0.032);
- LThighPoly.vertices[1] := k_scale * MakeVector(0.243, 0.032);
- LThighPoly.vertices[2] := k_scale * MakeVector(0.318, 0.343);
- LThighPoly.vertices[3] := k_scale * MakeVector(0.142, 0.343);
- {$ELSE}
- LThighPoly.vertices[0] := Multiply(MakeVector(0.137, 0.032), k_scale);
- LThighPoly.vertices[1] := Multiply(MakeVector(0.243, 0.032), k_scale);
- LThighPoly.vertices[2] := Multiply(MakeVector(0.318, 0.343), k_scale);
- LThighPoly.vertices[3] := Multiply(MakeVector(0.142, 0.343), k_scale);
- {$ENDIF}
- RThighPoly.vertices := LThighPoly.vertices;
- end;
- begin // pelvis
- PelvisPoly.vertexCount := 5;
- {$IFDEF OP_OVERLOAD}
- PelvisPoly.vertices[0] := k_scale * MakeVector(0.105, 0.051);
- PelvisPoly.vertices[1] := k_scale * MakeVector(0.277, 0.053);
- PelvisPoly.vertices[2] := k_scale * MakeVector(0.320, 0.233);
- PelvisPoly.vertices[3] := k_scale * MakeVector(0.112, 0.233);
- PelvisPoly.vertices[4] := k_scale * MakeVector(0.067, 0.152);
- {$ELSE}
- PelvisPoly.vertices[0] := Multiply(MakeVector(0.105, 0.051), k_scale);
- PelvisPoly.vertices[1] := Multiply(MakeVector(0.277, 0.053), k_scale);
- PelvisPoly.vertices[2] := Multiply(MakeVector(0.320, 0.233), k_scale);
- PelvisPoly.vertices[3] := Multiply(MakeVector(0.112, 0.233), k_scale);
- PelvisPoly.vertices[4] := Multiply(MakeVector(0.067, 0.152), k_scale);
- {$ENDIF}
- end;
- begin // stomach
- StomachPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- StomachPoly.vertices[0] := k_scale * MakeVector(0.088, 0.043);
- StomachPoly.vertices[1] := k_scale * MakeVector(0.284, 0.043);
- StomachPoly.vertices[2] := k_scale * MakeVector(0.295, 0.231);
- StomachPoly.vertices[3] := k_scale * MakeVector(0.100, 0.231);
- {$ELSE}
- StomachPoly.vertices[0] := Multiply(MakeVector(0.088, 0.043), k_scale);
- StomachPoly.vertices[1] := Multiply(MakeVector(0.284, 0.043), k_scale);
- StomachPoly.vertices[2] := Multiply(MakeVector(0.295, 0.231), k_scale);
- StomachPoly.vertices[3] := Multiply(MakeVector(0.100, 0.231), k_scale);
- {$ENDIF}
- end;
- begin // chest
- ChestPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- ChestPoly.vertices[0] := k_scale * MakeVector(0.091, 0.042);
- ChestPoly.vertices[1] := k_scale * MakeVector(0.283, 0.042);
- ChestPoly.vertices[2] := k_scale * MakeVector(0.177, 0.289);
- ChestPoly.vertices[3] := k_scale * MakeVector(0.065, 0.289);
- {$ELSE}
- ChestPoly.vertices[0] := Multiply(MakeVector(0.091, 0.042), k_scale);
- ChestPoly.vertices[1] := Multiply(MakeVector(0.283, 0.042), k_scale);
- ChestPoly.vertices[2] := Multiply(MakeVector(0.177, 0.289), k_scale);
- ChestPoly.vertices[3] := Multiply(MakeVector(0.065, 0.289), k_scale);
- {$ENDIF}
- end;
- begin // head
- HeadCircle.radius := k_scale * 0.115;
- end;
- begin // neck
- NeckPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- NeckPoly.vertices[0] := k_scale * MakeVector(0.038, 0.054);
- NeckPoly.vertices[1] := k_scale * MakeVector(0.149, 0.054);
- NeckPoly.vertices[2] := k_scale * MakeVector(0.154, 0.102);
- NeckPoly.vertices[3] := k_scale * MakeVector(0.054, 0.113);
- {$ELSE}
- NeckPoly.vertices[0] := Multiply(MakeVector(0.038, 0.054), k_scale);
- NeckPoly.vertices[1] := Multiply(MakeVector(0.149, 0.054), k_scale);
- NeckPoly.vertices[2] := Multiply(MakeVector(0.154, 0.102), k_scale);
- NeckPoly.vertices[3] := Multiply(MakeVector(0.054, 0.113), k_scale);
- {$ENDIF}
- end;
- begin // upper arms
- LUpperArmPoly.vertexCount := 5;
- RUpperArmPoly.vertexCount := 5;
- {$IFDEF OP_OVERLOAD}
- LUpperArmPoly.vertices[0] := k_scale * MakeVector(0.092, 0.059);
- LUpperArmPoly.vertices[1] := k_scale * MakeVector(0.159, 0.059);
- LUpperArmPoly.vertices[2] := k_scale * MakeVector(0.169, 0.335);
- LUpperArmPoly.vertices[3] := k_scale * MakeVector(0.078, 0.335);
- LUpperArmPoly.vertices[4] := k_scale * MakeVector(0.064, 0.248);
- {$ELSE}
- LUpperArmPoly.vertices[0] := Multiply(MakeVector(0.092, 0.059), k_scale);
- LUpperArmPoly.vertices[1] := Multiply(MakeVector(0.159, 0.059), k_scale);
- LUpperArmPoly.vertices[2] := Multiply(MakeVector(0.169, 0.335), k_scale);
- LUpperArmPoly.vertices[3] := Multiply(MakeVector(0.078, 0.335), k_scale);
- LUpperArmPoly.vertices[4] := Multiply(MakeVector(0.064, 0.248), k_scale);
- {$ENDIF}
- RUpperArmPoly.vertices := LUpperArmPoly.vertices;
- end;
- begin // forearms
- LForearmPoly.vertexCount := 4;
- RForearmPoly.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- LForearmPoly.vertices[0] := k_scale * MakeVector(0.082, 0.054);
- LForearmPoly.vertices[1] := k_scale * MakeVector(0.138, 0.054);
- LForearmPoly.vertices[2] := k_scale * MakeVector(0.149, 0.296);
- LForearmPoly.vertices[3] := k_scale * MakeVector(0.088, 0.296);
- {$ELSE}
- LForearmPoly.vertices[0] := Multiply(MakeVector(0.082, 0.054), k_scale);
- LForearmPoly.vertices[1] := Multiply(MakeVector(0.138, 0.054), k_scale);
- LForearmPoly.vertices[2] := Multiply(MakeVector(0.149, 0.296), k_scale);
- LForearmPoly.vertices[3] := Multiply(MakeVector(0.088, 0.296), k_scale);
- {$ENDIF}
- RForearmPoly.vertices := LForearmPoly.vertices;
- end;
- begin // hands
- LHandPoly.vertexCount := 5;
- RHandPoly.vertexCount := 5;
- {$IFDEF OP_OVERLOAD}
- LHandPoly.vertices[0] := k_scale * MakeVector(0.066, 0.031);
- LHandPoly.vertices[1] := k_scale * MakeVector(0.123, 0.020);
- LHandPoly.vertices[2] := k_scale * MakeVector(0.160, 0.127);
- LHandPoly.vertices[3] := k_scale * MakeVector(0.127, 0.178);
- LHandPoly.vertices[4] := k_scale * MakeVector(0.074, 0.178);
- {$ELSE}
- LHandPoly.vertices[0] := Multiply(MakeVector(0.066, 0.031), k_scale);
- LHandPoly.vertices[1] := Multiply(MakeVector(0.123, 0.020), k_scale);
- LHandPoly.vertices[2] := Multiply(MakeVector(0.160, 0.127), k_scale);
- LHandPoly.vertices[3] := Multiply(MakeVector(0.127, 0.178), k_scale);
- LHandPoly.vertices[4] := Multiply(MakeVector(0.074, 0.178), k_scale);
- {$ENDIF}
- RHandPoly.vertices := LHandPoly.vertices;
- end;
- end;
- procedure TBipedDef.DefaultPositions;
- begin
- {$IFDEF OP_OVERLOAD}
- LFootDef.position := k_scale * MakeVector(-0.122, -0.901);
- LCalfDef.position := k_scale * MakeVector(-0.177, -0.771);
- LThighDef.position := k_scale * MakeVector(-0.217, -0.391);
- LUpperArmDef.position := k_scale * MakeVector(-0.127, 0.228);
- LForearmDef.position := k_scale * MakeVector(-0.117, -0.011);
- LHandDef.position := k_scale * MakeVector(-0.112, -0.136);
- PelvisDef.position := k_scale * MakeVector(-0.177, -0.101);
- StomachDef.position := k_scale * MakeVector(-0.142, 0.088);
- ChestDef.position := k_scale * MakeVector(-0.132, 0.282);
- NeckDef.position := k_scale * MakeVector(-0.102, 0.518);
- HeadDef.position := k_scale * MakeVector(0.022, 0.738);
- {$ELSE}
- LFootDef.position := Multiply(MakeVector(-0.122, -0.901), k_scale);
- LCalfDef.position := Multiply(MakeVector(-0.177, -0.771), k_scale);
- LThighDef.position := Multiply(MakeVector(-0.217, -0.391), k_scale);
- LUpperArmDef.position := Multiply(MakeVector(-0.127, 0.228), k_scale);
- LForearmDef.position := Multiply(MakeVector(-0.117, -0.011), k_scale);
- LHandDef.position := Multiply(MakeVector(-0.112, -0.136), k_scale);
- PelvisDef.position := Multiply(MakeVector(-0.177, -0.101), k_scale);
- StomachDef.position := Multiply(MakeVector(-0.142, 0.088), k_scale);
- ChestDef.position := Multiply(MakeVector(-0.132, 0.282), k_scale);
- NeckDef.position := Multiply(MakeVector(-0.102, 0.518), k_scale);
- HeadDef.position := Multiply(MakeVector(0.022, 0.738), k_scale);
- {$ENDIF}
- RFootDef.position := LFootDef.position;
- RCalfDef.position := LCalfDef.position;
- RThighDef.position := LThighDef.position;
- RUpperArmDef.position := LUpperArmDef.position;
- RForearmDef.position := LForearmDef.position;
- RHandDef.position := LHandDef.position;
- end;
- procedure TBipedDef.DefaultJoints;
- var
- anchor: TVector2;
- begin
- begin // ankles
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.045, -0.75);
- LAnkleDef.localAnchor1 := anchor - LFootDef.position;
- LAnkleDef.localAnchor2 := anchor - LCalfDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.045, -0.75), k_scale);
- LAnkleDef.localAnchor1 := Subtract(anchor, LFootDef.position);
- LAnkleDef.localAnchor2 := Subtract(anchor, LCalfDef.position);
- {$ENDIF}
- RAnkleDef.localAnchor1 := LAnkleDef.localAnchor1;
- RAnkleDef.localAnchor2 := LAnkleDef.localAnchor2;
- LAnkleDef.referenceAngle := 0.0;
- RAnkleDef.referenceAngle := 0.0;
- LAnkleDef.lowerAngle := -0.523598776;
- RAnkleDef.lowerAngle := -0.523598776;
- LAnkleDef.upperAngle := 0.523598776;
- RAnkleDef.upperAngle := 0.523598776
- end;
- begin // knees
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.030, -0.355);
- LKneeDef.localAnchor1 := anchor - LCalfDef.position;
- LKneeDef.localAnchor2 := anchor - LThighDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.030, -0.355), k_scale);
- LKneeDef.localAnchor1 := Subtract(anchor, LCalfDef.position);
- LKneeDef.localAnchor2 := Subtract(anchor, LThighDef.position);
- {$ENDIF}
- RKneeDef.localAnchor1 := LKneeDef.localAnchor1;
- RKneeDef.localAnchor2 := LKneeDef.localAnchor2;
- LKneeDef.referenceAngle := 0.0;
- RKneeDef.referenceAngle := 0.0;
- LKneeDef.lowerAngle := 0;
- RKneeDef.lowerAngle := 0;
- LKneeDef.upperAngle := 2.61799388;
- RKneeDef.upperAngle := 2.61799388;
- end;
- begin // hips
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(0.005, -0.045);
- LHipDef.localAnchor1 := anchor - LThighDef.position;
- LHipDef.localAnchor2 := anchor - PelvisDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(0.005, -0.045), k_scale);
- LHipDef.localAnchor1 := Subtract(anchor, LThighDef.position);
- LHipDef.localAnchor2 := Subtract(anchor, PelvisDef.position);
- {$ENDIF}
- RHipDef.localAnchor1 := LHipDef.localAnchor1;
- RHipDef.localAnchor2 := LHipDef.localAnchor2;
- LHipDef.referenceAngle := 0.0;
- RHipDef.referenceAngle := 0.0;
- LHipDef.lowerAngle := -2.26892803;
- RHipDef.lowerAngle := -2.26892803;
- LHipDef.upperAngle := 0;
- RHipDef.upperAngle := 0;
- end;
- begin // lower abs
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(0.035, 0.135);
- LowerAbsDef.localAnchor1 := anchor - PelvisDef.position;
- LowerAbsDef.localAnchor2 := anchor - StomachDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(0.035, 0.135), k_scale);
- LowerAbsDef.localAnchor1 := Subtract(anchor, PelvisDef.position);
- LowerAbsDef.localAnchor2 := Subtract(anchor, StomachDef.position);
- {$ENDIF}
- LowerAbsDef.referenceAngle := 0.0;
- LowerAbsDef.lowerAngle := -0.523598776;
- LowerAbsDef.upperAngle := 0.523598776;
- end;
- begin // upper abs
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(0.045, 0.320);
- UpperAbsDef.localAnchor1 := anchor - StomachDef.position;
- UpperAbsDef.localAnchor2 := anchor - ChestDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(0.045, 0.320), k_scale);
- UpperAbsDef.localAnchor1 := Subtract(anchor, StomachDef.position);
- UpperAbsDef.localAnchor2 := Subtract(anchor, ChestDef.position);
- {$ENDIF}
- UpperAbsDef.referenceAngle := 0.0;
- UpperAbsDef.lowerAngle := -0.523598776;
- UpperAbsDef.upperAngle := 0.174532925;
- end;
- begin // lower neck
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.015, 0.575);
- LowerNeckDef.localAnchor1 := anchor - ChestDef.position;
- LowerNeckDef.localAnchor2 := anchor - NeckDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.015, 0.575), k_scale);
- LowerNeckDef.localAnchor1 := Subtract(anchor, ChestDef.position);
- LowerNeckDef.localAnchor2 := Subtract(anchor, NeckDef.position);
- {$ENDIF}
- LowerNeckDef.referenceAngle := 0.0;
- LowerNeckDef.lowerAngle := -0.174532925;
- LowerNeckDef.upperAngle := 0.174532925;
- end;
- begin // upper neck
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.005, 0.630);
- UpperNeckDef.localAnchor1 := anchor - ChestDef.position;
- UpperNeckDef.localAnchor2 := anchor - HeadDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.005, 0.630), k_scale);
- UpperNeckDef.localAnchor1 := Subtract(anchor, ChestDef.position);
- UpperNeckDef.localAnchor2 := Subtract(anchor, HeadDef.position);
- {$ENDIF}
- UpperNeckDef.referenceAngle := 0.0;
- UpperNeckDef.lowerAngle := -0.610865238;
- UpperNeckDef.upperAngle := 0.785398163;
- end;
- begin // shoulders
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.015, 0.545);
- LShoulderDef.localAnchor1 := anchor - ChestDef.position;
- LShoulderDef.localAnchor2 := anchor - LUpperArmDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.015, 0.545), k_scale);
- LShoulderDef.localAnchor1 := Subtract(anchor, ChestDef.position);
- LShoulderDef.localAnchor2 := Subtract(anchor, LUpperArmDef.position);
- {$ENDIF}
- RShoulderDef.localAnchor1 := LShoulderDef.localAnchor1;
- RShoulderDef.localAnchor2 := LShoulderDef.localAnchor2;
- LShoulderDef.referenceAngle := 0.0;
- RShoulderDef.referenceAngle := 0.0;
- LShoulderDef.lowerAngle := -1.04719755;
- RShoulderDef.lowerAngle := -1.04719755;
- LShoulderDef.upperAngle := 3.14159265;
- RShoulderDef.upperAngle := 3.14159265;
- end;
- begin // elbows
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.005, 0.290);
- LElbowDef.localAnchor1 := anchor - LForearmDef.position;
- LElbowDef.localAnchor2 := anchor - LUpperArmDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.005, 0.290), k_scale);
- LElbowDef.localAnchor1 := Subtract(anchor, LForearmDef.position);
- LElbowDef.localAnchor2 := Subtract(anchor, LUpperArmDef.position);
- {$ENDIF}
- RElbowDef.localAnchor1 := LElbowDef.localAnchor1;
- RElbowDef.localAnchor2 := LElbowDef.localAnchor2;
- LElbowDef.referenceAngle := 0.0;
- RElbowDef.referenceAngle := 0.0;
- LElbowDef.lowerAngle := -2.7925268;
- RElbowDef.lowerAngle := -2.7925268;
- LElbowDef.upperAngle := 0;
- RElbowDef.upperAngle := 0;
- end;
- begin // wrists
- {$IFDEF OP_OVERLOAD}
- anchor := k_scale * MakeVector(-0.010, 0.045);
- LWristDef.localAnchor1 := anchor - LHandDef.position;
- LWristDef.localAnchor2 := anchor - LForearmDef.position;
- {$ELSE}
- anchor := Multiply(MakeVector(-0.010, 0.045), k_scale);
- LWristDef.localAnchor1 := Subtract(anchor, LHandDef.position);
- LWristDef.localAnchor2 := Subtract(anchor, LForearmDef.position);
- {$ENDIF}
- RWristDef.localAnchor1 := LWristDef.localAnchor1;
- RWristDef.localAnchor2 := LWristDef.localAnchor2;
- LWristDef.referenceAngle := 0.0;
- RWristDef.referenceAngle := 0.0;
- LWristDef.lowerAngle := -0.174532925;
- RWristDef.lowerAngle := -0.174532925;
- LWristDef.upperAngle := 0.174532925;
- RWristDef.upperAngle := 0.174532925;
- end;
- end;
- { TBiped }
- constructor TBiped.Create(world: Tb2World; const position: TVector2);
- var
- def: TBipedDef;
- bd: Tb2BodyDef;
- begin
- m_world := world;
- def := TBipedDef.Create;
- // create body parts
- bd := def.LFootDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LFoot := world.CreateBody(bd, False);
- LFoot.CreateShape(def.LFootPoly, False);
- LFoot.SetMassFromShapes;
- bd := def.RFootDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RFoot := world.CreateBody(bd, False);
- RFoot.CreateShape(def.RFootPoly, False);
- RFoot.SetMassFromShapes;
- bd := def.LCalfDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LCalf := world.CreateBody(bd, False);
- LCalf.CreateShape(def.LCalfPoly, False);
- LCalf.SetMassFromShapes;
- bd := def.RCalfDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RCalf := world.CreateBody(bd, False);
- RCalf.CreateShape(def.RCalfPoly, False);
- RCalf.SetMassFromShapes;
- bd := def.LThighDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LThigh := world.CreateBody(bd, False);
- LThigh.CreateShape(def.LThighPoly, False);
- LThigh.SetMassFromShapes;
- bd := def.RThighDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RThigh := world.CreateBody(bd, False);
- RThigh.CreateShape(def.RThighPoly, False);
- RThigh.SetMassFromShapes;
- bd := def.PelvisDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- Pelvis := world.CreateBody(bd, False);
- Pelvis.CreateShape(def.PelvisPoly, False);
- Pelvis.SetMassFromShapes;
- bd := def.StomachDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- Stomach := world.CreateBody(bd, False);
- Stomach.CreateShape(def.StomachPoly, False);
- Stomach.SetMassFromShapes;
- bd := def.ChestDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- Chest := world.CreateBody(bd, False);
- Chest.CreateShape(def.ChestPoly, False);
- Chest.SetMassFromShapes;
- bd := def.NeckDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- Neck := world.CreateBody(bd, False);
- Neck.CreateShape(def.NeckPoly, False);
- Neck.SetMassFromShapes;
- bd := def.HeadDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- Head := world.CreateBody(bd, False);
- Head.CreateShape(def.HeadCircle, False);
- Head.SetMassFromShapes;
- bd := def.LUpperArmDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LUpperArm := world.CreateBody(bd, False);
- LUpperArm.CreateShape(def.LUpperArmPoly, False);
- LUpperArm.SetMassFromShapes;
- bd := def.RUpperArmDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RUpperArm := world.CreateBody(bd, False);
- RUpperArm.CreateShape(def.RUpperArmPoly, False);
- RUpperArm.SetMassFromShapes;
- bd := def.LForearmDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LForearm := world.CreateBody(bd, False);
- LForearm.CreateShape(def.LForearmPoly, False);
- LForearm.SetMassFromShapes;
- bd := def.RForearmDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RForearm := world.CreateBody(bd, False);
- RForearm.CreateShape(def.RForearmPoly, False);
- RForearm.SetMassFromShapes;
- bd := def.LHandDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- LHand := world.CreateBody(bd, False);
- LHand.CreateShape(def.LHandPoly, False);
- LHand.SetMassFromShapes;
- bd := def.RHandDef;
- {$IFDEF OP_OVERLOAD}
- bd.position.AddBy(position);
- {$ELSE}
- AddBy(bd.position, position);
- {$ENDIF}
- RHand := world.CreateBody(bd, False);
- RHand.CreateShape(def.RHandPoly, False);
- RHand.SetMassFromShapes;
- // link body parts
- def.LAnkleDef.body1 := LFoot;
- def.LAnkleDef.body2 := LCalf;
- def.RAnkleDef.body1 := RFoot;
- def.RAnkleDef.body2 := RCalf;
- def.LKneeDef.body1 := LCalf;
- def.LKneeDef.body2 := LThigh;
- def.RKneeDef.body1 := RCalf;
- def.RKneeDef.body2 := RThigh;
- def.LHipDef.body1 := LThigh;
- def.LHipDef.body2 := Pelvis;
- def.RHipDef.body1 := RThigh;
- def.RHipDef.body2 := Pelvis;
- def.LowerAbsDef.body1 := Pelvis;
- def.LowerAbsDef.body2 := Stomach;
- def.UpperAbsDef.body1 := Stomach;
- def.UpperAbsDef.body2 := Chest;
- def.LowerNeckDef.body1 := Chest;
- def.LowerNeckDef.body2 := Neck;
- def.UpperNeckDef.body1 := Chest;
- def.UpperNeckDef.body2 := Head;
- def.LShoulderDef.body1 := Chest;
- def.LShoulderDef.body2 := LUpperArm;
- def.RShoulderDef.body1 := Chest;
- def.RShoulderDef.body2 := RUpperArm;
- def.LElbowDef.body1 := LForearm;
- def.LElbowDef.body2 := LUpperArm;
- def.RElbowDef.body1 := RForearm;
- def.RElbowDef.body2 := RUpperArm;
- def.LWristDef.body1 := LHand;
- def.LWristDef.body2 := LForearm;
- def.RWristDef.body1 := RHand;
- def.RWristDef.body2 := RForearm;
- // create joints
- LAnkle := Tb2RevoluteJoint(world.CreateJoint(def.LAnkleDef, False));
- RAnkle := Tb2RevoluteJoint(world.CreateJoint(def.RAnkleDef, False));
- LKnee := Tb2RevoluteJoint(world.CreateJoint(def.LKneeDef, False));
- RKnee := Tb2RevoluteJoint(world.CreateJoint(def.RKneeDef, False));
- LHip := Tb2RevoluteJoint(world.CreateJoint(def.LHipDef, False));
- RHip := Tb2RevoluteJoint(world.CreateJoint(def.RHipDef, False));
- LowerAbs := Tb2RevoluteJoint(world.CreateJoint(def.LowerAbsDef, False));
- UpperAbs := Tb2RevoluteJoint(world.CreateJoint(def.UpperAbsDef, False));
- LowerNeck := Tb2RevoluteJoint(world.CreateJoint(def.LowerNeckDef, False));
- UpperNeck := Tb2RevoluteJoint(world.CreateJoint(def.UpperNeckDef, False));
- LShoulder := Tb2RevoluteJoint(world.CreateJoint(def.LShoulderDef, False));
- RShoulder := Tb2RevoluteJoint(world.CreateJoint(def.RShoulderDef, False));
- LElbow := Tb2RevoluteJoint(world.CreateJoint(def.LElbowDef, False));
- RElbow := Tb2RevoluteJoint(world.CreateJoint(def.RElbowDef, False));
- LWrist := Tb2RevoluteJoint(world.CreateJoint(def.LWristDef, False));
- RWrist := Tb2RevoluteJoint(world.CreateJoint(def.RWristDef, False));
- def.Free;
- end;
- destructor TBiped.Destroy;
- begin
- with m_world do
- begin
- DestroyBody(LFoot);
- DestroyBody(RFoot);
- DestroyBody(LCalf);
- DestroyBody(RCalf);
- DestroyBody(LThigh);
- DestroyBody(RThigh);
- DestroyBody(Pelvis);
- DestroyBody(Stomach);
- DestroyBody(Chest);
- DestroyBody(Neck);
- DestroyBody(Head);
- DestroyBody(LUpperArm);
- DestroyBody(RUpperArm);
- DestroyBody(LForearm);
- DestroyBody(RForearm);
- DestroyBody(LHand);
- DestroyBody(RHand);
- end;
- inherited;
- end;
- { TBipedTest1 }
- constructor TBipedTest1.Create;
- const
- k_restitution = 1.4;
- var
- i: Integer;
- bd: Tb2BodyDef;
- body: Tb2Body;
- sd: Tb2PolygonDef;
- cd: Tb2CircleDef;
- begin
- inherited;
- begin
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 20.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 20.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- sd := Tb2PolygonDef.Create;
- sd.density := 0.0;
- sd.restitution := k_restitution;
- sd.SetAsBox(0.1, 10.0, MakeVector(-10.0, 0.0), 0.0);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(10.0, 0.0), 0.0);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(0.0, -10.0), 0.5 * Pi);
- body.CreateShape(sd, False);
- sd.SetAsBox(0.1, 10.0, MakeVector(0.0, 10.0), -0.5 * Pi);
- body.CreateShape(sd);
- end;
- m_biped := TBiped.Create(m_world, MakeVector(0.0, 20.0));
- bd := Tb2BodyDef.Create;
- bd.isBullet := True;
- cd := Tb2CircleDef.Create;
- cd.radius := 0.25;
- cd.density := 15.0;
- cd.restitution := k_restitution;
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(0.25, 0.25);
- sd.density := 15.0;
- sd.restitution := k_restitution;
- for i := 0 to 7 do
- begin
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(5.0, 20.0 + i);
- {$ELSE}
- SetValue(bd.position, 5.0, 20.0 + i);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.SetLinearVelocity(MakeVector(0.0, -100.0));
- body.SetAngularVelocity(RandomRange(-50, 50));
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- end;
- bd.Free;
- cd.Free;
- sd.Free;
- end;
- destructor TBipedTest1.Destroy;
- begin
- m_biped.Free;
- inherited;
- end;
- { TBipedTest2 }
- constructor TBipedTest2.Create;
- var
- i: Integer;
- pd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- begin
- inherited;
- pd := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- for i := 0 to 8 do
- begin
- pd.SetAsBox(1, 1, MakeVector(-4 + i, 4 - i), 0);
- pd.friction := 0.2;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-4 + i, 4 - i);
- {$ELSE}
- SetValue(bd.position, -4 + i, 4 - i);
- {$ENDIF}
- m_world.CreateBody(bd, False).CreateShape(pd, False);
- end;
- pd.SetAsBox(30, 0.5, MakeVector(0, -5), 0);
- pd.friction := 0.2;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0, -5);
- {$ELSE}
- SetValue(bd.position, 0, -5);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(pd);
- m_biped := TBiped.Create(m_world, MakeVector(-3.5, 10.0));
- end;
- destructor TBipedTest2.Destroy;
- begin
- m_biped.Free;
- inherited;
- end;
- initialization
- Biped_count := 0;
- RegisterTestEntry('Biped1', TBipedTest1);
- RegisterTestEntry('Biped2', TBipedTest2);
- end.