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.