UMotorsAndLimits.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:4k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UMotorsAndLimits;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TMotorsAndLimits = class(TTester)
- public
- m_joint1, m_joint2: Tb2RevoluteJoint;
- m_joint3: Tb2PrismaticJoint;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TMotorsAndLimits }
- constructor TMotorsAndLimits.Create;
- const
- y = 8.0;
- var
- ground, body, prevBody: Tb2Body;
- sd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- rjd: Tb2RevoluteJointDef;
- pjd: Tb2PrismaticJointDef;
- 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(2.0, 0.5);
- sd.density := 5.0;
- sd.friction := 0.05;
- bd := Tb2BodyDef.Create;
- rjd := Tb2RevoluteJointDef.Create;
- prevBody := ground;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(3.0, y);
- {$ELSE}
- SetValue(bd.position, 3.0, y);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- rjd.Initialize(prevBody, body, MakeVector(0.0, y));
- rjd.motorSpeed := 1.0 * Pi;
- rjd.maxMotorTorque := 10000.0;
- rjd.enableMotor := True;
- m_joint1 := Tb2RevoluteJoint(m_world.CreateJoint(rjd, False));
- prevBody := body;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(9.0, y);
- {$ELSE}
- SetValue(bd.position, 9.0, y);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- rjd.Initialize(prevBody, body, MakeVector(6.0, y));
- rjd.motorSpeed := 0.5 * Pi;
- rjd.maxMotorTorque := 2000.0;
- rjd.enableMotor := True;
- rjd.lowerAngle := -0.5 * Pi;
- rjd.upperAngle := 0.5 * Pi;
- rjd.enableLimit := True;
- m_joint2 := Tb2RevoluteJoint(m_world.CreateJoint(rjd));
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-10.0, 10.0);
- {$ELSE}
- SetValue(bd.position, -10.0, 10.0);
- {$ENDIF}
- bd.angle := 0.5 * Pi;
- body := m_world.CreateBody(bd);
- body.CreateShape(sd);
- body.SetMassFromShapes;
- pjd := Tb2PrismaticJointDef.Create;
- pjd.Initialize(ground, body, MakeVector(-10.0, 10.0), MakeVector(1.0, 0.0));
- pjd.motorSpeed := 10.0;
- pjd.maxMotorForce := 1000.0;
- pjd.enableMotor := True;
- pjd.lowerTranslation := 0.0;
- pjd.upperTranslation := 20.0;
- pjd.enableLimit := True;
- m_joint3 := Tb2PrismaticJoint(m_world.CreateJoint(pjd));
- end;
- end;
- procedure TMotorsAndLimits.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Keys: (L) limits, (M) motors, (P) prismatic speed');
- DrawText(Format('Motor Torque = %4.0f, %4.0f : Motor Force = %4.0f', [m_joint1.m_motorForce,
- m_joint2.m_motorForce, m_joint3.m_motorForce]));
- end;
- procedure TMotorsAndLimits.Keyboard(key: Byte);
- begin
- case key of
- 76{L}:
- begin
- m_joint2.LimitEnabled := not m_joint2.LimitEnabled;
- m_joint3.LimitEnabled := not m_joint3.LimitEnabled;
- m_joint2.GetBody1.WakeUp;
- m_joint3.GetBody2.WakeUp;
- end;
- 77{M}:
- begin
- m_joint1.MotorEnabled := not m_joint1.MotorEnabled;
- m_joint2.MotorEnabled := not m_joint2.MotorEnabled;
- m_joint3.MotorEnabled := not m_joint3.MotorEnabled;
- m_joint2.GetBody1.WakeUp;
- m_joint3.GetBody2.WakeUp;
- end;
- 80{P}:
- begin
- m_joint3.GetBody2.WakeUp;
- m_joint3.m_motorSpeed := -m_joint3.m_motorSpeed;
- end;
- end;
- end;
- initialization
- RegisterTestEntry('Motors & Limits', TMotorsAndLimits);
- end.