Exemplo n.º 1
0
		void CreatePhysicsVehicle()
		{
			if( !PhysicsWorld.Instance.IsVehicleSupported )
			{
				Log.Warning( "Car: CreatePhysicsVehicle: Vehicles are not supported by \"{0}\".", PhysicsWorld.Instance.DriverName );
				return;
			}

			//save and restore velocities
			Vec3 lastLinearVelocity = Vec3.Zero;
			Vec3 lastAngularVelocity = Vec3.Zero;
			if( physicsVehicle != null )
			{
				lastLinearVelocity = physicsVehicle.BaseBody.LinearVelocity;
				lastAngularVelocity = physicsVehicle.BaseBody.AngularVelocity;
			}

			DestroyPhysicsVehicle();

			CarType.TireTypeItem tireTypeItem = Type.FindTireTypeItem( TireType );
			if( tireTypeItem == null )
				Log.Fatal( "Car: CreatePhysicsVehicle: Tire type with name \"{0}\" is not defined.", TireType );

			if( PhysicsModel == null )
				Log.Fatal( "Car: CreatePhysicsVehicle: PhysicsModel == null." );
			if( PhysicsModel.Bodies.Length == 0 )
				Log.Fatal( "Car: CreatePhysicsVehicle: PhysicsModel.Bodies.Length == 0." );
			Body baseBody = PhysicsModel.Bodies[ 0 ];
			if( baseBody.Shapes.Length < 5 )
				Log.Fatal( "Car: CreatePhysicsVehicle: baseBody.Shapes.Length < 5." );

			float totalMass = Type.MassChassis + Type.WheelFrontLeft.Mass + Type.WheelFrontRight.Mass +
				Type.WheelRearLeft.Mass + Type.WheelRearRight.Mass;

			if( Math.Abs( baseBody.Mass - totalMass ) > .1f )
				Log.Fatal( "Car: CreatePhysicsVehicle: Specified mass of physics model and masses defined in the car type are not equal. Need set equal masses for physics model and car type settings (MassChassis, wheels Mass properties)." );

			//recreate physics model
			PhysicsModel.PopFromWorld();
			PhysicsModel.PushToWorld();

			//Create vehicle
			physicsVehicle = PhysicsWorld.Instance.MainScene.CreateVehicle( baseBody );

			//Chassis mass
			physicsVehicle.InitData.MassChassis = Type.MassChassis;

			//Differential
			physicsVehicle.InitData.DifferentialType = Type.DifferentialType;
			physicsVehicle.InitData.DifferentialFrontRearSplit = Type.DifferentialFrontRearSplit;
			physicsVehicle.InitData.DifferentialFrontLeftRightSplit = Type.DifferentialFrontLeftRightSplit;
			physicsVehicle.InitData.DifferentialRearLeftRightSplit = Type.DifferentialRearLeftRightSplit;
			physicsVehicle.InitData.DifferentialCenterBias = Type.DifferentialCenterBias;
			physicsVehicle.InitData.DifferentialFrontBias = Type.DifferentialFrontBias;
			physicsVehicle.InitData.DifferentialRearBias = Type.DifferentialRearBias;

			//Engine
			physicsVehicle.InitData.EnginePeakTorque = Type.EnginePeakTorque;
			physicsVehicle.InitData.EngineMaxRPM = Type.EngineMaxRPM;
			physicsVehicle.InitData.EngineDampingRateFullThrottle = Type.EngineDampingRateFullThrottle;
			physicsVehicle.InitData.EngineDampingRateZeroThrottleClutchEngaged = Type.EngineDampingRateZeroThrottleClutchEngaged;
			physicsVehicle.InitData.EngineDampingRateZeroThrottleClutchDisengaged = Type.EngineDampingRateZeroThrottleClutchDisengaged;
			foreach( CarType.EngineTorqueCurveItem item in Type.EngineTorqueCurve )
			{
				physicsVehicle.InitData.EngineTorqueCurve.Add( new PhysicsVehicle.InitDataClass.EngineTorqueCurveItem(
					item.NormalizedTorque, item.NormalizedRev ) );
			}

			//Gears
			foreach( CarType.GearItem gearItem in Type.Gears )
				physicsVehicle.InitData.Gears.Add( new PhysicsVehicle.InitDataClass.GearItem( gearItem.Number, gearItem.Ratio ) );
			physicsVehicle.InitData.GearsSwitchTime = Type.GearsSwitchTime;

			//Clutch
			physicsVehicle.InitData.ClutchStrength = Type.ClutchStrength;

			//Ackermann steer accuracy
			physicsVehicle.InitData.AckermannSteerAccuracy = Type.AckermannSteerAccuracy;

			//Wheels
			UpdateWheelSettings( physicsVehicle.InitData.WheelFrontLeft, Type.WheelFrontLeft, tireTypeItem.WheelFrontLeft );
			UpdateWheelSettings( physicsVehicle.InitData.WheelFrontRight, Type.WheelFrontRight, tireTypeItem.WheelFrontRight );
			UpdateWheelSettings( physicsVehicle.InitData.WheelRearLeft, Type.WheelRearLeft, tireTypeItem.WheelRearLeft );
			UpdateWheelSettings( physicsVehicle.InitData.WheelRearRight, Type.WheelRearRight, tireTypeItem.WheelRearRight );

			//Tire settings
			foreach( CarType.TireTypeItem.FrictionMultiplierItem frictionItem in tireTypeItem.FrictionMultipliers )
			{
				physicsVehicle.InitData.TireFrictionMultipliers.Add( new PhysicsVehicle.InitDataClass.TireFrictionMultiplierItem(
					frictionItem.SurfaceMaterialName, frictionItem.Value ) );
			}

			//create vehicle on physics engine
			physicsVehicle.PushedToWorld = true;

			//update dynamic settings
			physicsVehicle.AutoGear = autoGear;

			physicsVehicle.BaseBody.LinearVelocity = lastLinearVelocity;
			physicsVehicle.BaseBody.AngularVelocity = lastAngularVelocity;

			//suspend before first call OnTick()
			physicsVehicle.EnableUpdate = false;
		}
