Rotational Velocity, Quaternions, Vector3 & Transfrom.rotation - Help

Hi,

I’m trying to better my understanding of programming physics so I actually understand what Rigidbody is doing which is why I’m not using Rigidbody.

I’m moving the player based on force and velocity and I’m trying to do the same for rotation.

I’m calculating the angular velocity but am having trouble passing that into the transform. This comes down to my understanding of it. I know I’m trying to pass in a vector3 to a Quaternion which is like a no no but I can’t find anything to work which makes me think I’m going about it all wrong.

    private Vector3 deltaYaw;
	private Vector3 deltaPitch;
	private Vector3 deltaRoll;
	private Vector3 rotationVelocity;

	private Vector3 rotationLast;
	private Vector3 rotationDelta;


       rotationVelocity = transform.rotation.eulerAngles - rotationLast / Time.deltaTime;

	
		deltaYaw = transform.rotation.eulerAngles * (yawOutput * Time.deltaTime / mass);
		deltaPitch = transform.rotation.eulerAngles * (pitchOutput * Time.deltaTime / mass);
		deltaRoll = transform.rotation.eulerAngles * (rollOutput * Time.deltaTime / mass);

		rotationVelocity += deltaYaw += deltaPitch += deltaRoll;
		rotationDelta = rotationVelocity * Time.deltaTime;

		transform.rotation += rotationDelta;


		rotationLast = transform.rotation.eulerAngles;

transform.rotation += rotationDelta I know is never going to work but after trying many things I’ve left it here as that is in movement speak is what I’m trying to achieve if that makes sense.

If any one has any help or suggestions I will be very grateful.
Thanks in advance.

Pretty much most of your calculation doesn’t make much sense. Your deltaYas, deltaPitch and deltaRoll vectors is every time the same vector, just with a different length.

Why do you use “+=” three times?

rotationVelocity += deltaYaw += deltaPitch += deltaRoll;

Do you know what the operator does? You basically did this

deltaPitch = deltaPitch + deltaRoll;
deltaYaw  = deltaYaw + deltaPitch;
rotationVelocity = rotationVelocity + deltaYaw;

It will place the correct value in “rotationVelocity” but all other variables are messed up as they contain the partial sum of others. You want to do this:

rotationVelocity += deltaYaw + deltaPitch + deltaRoll;

However i’m not sure what’s the actual point of this. Do you want a constant acceleration? Torques are actually added just like forces, you simply add the vectors together. However from your variables it’s not clear what should be what.

Just to summarize how rigidbody physics work. A rigidbody has exactly 4 state variables:

  • position
  • rotation
  • linear velocity
  • angular velocity

When using gameobjects position and rotation are already covered by the Transform component so you only have to deal with the two velocities:

Vector3 velocity;
Vector3 angularVelocity;

void Update()
{
    transform.position += velocity * Time.deltaTime;
    transform.rotation = Quaternion.AngleAxis(angularVelocity.magnitude*Time.deltaTime, angularVelocity) * transform.rotation;
}

That’s how the current velocity and angularVelocity are applied to the object.

As i said earlier the changing of the velocity / angularVelocity based on a given acceleration is just simple addition:

void AddLinearAcceleration(Vector3 aLinearAcceleration)
{
    velocity += aLinearAcceleration * Time.deltaTime;
}

void AddAngularAcceleration(Vector3 aAngularAcceleration)
{
    angularVelocity += aAngularAcceleration * Time.deltaTime;
}

To convert a linear force into a linear acceleration you simply divide by the mass of the object. Converting a Torque into an angularAcceleration is much more complicated as you have to consider the objects inertiaTensor and it’s rotation relative to the rigidbody. If you want to know how to apply an inertiaTensor, have a look at my answer over here.

Btw: What was your reason again why you don’t want to use a Rigidbody? Because when you need other things like AddForceAtPoint or collisions things get really nasty.