UCar.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:7k
- unit UCar;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TCar = class(TTester)
- public
- m_leftWheel, m_rightWheel, m_vehicle: Tb2Body;
- m_leftJoint, m_rightJoint: Tb2RevoluteJoint;
- constructor Create; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- procedure Keyboard(key: Byte); override;
- end;
- implementation
- { TCar }
- constructor TCar.Create;
- var
- poly1, poly2: Tb2PolygonDef;
- bd: Tb2BodyDef;
- circ: Tb2CircleDef;
- jd: Tb2RevoluteJointDef;
- box: Tb2PolygonDef;
- begin
- inherited;
- begin // car body
- // bottom half
- poly1 := Tb2PolygonDef.Create;
- poly1.vertexCount := 5;
- {$IFDEF OP_OVERLOAD}
- poly1.vertices[4].SetValue(-2.2,-0.74);
- poly1.vertices[3].SetValue(-2.2,0);
- poly1.vertices[2].SetValue(1.0,0);
- poly1.vertices[1].SetValue(2.2,-0.2);
- poly1.vertices[0].SetValue(2.2,-0.74);
- {$ELSE}
- SetValue(poly1.vertices[4], -2.2,-0.74);
- SetValue(poly1.vertices[3], -2.2,0);
- SetValue(poly1.vertices[2], 1.0,0);
- SetValue(poly1.vertices[1], 2.2,-0.2);
- SetValue(poly1.vertices[0], 2.2,-0.74);
- {$ENDIF}
- poly1.filter.groupIndex := -1;
- poly1.density := 20.0;
- poly1.friction := 0.68;
- poly1.filter.groupIndex := -1;
- // top half
- poly2 := Tb2PolygonDef.Create;
- poly2.vertexCount := 4;
- {$IFDEF OP_OVERLOAD}
- poly2.vertices[3].SetValue(-1.7,0);
- poly2.vertices[2].SetValue(-1.3,0.7);
- poly2.vertices[1].SetValue(0.5,0.74);
- poly2.vertices[0].SetValue(1.0,0);
- {$ELSE}
- SetValue(poly2.vertices[3], -1.7,0);
- SetValue(poly2.vertices[2], -1.3,0.7);
- SetValue(poly2.vertices[1], 0.5,0.74);
- SetValue(poly2.vertices[0], 1.0,0);
- {$ENDIF}
- poly2.filter.groupIndex := -1;
- poly2.density := 5.0;
- poly2.friction := 0.68;
- poly2.filter.groupIndex := -1;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-35.0, 2.8);
- {$ELSE}
- SetValue(bd.position, -35.0, 2.8);
- {$ENDIF}
- m_vehicle := m_world.CreateBody(bd);
- m_vehicle.CreateShape(poly1);
- m_vehicle.CreateShape(poly2);
- m_vehicle.SetMassFromShapes;
- end;
- begin // vehicle wheels
- circ := Tb2CircleDef.Create;
- circ.density := 40.0;
- circ.radius := 0.38608;
- circ.friction := 0.8;
- circ.filter.groupIndex := -1;
- bd := Tb2BodyDef.Create;
- bd.allowSleep := False;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-33.8, 2.0);
- {$ELSE}
- SetValue(bd.position, -33.8, 2.0);
- {$ENDIF}
- m_rightWheel := m_world.CreateBody(bd, False);
- m_rightWheel.CreateShape(circ, False);
- m_rightWheel.SetMassFromShapes;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-36.2, 2.0);
- {$ELSE}
- SetValue(bd.position, -36.2, 2.0);
- {$ENDIF}
- m_leftWheel := m_world.CreateBody(bd);
- m_leftWheel.CreateShape(circ);
- m_leftWheel.SetMassFromShapes;
- end;
- begin // join wheels to chassis
- jd := Tb2RevoluteJointDef.Create;
- jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel.GetWorldCenter);
- jd.collideConnected := False;
- jd.enableMotor := True;
- jd.maxMotorTorque := 10.0;
- jd.motorSpeed := 0.0;
- m_leftJoint := Tb2RevoluteJoint(m_world.CreateJoint(jd, False));
- jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel.GetWorldCenter);
- jd.collideConnected := False;
- m_rightJoint := Tb2RevoluteJoint(m_world.CreateJoint(jd));
- end;
- begin // ground
- box := Tb2PolygonDef.Create;
- box.SetAsBox(19.5, 0.5);
- box.friction := 0.62;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(-25.0, 1.0);
- {$ELSE}
- SetValue(bd.position, -25.0, 1.0);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- begin // more ground
- box := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- box.SetAsBox(9.5, 0.5, b2Vec2_zero, 0.1 * Pi);
- box.friction := 0.62;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(27.0 - 30.0, 3.1);
- {$ELSE}
- SetValue(bd.position, 27.0 - 30.0, 3.1);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- begin // more ground
- box := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- box.SetAsBox(9.5, 0.5, b2Vec2_zero, -0.1 * Pi);
- box.friction := 0.62;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(55.0 - 30.0, 3.1);
- {$ELSE}
- SetValue(bd.position, 55.0 - 30.0, 3.1);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- begin // more ground
- box := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- box.SetAsBox(9.5, 0.5, b2Vec2_zero, 0.03 * Pi);
- box.friction := 0.62;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(41.0, 2.0);
- {$ELSE}
- SetValue(bd.position, 41.0, 2.0);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- begin // more ground
- box := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- box.SetAsBox(5.0, 0.5, b2Vec2_zero, 0.15 * Pi);
- box.friction := 0.62;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(50.0, 4.0);
- {$ELSE}
- SetValue(bd.position, 50.0, 4.0);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- begin // more ground
- box := Tb2PolygonDef.Create;
- bd := Tb2BodyDef.Create;
- box.SetAsBox(20.0, 0.5);
- box.friction := 0.62;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(85.0, 2.0);
- {$ELSE}
- SetValue(bd.position, 85.0, 2.0);
- {$ENDIF}
- m_world.CreateBody(bd).CreateShape(box);
- end;
- end;
- procedure TCar.Step(var settings: TSettings; timeStep: Float);
- begin
- inherited;
- DrawText('Keys: left = a, brake = s, right = d');
- end;
- procedure TCar.Keyboard(key: Byte);
- begin
- case key of
- 65{A}:
- begin
- m_leftJoint.m_maxMotorTorque := 800.0;
- m_leftJoint.m_motorSpeed := 12.0;
- end;
- 83{S}:
- begin
- m_leftJoint.m_maxMotorTorque := 100.0;
- m_leftJoint.m_motorSpeed := 0.0;
- end;
- 68{D}:
- begin
- m_leftJoint.m_maxMotorTorque := 1200.0;
- m_leftJoint.m_motorSpeed := -36.0;
- end;
- end;
- end;
- initialization
- RegisterTestEntry('Car', TCar);
- end.