USliderCrank.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:4k
- unit USliderCrank;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TSliderCrank = class(TTester)
- public
- m_joint1: Tb2RevoluteJoint;
- m_joint2: Tb2PrismaticJoint;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TSliderCrank }
- constructor TSliderCrank.Create;
- var
- ground, prevBody, body: 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
- // Define crank.
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(0.5, 2.0);
- sd.density := 1.0;
- rjd := Tb2RevoluteJointDef.Create;
- prevBody := ground;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 7.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 7.0);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- rjd.Initialize(prevBody, body, MakeVector(0.0, 5.0));
- rjd.motorSpeed := 1.0 * Pi;
- rjd.maxMotorTorque := 10000.0;
- rjd.enableMotor := True;
- m_joint1 := Tb2RevoluteJoint(m_world.CreateJoint(rjd, False));
- prevBody := body;
- // Define follower.
- sd.SetAsBox(0.5, 4.0);
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 13.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 13.0);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- rjd.Initialize(prevBody, body, MakeVector(0.0, 9.0));
- rjd.enableMotor := False;
- m_world.CreateJoint(rjd, False);
- prevBody := body;
- // Define piston
- sd.SetAsBox(1.5, 1.5);
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 17.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 17.0);
- {$ENDIF}
- body := m_world.CreateBody(bd, False);
- body.CreateShape(sd, False);
- body.SetMassFromShapes;
- rjd.Initialize(prevBody, body, MakeVector(0.0, 17.0));
- m_world.CreateJoint(rjd);
- pjd := Tb2PrismaticJointDef.Create;
- pjd.Initialize(ground, body, MakeVector(0.0, 17.0), MakeVector(0.0, 1.0));
- pjd.maxMotorForce := 1000.0;
- pjd.enableMotor := True;
- m_joint2 := Tb2PrismaticJoint(m_world.CreateJoint(pjd));
- // Create a payload
- sd.density := 2.0;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, 23.0);
- {$ELSE}
- SetValue(bd.position, 0.0, 23.0);
- {$ENDIF}
- body := m_world.CreateBody(bd);
- body.CreateShape(sd);
- body.SetMassFromShapes;
- end;
- end;
- procedure TSliderCrank.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Keys: (f) toggle friction, (m) toggle motor');
- DrawText(Format('Motor Torque = %5.0f', [m_joint1.MotorTorque]));
- end;
- procedure TSliderCrank.Keyboard(key: Byte);
- begin
- case key of
- 70{F}:
- begin
- m_joint2.m_enableMotor := not m_joint2.m_enableMotor;
- m_joint2.GetBody2.WakeUp;
- end;
- 77{M}:
- begin
- m_joint1.m_enableMotor := not m_joint1.m_enableMotor;
- m_joint1.GetBody2.WakeUp;
- end;
- end;
- end;
- initialization
- RegisterTestEntry('Slider Crank', TSliderCrank);
- end.