UGears.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:4k
- unit UGears;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TGears = class(TTester)
- public
- m_joint1, m_joint2: Tb2RevoluteJoint;
- m_joint3: Tb2PrismaticJoint;
- m_joint4, m_joint5: Tb2GearJoint;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- end;
- implementation
- { TGears }
- constructor TGears.Create;
- var
- ground, body1, body2, body3: Tb2Body;
- bd, bd1, bd2, bd3: Tb2BodyDef;
- sd: Tb2PolygonDef;
- circle1, circle2: Tb2CircleDef;
- box: Tb2PolygonDef;
- jd1, jd2: Tb2RevoluteJointDef;
- jd3: Tb2PrismaticJointDef;
- jd4, jd5: Tb2GearJointDef;
- begin
- inherited;
- 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);
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0);
- ground.CreateShape(sd);
- circle1 := Tb2CircleDef.Create;
- circle1.radius := 1.0;
- circle1.density := 5.0;
- circle2 := Tb2CircleDef.Create;
- circle2.radius := 2.0;
- circle2.density := 5.0;
- box := Tb2PolygonDef.Create;
- box.SetAsBox(0.5, 5.0);
- box.density := 5.0;
- bd1 := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd1.position.SetValue(-3.0, 12.0);
- {$ELSE}
- SetValue(bd1.position, -3.0, 12.0);
- {$ENDIF}
- body1 := m_world.CreateBody(bd1);
- body1.CreateShape(circle1, False);
- body1.SetMassFromShapes;
- jd1 := Tb2RevoluteJointDef.Create;
- jd1.body1 := ground;
- jd1.body2 := body1;
- jd1.localAnchor1 := ground.GetLocalPoint(bd1.position);
- jd1.localAnchor2 := body1.GetLocalPoint(bd1.position);
- jd1.referenceAngle := body1.GetAngle - ground.GetAngle;
- m_joint1 := Tb2RevoluteJoint(m_world.CreateJoint(jd1));
- bd2 := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd2.position.SetValue(0.0, 12.0);
- {$ELSE}
- SetValue(bd2.position, 0.0, 12.0);
- {$ENDIF}
- body2 := m_world.CreateBody(bd2);
- body2.CreateShape(circle2, False);
- body2.SetMassFromShapes;
- jd2 := Tb2RevoluteJointDef.Create;
- jd2.Initialize(ground, body2, bd2.position);
- m_joint2 := Tb2RevoluteJoint(m_world.CreateJoint(jd2));
- bd3 := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd3.position.SetValue(2.5, 12.0);
- {$ELSE}
- SetValue(bd3.position, 2.5, 12.0);
- {$ENDIF}
- body3 := m_world.CreateBody(bd3);
- body3.CreateShape(box);
- body3.SetMassFromShapes;
- jd3 := Tb2PrismaticJointDef.Create;
- jd3.Initialize(ground, body3, bd3.position, MakeVector(0.0, 1.0));
- jd3.lowerTranslation := -5.0;
- jd3.upperTranslation := 5.0;
- jd3.enableLimit := True;
- m_joint3 := Tb2PrismaticJoint(m_world.CreateJoint(jd3));
- jd4 := Tb2GearJointDef.Create;
- jd4.body1 := body1;
- jd4.body2 := body2;
- jd4.joint1 := m_joint1;
- jd4.joint2 := m_joint2;
- jd4.ratio := circle2.radius / circle1.radius;
- m_joint4 := Tb2GearJoint(m_world.CreateJoint(jd4));
- jd5 := Tb2GearJointDef.Create;
- jd5.body1 := body2;
- jd5.body2 := body3;
- jd5.joint1 := m_joint2;
- jd5.joint2 := m_joint3;
- jd5.ratio := -1.0 / circle2.radius;
- m_joint5 := Tb2GearJoint(m_world.CreateJoint(jd5));
- circle1.Free;
- circle2.Free;
- end;
- procedure TGears.Step(var settings: TSettings; timeStep: Float);
- var
- value: Float;
- begin
- inherited;
- value := m_joint1.GetJointAngle + m_joint4.GetRatio * m_joint2.GetJointAngle;
- DrawText(Format('theta1 + %4.2f * theta2 := %4.2f', [m_joint4.GetRatio, value]));
- value := m_joint2.GetJointAngle + m_joint5.GetRatio * m_joint3.GetJointTranslation;
- DrawText(Format('theta2 + %4.2f * delta := %4.2f', [m_joint5.GetRatio, value]));
- end;
- initialization
- RegisterTestEntry('Gears', TGears);
- end.