from about two days i'm trying to setup a vehicle simulation,my objective is obtain a fast and stable car.
I've managed to setup the vehicle and the environment, i've also obtain the car to move and steer but when i make steer the car, the car always tend to go on two wheels. i've tryed playing with the mass and the suspension values but with no success.
also i'm making accelerate the front wheel to simulate front traction and i've noticed two problems when it brakes the car rear go up, and in backward it seems to reach faster the top speed.
the car is a dodge charger so it has high setup from ground.
if someone could help i would be very grateful.
i post the code for the setupping of the car
Code: Select all
var bodies = new List<CompoundShapeEntry>()
{
new CompoundShapeEntry(new BoxShape(7.45f, 2.83f, 16.5f), new Vector3(0, 0.0f, 0), 400),
new CompoundShapeEntry(new BoxShape(7.5f, 1.14f, 6), new Vector3(0, 1.965f, 1.477f), 350)
};
CompoundBody vehiclebody=new CompoundBody(bodies,750);
vehiclebody.Material.Bounciness = 0.2f;
vehiclebody.Material.KineticFriction = 1;
vehiclebody.Material.StaticFriction = 1;
vehicle = new Vehicle(vehiclebody);
#region RaycastWheelShapes
//The wheel model used is not aligned initially with how a wheel would normally look, so rotate them.
Matrix wheelGraphicRotation = Matrix.CreateFromAxisAngle(Vector3.Forward, MathHelper.PiOver2);
vehicle.AddWheel(new Wheel(
new RaycastWheelShape(1.5f, wheelGraphicRotation),
new WheelSuspension(1000, 200, Vector3.Down, 1f, new Vector3(-2.737f, -2f, 4.949f)),
new WheelDrivingMotor(22, 60000, 10000),
new WheelBrake(4, 6, .01f),
new WheelSlidingFriction(8,10)));
vehicle.AddWheel(new Wheel(
new RaycastWheelShape(1.5f, wheelGraphicRotation),
new WheelSuspension(1000, 200, Vector3.Down, 1f, new Vector3(2.737f, -2f, 4.949f)),
new WheelDrivingMotor(22, 60000, 10000),
new WheelBrake(4, 6, .01f),
new WheelSlidingFriction(8, 10)));
vehicle.AddWheel(new Wheel(
new RaycastWheelShape(1.5f, wheelGraphicRotation),
new WheelSuspension(1000, 200, Vector3.Down, 1f, new Vector3(-2.737f, -2f, -4.895f)),
new WheelDrivingMotor(22, 60000, 10000),
new WheelBrake(4, 6, .01f),
new WheelSlidingFriction(8, 10)));
vehicle.AddWheel(new Wheel(
new RaycastWheelShape(1.5f, wheelGraphicRotation),
new WheelSuspension(1000, 200, Vector3.Down, 1f, new Vector3(2.737f, -2f, -4.895f)),
new WheelDrivingMotor(22, 60000, 10000),
new WheelBrake(4, 6, .01f),
new WheelSlidingFriction(8, 10)));
Code: Select all
if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Y))
{
vehicle.Body.WorldTransform = Matrix.CreateFromYawPitchRoll(0, 0, 0);
vehicle.Body.Position = Vector3.Zero + (Vector3.Down * 90);
}
if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A))
{
vehicle.Wheels[0].Brake.IsBraking = true;
vehicle.Wheels[1].Brake.IsBraking = true;
vehicle.Wheels[2].Brake.IsBraking = true;
vehicle.Wheels[3].Brake.IsBraking = true;
}
if (GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.A))
{
vehicle.Wheels[0].Brake.IsBraking = false;
vehicle.Wheels[1].Brake.IsBraking = false;
vehicle.Wheels[2].Brake.IsBraking = false;
vehicle.Wheels[3].Brake.IsBraking = false;
}
if (GamePad.GetState(PlayerIndex.One).Triggers.Left < 0.3f && GamePad.GetState(PlayerIndex.One).Triggers.Right < 0.3f)
{
targetspeed = 0;
}
if (GamePad.GetState(PlayerIndex.One).Triggers.Right > 0.3f)
{
targetspeed = 60000 * GamePad.GetState(PlayerIndex.One).Triggers.Right;
}
if (GamePad.GetState(PlayerIndex.One).Triggers.Left > 0.3f)
{
targetspeed = -10000 * GamePad.GetState(PlayerIndex.One).Triggers.Left;
}
targetrotation = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * ((float)MathHelper.PiOver4 + ((float)MathHelper.PiOver4 / 4));
if (vehicle.Wheels[2].DrivingMotor.TargetSpeed != targetspeed || vehicle.Wheels[3].DrivingMotor.TargetSpeed != targetspeed)
{
if (vehicle.Wheels[2].DrivingMotor.TargetSpeed < targetspeed)
{
vehicle.Wheels[2].DrivingMotor.TargetSpeed += gameTime.ElapsedGameTime.Milliseconds * 0.1f;
vehicle.Wheels[3].DrivingMotor.TargetSpeed += gameTime.ElapsedGameTime.Milliseconds * 0.1f;
}
if (vehicle.Wheels[2].DrivingMotor.TargetSpeed > targetspeed)
{
vehicle.Wheels[2].DrivingMotor.TargetSpeed -= gameTime.ElapsedGameTime.Milliseconds * 0.1f;
vehicle.Wheels[3].DrivingMotor.TargetSpeed += gameTime.ElapsedGameTime.Milliseconds * 0.1f;
}
}
if (vehicle.Wheels[2].Shape.SteeringAngle != targetrotation && vehicle.Wheels[3].Shape.SteeringAngle != targetrotation)
{
if (vehicle.Wheels[2].Shape.SteeringAngle < targetrotation)
{
vehicle.Wheels[2].Shape.SteeringAngle += gameTime.ElapsedGameTime.Milliseconds * 0.0025f;
MathHelper.Clamp(vehicle.Wheels[2].Shape.SteeringAngle, 0, targetrotation);
vehicle.Wheels[3].Shape.SteeringAngle = vehicle.Wheels[2].Shape.SteeringAngle;
}
if (vehicle.Wheels[2].Shape.SteeringAngle > targetrotation)
{
vehicle.Wheels[2].Shape.SteeringAngle -= gameTime.ElapsedGameTime.Milliseconds * 0.0025f;
MathHelper.Clamp(vehicle.Wheels[2].Shape.SteeringAngle, targetrotation, 0);
vehicle.Wheels[3].Shape.SteeringAngle = vehicle.Wheels[2].Shape.SteeringAngle;
}
}