vehicle wheels falling behind / gravity acting strange

Post Reply
b0gd4n
Posts: 14
Joined: Thu Nov 07, 2013 2:38 am

vehicle wheels falling behind / gravity acting strange

Post by b0gd4n » Sun Nov 24, 2013 5:55 pm

Hey guys.

I have created a bullet vehicle with a attached turret at the top like this:

Code: Select all

	gEngineForce = 0.f;
	gBreakingFrontForce = 0.f;
	gBreakingBackForce = 0.f;

	maxEngineForce = 20000.f;
	minEngineForce = -2000.f;
	maxBreakingFrontForce = 4000.f;
	maxBreakingBackForce = 600.f;

	gVehicleSteering = 0.f;
	steeringIncrement = 0.002f;
	steeringClamp = 0.6f;
	wheelRadius = 0.5f;
	wheelWidth = 0.4f;
	wheelFriction = 50;
	suspensionStiffness = 10.f;
	suspensionDamping = 1.3f;
	suspensionCompression = 4.4f;
	rollInfluence = 0.1f;//1.0f

	btScalar suspensionRestLength(0.6);
	btVector3 wheelDirectionCS0(0, -1, 0);
	btVector3 wheelAxleCS(-1, 0, 0);




	btTransform tr;
	tr.setIdentity();

	chassisShape = new btBoxShape(btVector3(1.f, 0.5f, 2.f));
	bulletCollisionShapes.push_back(chassisShape);

	compound = new btCompoundShape();
	bulletCollisionShapes.push_back(compound);

	btTransform chassisTrans;
	chassisTrans.setIdentity();
	//localTrans effectively shifts the center of mass with respect to the chassis
	chassisTrans.setOrigin(btVector3(0, 1.3, 0));
	compound->addChildShape(chassisTrans, chassisShape);

	tr.setOrigin(btVector3(0, 0.f, 0));
	//m_carChassis = CreateRigidBody(2000, tr, compound);
	//m_carChassis->setDamping(0.2,0.2);
	m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth, wheelRadius, wheelRadius));

	m_carChassis = CreateRigidBody(2000, tr, compound);


	// create turret
	turretShape = new btBoxShape(btVector3(0.4f, 0.2f, 0.8f));
	bulletCollisionShapes.push_back(turretShape);
	btTransform turretTrans;
	turretTrans.setIdentity();
	turretTrans.setOrigin(btVector3(0.0f, 0.7f, 0.0f));
	turretBody = CreateRigidBody(1, turretTrans, turretShape);

	// add some data to build constraint frames
	btVector3 axisA(0.f, 1.f, 0.f);
	btVector3 axisB(0.f, 0.f, 0.f);
	btVector3 pivotA(0.f, 1.f, 0.f);
	btVector3 pivotB(0.f, 0.f, 0.f);
	hinge = new btHingeConstraint(*m_carChassis, *turretBody, pivotA, pivotB, axisA, axisB);
	//hinge->setLimit(-SIMD_HALF_PI * 0.5f, SIMD_HALF_PI * 0.5f);
	hinge->enableAngularMotor(true, 0, 1);

	// add constraint to world
	bt_dynamicsWorld->addConstraint(hinge, true);

	{
		btTransform something;
		something.setIdentity();
		something.setOrigin(btVector3(0, 10, 0));
		m_carChassis->setWorldTransform(something);

		m_vehicleRayCaster = new btDefaultVehicleRaycaster(bt_dynamicsWorld);
		m_vehicle = new btRaycastVehicle(m_tuning, m_carChassis, m_vehicleRayCaster);

		m_carChassis->setActivationState(DISABLE_DEACTIVATION);

		bt_dynamicsWorld->addVehicle(m_vehicle);

		float connectionHeight = 1.2f;


		//choose coordinate system
		m_vehicle->setCoordinateSystem(rightIndex, upIndex, forwardIndex);

		bool isFrontWheel = true;
		btVector3 connectionPointCS0(1 - (-0.8*wheelWidth), connectionHeight, 3 * 1 - wheelRadius);
		m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);

		connectionPointCS0 = btVector3(-1 + (-0.8*wheelWidth), connectionHeight, 3 * 1 - wheelRadius);
		m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);

		isFrontWheel = false;
		connectionPointCS0 = btVector3(-1 + (-0.8*wheelWidth), connectionHeight, -3 * 1 + wheelRadius);
		m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);

		connectionPointCS0 = btVector3(1 - (-0.8*wheelWidth), connectionHeight, -3 * 1 + wheelRadius);
		m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);

		for (int i = 0; i < m_vehicle->getNumWheels(); i++)
		{
			btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
			wheel.m_suspensionStiffness = suspensionStiffness;
			wheel.m_wheelsDampingRelaxation = suspensionDamping;
			wheel.m_wheelsDampingCompression = suspensionCompression;
			wheel.m_frictionSlip = wheelFriction;
			wheel.m_rollInfluence = rollInfluence;
		}

	int wheelIndex = 2;
	m_vehicle->applyEngineForce(gEngineForce, wheelIndex);
	m_vehicle->setBrake(gBreakingBackForce, wheelIndex);
	wheelIndex = 3;
	m_vehicle->applyEngineForce(gEngineForce, wheelIndex);
	m_vehicle->setBrake(gBreakingBackForce, wheelIndex);

	wheelIndex = 0;
	m_vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
	m_vehicle->setBrake(gBreakingFrontForce, wheelIndex);
	wheelIndex = 1;
	m_vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
	m_vehicle->setBrake(gBreakingFrontForce, wheelIndex);
	}

