angularVelocity, Quaternions, Vector3 & Transfrom.rotation - a space sim without Rigidbody help

Hi,

Ive posted something similar like this is answers but there seems to be too a lot to it. We are doing this to enhance our programming and maths ability.

We have managed to get a ship to move in a 3 axis via force to keep its a momentum (a lot like Kerbal Space Program game). We are tripping up so much with rotation and angular velocity, at this point I’m not sure whether the math is wrong or scripting, maybe both but definitely the scripting.

This is the movement script that works well and is smooth.

public float mass;
 public float engineForce;
 public float thrusterForce;

private Vector3 velocity; 
 private Vector3 velocityDisplacement;
 private Vector3 deltaThrottle;
 private Vector3 deltaLateral;
 private Vector3 deltaVertical;
 private Vector3 positionLast;

// The ships current Velocity is calculated using the current positon - the last position
        velocity = ((transform.position - positionLast) / Time.deltaTime);
        positionLast = transform.position;


        // Calculates the force in the form of Vector3 using the formula
        // F = m*a = m*dv/dt  therefore  dv = F*dt/m
        deltaThrottle = transform.forward * (engineOutput * Time.deltaTime / mass);
        deltaVertical = transform.up * (verticalOutput * Time.deltaTime / mass);
        deltaLateral = transform.right * (lateralOutput * Time.deltaTime / mass);


        // Takes the forces from the last forumla and adds it to the velocity.
        velocity += deltaThrottle + deltaVertical + deltaLateral;

        // v = ds/dt  therefore  ds = v*dt
        transform.position += velocity * Time.deltaTime;

The Outputs (such as engineOutput) are either 0 or equal to EngineForce.

Trying to do the same sort of thing with rotation. This code is a tidy attempt at what we’ve tried. We know that eulers dont seem to work with this so we are trying different things but this code I think gets the point across but may all be very wrong.

private Vector3 angularVelocity;
 private Vector3 angularDisplacement;
 private Vector3 deltaYaw;
 private Vector3 deltaPitch;
 private Vector3 deltaRoll;
 private Vector3 rotationLast;

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


        // Calculates the force in the form of Vector3 using the formula
        // F = m*a = m*dv/dt  therefore  dv = F*dt/m
        deltaYaw = transform.rotation.eulerAngles * (yawOutput * Time.deltaTime / mass);
        deltaPitch = transform.rotation.eulerAngles * (pitchOutput * Time.deltaTime / mass);
        deltaRoll = transform.rotation.eulerAngles * (rollOutput * Time.deltaTime / mass);


        // Takes the forces from the last forumla and adds it to the velocity.
        angularVelocity += deltaYaw + deltaPitch + deltaRoll * Time.deltaTime;


        transform.rotation = Quaternion.AngleAxis(angularVelocity.magnitude*Time.deltaTime, angularVelocity) * transform.rotation;

Some of that is the result of what we tried from our Answers thread.
Are we going about this all wrong? Any help or suggestions really would be greatly appreciated as we are now like out of ideas. Thanks in advance.

Well when you calculated your velocity based on current position versus last known position here:

 velocity = ((transform.position - positionLast) / Time.deltaTime);

Shouldn’t you be using parentheses and calculate the angular velocity the same way. Your current code is missing the parentheses:
Your code:
angularVelocity = transform.rotation.eulerAngles - rotationLast / Time.deltaTime;\

should probably be:

angularVelocity = (transform.rotation.eulerAngles - rotationLast) / Time.deltaTime;
//  dv = F*dt/m  (this looks right- so why multiply by the position?- I suggest removing that.)
        deltaYaw = (yawOutput * Time.deltaTime / mass);
        deltaPitch = (pitchOutput * Time.deltaTime / mass);
        deltaRoll = (rollOutput * Time.deltaTime / mass);

I would store each of these angular accelerations separately in the AngularVelocity Vector3 (increment velocity by acceleration). Furthermore, I would declare this velocity in the class, not the function, so it persists across calls.

angularVelocity.x += deltaPitch;
angularVelocity.y += deltaRoll;
angularVelocity.x += deltaYaw;

Then, to increment the current rotation by this angularVelocity. This is different from what you are doing: I’m keeping the angular velocity and using THAT to increment the rotation (rather than the reverse; using the change in rotation to determine the anglular velocity)

transform.rotation.eulerAngles += (angularVelocity* Time.deltaTime);