Finding out if car is sideways

So I’ve recently delved into the world of vehicle physics. And I’m writing a 2D “tech” demo to make sure the physics are to my liking before moving the physics into my 3D rasterizer. Now, I love drifting and I need to find a way to determine whether the vehicle is sideways while it is moving. I thought of this concept:

KEY:
The yellow rectangle is the car, the black lines next to it are the wheels, the wheels closest to the bottom of the image are the front wheels of the car.
The red pair of numbers are the values of the velocity vector and the position vector added up, and a red line is drawn from the position to the velocity vector to represent the force.
The darker blue lines represent the borders to what I consider to be the “front” and “back” of the car.
The lighter blue areas simply represent the areas in which the front and the back of the car would be.

Now my question is how can I determine whether or not the velocity vector’s components are within the blue areas. Keep in mind that the car rotates lol

How do you represent the orientation?
You could for example use the dot product to evaluate the angle between the ‘front vector’ (showing to the front :persecutioncomplex:) and the velocity.

-ClaasJG

This is some simple code I used (where body is the car).


   /*
	 * Kill the literal velocity
	 */
	void updateFriction(Body body)
	{
		Vector2 impulse = getLiteralVelocity(body).scl(-0.1f*body.getMass()/maxSpeed*getSpeed());
		body.applyLinearImpulse(impulse, body.getWorldCenter(), true);
	}
	
	Vector2 getLiteralVelocity(Body body)
	{
		Vector2 currentRightNormal = body.getWorldVector(new Vector2(0, 1));
		return currentRightNormal.scl(currentRightNormal.dot(body.getLinearVelocity()));
	}

getWorldVector returns the vector from the car’s perspective, so you want to kill vertical velocity (car’s perspective) although it would be quite handy for parallel parking :slight_smile:

I followed this tutorial (implemented in Box2D) but can be applied everywhere since it’s about car physics:
http://www.iforce2d.net/b2dtut/top-down-car

EDIT: It also explains an easy way to get your car drifting.

Thank you both, however I pulled an all-nighter and figured out how to do it. Basically, I define a rectangle with the vertices of the rectangle being the two front points of the car and two points ahead of the car but parallel to the car’s body. Then I check to see if the velocity vector is located within the rectangle. :slight_smile:

Cheers.