This is how I render the vehicle:

Code: Select all

	// render wheels
	btScalar mwheel[16];
	for (int i = 0; i<m_vehicle->getNumWheels(); i++){
		//synchronize the wheels with the (interpolated) chassis worldtransform
		m_vehicle->updateWheelTransform(i, true);
		//draw wheels
		m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(mwheel);
		RenderWheel(m_wheelShape, mwheel);
	}


	// render car chassis
	btScalar mchassis[16];
	m_vehicle->getChassisWorldTransform().getOpenGLMatrix(mchassis);
	RenderBox(chassisShape, mchassis);


	// render turret
	btScalar mturret[16];
	// get chassis and turret transforms
	btTransform chassisTransform = m_vehicle->getChassisWorldTransform();
	//btTransform turretTransform = compound->getChildTransform(1);
	btTransform turretTransform = turretBody->getWorldTransform();
	// multiply transforms to get updated turret transform
	//turretTransform *= chassisTransform;
	turretTransform.getOpenGLMatrix(mturret);
	RenderBox(turretShape, mturret);


Using the arrow keys I apply break or acceleration forces like this:

Code: Select all

		bullet.gEngineForce = bullet.maxEngineForce;
		bullet.gBreakingFrontForce = 0.f;
		bullet.gBreakingBackForce = 0.f;
etc.



The camera is attached to the turret on top of the car, therefore attached to the car itself.

The issues I have now is when the car moves relatively fast the wheels appear to stutter and fall behind the car's chassis.

I don't think there is a better way of demonstrating this than using a video: https://www.dropbox.com/s/e1aur7hwxrtn1 ... -05-39.avi




EDIT:

OK, I figured out what the stuttering problem was. It had to do with the fact that I did not have a fixed frame rate. I now have it set to 60FPS and that problem is solved.

However, the fact that the wheels get left behind as you move faster is still there. Here's the update in video: https://www.dropbox.com/s/x5y9w6idivaot ... -54-03.avi



EDIT 2:

I have increased the gravity now and I have noticed another strange behaviour: when I accelerate, the chassis pitches forward and when I brake it goes backwards. This is obviously wrong as it should be the other way around. Same happens when turning left, the car pitches left instead of right, and the other way around.

I have a video trying to show this behaviour here: https://www.dropbox.com/s/corbixp2rnv39 ... -42-89.avi

NotCamelCase
Posts: 2
Joined: Thu Dec 26, 2013 8:44 pm

Re: vehicle wheels falling behind / gravity acting strange

Post by NotCamelCase » Sat Mar 29, 2014 1:27 am

Were you able to solve the problem that wheels get left behind as the vehicle moves fast as if running late ? I'm facing the same issue but couldn't figure it out yet.

nutmmm
Posts: 1
Joined: Wed Apr 02, 2014 6:18 am

Re: vehicle wheels falling behind / gravity acting strange

Post by nutmmm » Wed Apr 02, 2014 6:22 am

Hey, the answer may be that when you stick the wheels to the body (or the body to the wheels however you did it) kinda hard to explain, I had a simlar problem and I had a function that stuck the camera an object and the object would get away from the camera and i had to change the position of that function in the main loop maybe even duplicate the function, just try messing around till something works ;)
hope that helps.

PcChip
Posts: 31
Joined: Sun May 20, 2018 3:09 pm

Re: vehicle wheels falling behind / gravity acting strange

Post by PcChip » Tue Jun 19, 2018 1:54 am

edit: 7/4/2018 - this post was wrong, so I'm editing this in case any future person is here looking for a fix

AFTER you call StepSimulation, and you're ready to draw your vehicle, do the following:

Code: Select all

vehicle->getRigidBody()->getMotionState()->getWorldTransform(buggyPosition);  
vehicle->updateWheelTransform(0, true);
vehicle->updateWheelTransform(1, true);
vehicle->updateWheelTransform(2, true);
vehicle->updateWheelTransform(3, true);  

then you can use buggyPosition to convert to a 4x4matrix and send it to the GPU

then you can do the following for the wheels:

Code: Select all

		thisWheelPosition = vehicle->getWheelTransformWS(0);
		buggyRenderMatrix = bulletToGlm(thisWheelPosition);
		shader.setMat4("model", buggyRenderMatrix);
		wheelModel->render();
		thisWheelPosition = vehicle->getWheelTransformWS(1);
		buggyRenderMatrix = bulletToGlm(thisWheelPosition);
		shader.setMat4("model", buggyRenderMatrix);
		wheelModel->render();
		thisWheelPosition = vehicle->getWheelTransformWS(2);
		buggyRenderMatrix = bulletToGlm(thisWheelPosition);
		shader.setMat4("model", buggyRenderMatrix);
		wheelModel->render();
		thisWheelPosition = vehicle->getWheelTransformWS(3);
		buggyRenderMatrix = bulletToGlm(thisWheelPosition);
		shader.setMat4("model", buggyRenderMatrix);
		wheelModel->render();

Post Reply