Sign in to follow this  
Xoliul

Tokamak car physics

Recommended Posts

Okay, I'm new here, pretty serious question to begin with. We're starting on a game project for school, which will be utilizing Tokamak physics? You'll be driving a car, so I need to do the car physics. I'll have Tokamak handle collision and integration of forces over time, and do the tire and suspension code myself, starting with the car example code that is available on their site. Now thing is, I want to fully understand this code before I start using and modifying it myself, yet I have some trouble with it. Here's what I have so far (posting all of the car control code so it's understandable):
void CSampleCar::CarController(neRigidBodyController * controller)
{
	neT3 body2World = carRigidBody->GetTransform();

	carRigidBody->BeginIterateSensor();

	neSensor * sn;

	neV3 force, torque;

	force.SetZero();

	torque.SetZero();

	s32 i = 0;

	while (sn = carRigidBody->GetNextSensor())
	{
		f32 k = 6.0f; //spring constant

		f32 u = 0.8f; //damping constant

		if (i == 2 || i == 3)
		{
			k = 10.0f;
		}

		// add spring force

		f32 depth = sn->GetDetectDepth();

		//gw.car.suspensionLength[i] = 1.0f - depth;
		suspensionLength[i] = 1.0f - depth;

		if (depth == 0.0f)
		{
			i++;
			continue;
		}
		//if (depth > 0.7f)
		//	depth = 0.7f;
		
		neV3 groundNormal = sn->GetDetectNormal();

		neV3 lineNormal = body2World.rot * sn->GetLineUnitVector();

		neV3 linePos = body2World * sn->GetLinePos();

		f32 dot = lineNormal.Dot(groundNormal) * -1.0f; //apparently unused ?

		//if (dot <= 0.7f)
		//	continue;


		neV3 f = depth * lineNormal * -k ;//* dot; //get spring push force: force along normal, strength from spring compression (inverse)

		force += f;//add spring force to total wheel force vector

		neV3 r = linePos - carRigidBody->GetPos();//get normalised wheel offset

		torque += r.Cross(f);//add normal of spring force and normalised wheel offset to torque vector -> torque vector serves for 

		// add damping force
		
		//GetVelocityAtPoint most probably requires a relative value from the rigid body's center
		f32 speed = carRigidBody->GetVelocityAtPoint(r).Dot(lineNormal); //dot product of linenormal and velocity at relative wheel offset for scalar speed value
		//speed is the car bodies speed along the sensor line's normal vector

		f = -speed * lineNormal * u; //inverse speed along sensor, dampened by dampening constant, along sensor line's normal.

		force += f; //add dampening force to suspension force

		torque += r.Cross(f);//apply dampening torque

		// add friction force

		neV3 vel = carRigidBody->GetVelocityAtPoint( sn->GetDetectContactPoint() - carRigidBody->GetPos() ); //get velocity at wheel/ground contactpoint

		vel.RemoveComponent(groundNormal);

		//RemoveComponent function code, copied from library source
		/*	RemoveComponent	(const neV3& V)
		{
			f32 dot = (*this).Dot(V);

			(*this) = (*this) - V * dot;
		}
		*/

		if (i == 0 || i == 1)
		{
			//steering

			vel.RemoveComponent(steerDir);
		}
		else
		{
			vel.RemoveComponent(body2World.rot[0]); //rear wheel always parallel to car body
		}


		f = vel;

		f.Normalize();

		f *= -2.0f;

		if (i == 2 || i ==3)
		{
			f *= (1.0f - slide);
		}
		f[1] = 0.0f;
		
		force += f;

		r = sn->GetDetectContactPoint() - carRigidBody->GetPos();

		torque += r.Cross(f);

		// driving force

		if (i == 2 || i == 3) // rear wheel
		{
			f = body2World.rot[0];

			f *= accel;

			force += f;

			torque += r.Cross(f);
		}
/*
		gw.car.displayLines[i][0] = linePos;

		gw.car.displayLines[i][1] = sn->GetDetectContactPoint();
*/
		i++;
	}

	// drag
	f32 dragConstant = 0.5f;

	neV3 vel = carRigidBody->GetVelocity();

	f32 dot = vel.Dot(body2World.rot[0]);

	neV3 drag = dot * body2World.rot[0] * -dragConstant;

	force += drag;

	controller->SetControllerForce(force);

	controller->SetControllerTorque(torque);
}

Where I'm running stuck in trying to understand, is as soon as they start to use the neV3.RemoveComponent() function. I don't understand what excatly the result of this function is supposed to be, and I don't understand how these functions will create the friction forces. Also, why do they normalize the velocity vector afterwards ? I really want to understand the friction and grip part here, as this is where the demo is lacking most of all: the friction model will need a lot more elaboration in our project, so naturally I want to fully understand it before I start changing it. Hope somebody can help me out here, as I really don't have anybody I can ask this. [Edited by - Xoliul on February 20, 2008 2:18:12 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this