Exemplo n.º 2
0
		void DestroyPhysicsVehicle()
		{
			if( physicsVehicle != null )
			{
				physicsVehicle.Dispose();
				physicsVehicle = null;
			}
		}
Exemplo n.º 3
0
		void UpdateWheelSettings( PhysicsVehicle.InitDataClass.WheelItem destination, CarType.WheelItem source,
			CarType.TireTypeItem.TireWheelItem tireSource )
		{
			destination.Mass = source.Mass;

			destination.WheelDampingRate = source.WheelDampingRate;
			destination.WheelMaxBrakeTorque = source.WheelMaxBrakeTorque;
			destination.WheelMaxHandBrakeTorque = source.WheelMaxHandBrakeTorque;
			destination.WheelMaxSteer = source.WheelMaxSteer;
			destination.WheelToeAngle = source.WheelToeAngle;
			destination.SuspensionSpringStrength = source.SuspensionSpringStrength;
			destination.SuspensionSpringDamperRate = source.SuspensionSpringDamperRate;
			destination.SuspensionMaxCompression = source.SuspensionMaxCompression;
			destination.SuspensionMaxDroop = source.SuspensionMaxDroop;
			destination.SuspensionSprungMassCoefficient = source.SuspensionSprungMassCoefficient;
			destination.SuspensionForceApplicationPointOffset = source.SuspensionForceApplicationPointOffset;
			destination.TireForceApplicationPointOffset = source.TireForceApplicationPointOffset;

			destination.TireLatStiffX = tireSource.TireLatStiffX;
			destination.TireLatStiffY = tireSource.TireLatStiffY;
			destination.TireLongitudinalStiffnessPerUnitGravity = tireSource.TireLongitudinalStiffnessPerUnitGravity;
			destination.TireCamberStiffness = tireSource.TireCamberStiffness;
			destination.FrictionVsSlipGraphZeroLongitudinalSlip = tireSource.FrictionVsSlipGraphZeroLongitudinalSlip;
			destination.FrictionVsSlipGraphLongitudinalSlipWithMaximumFriction = tireSource.FrictionVsSlipGraphLongitudinalSlipWithMaximumFriction;
			destination.FrictionVsSlipGraphMaximumFriction = tireSource.FrictionVsSlipGraphMaximumFriction;
			destination.FrictionVsSlipGraphEndPointOfGraph = tireSource.FrictionVsSlipGraphEndPointOfGraph;
			destination.FrictionVsSlipGraphValueOfFrictionForSlipsGreaterThanEndPointOfGraph = tireSource.FrictionVsSlipGraphValueOfFrictionForSlipsGreaterThanEndPointOfGraph;
		}