Ok, first i'll fuse all together...
- Code: Select all
vector steeringAngVel; // turning
vector steeringLinVel; // thrust
// input...
steeringAngVel = vector (0, 0, 0) // note: you used torque, i use angular velocity here
steeringLinVel = vector (0, 0, 0)
if keyboard.pressedKeys[keyboard.keys.a] then
steeringAngVel = vector (0, 0, 1.5)
if keyboard.pressedKeys[keyboard.keys.d] then
steeringAngVel = vector (0, 0, -1.5)
if keyboard.pressedKeys[keyboard.keys.w] then
steeringLinVel = vector (0, 0.5, 0) // assuming y axis goes forward
if keyboard.pressedKeys[keyboard.keys.s] then
steeringLinVel = vector (0, -0.5, 0)
// callback...
matrix = BodyGetMatrix()
vector curAngVel = BodyGetOmega() // get body angular velocity in GLOBAL space
vector globalSteeringAngVel = matrix.Rotate (steeringAngVel); // rotate from local space to global
vector targetAngVel = curAngVel + globalSteeringAngVel;
vector curLinVel = BodyGetVelocity() // get body linear velocity in GLOBAL space
vector globalSteeringLinVel = matrix.Rotate (steeringLinVel); // rotate from local space to global
vector targetLinVel = curLinVel + globalSteeringLinVel;
// limit...
if (targetAngVel.Length() > MAX_OMEGA) targetAngVel = targetAngVel / curAngVel.Lenght() * MAX_OMEGA;
if (targetLinVel.Length() > MAX_VEL) targetLinVel = targetLinVel / curLinVel.Lenght() * MAX_VEL;
if (tryThatFirst) // set velocities directly
{
NewtonBodySetOmega (targetAngVel);
NewtonBodySetVelocity (targetLinVel);
}
else // calculate force and torque to reach wanted velocities, which is better
{
vector force = (targetLinVel - curLinVel) * (mass / timestep); // mass got from BodyGetMassMatrix
NewtonBodyAddForce (torque);
targetAngVel -= curAngVel;
vector torque = targetAngVel / timestep;
torque = matrix.Unrotate (torque);
torque.x *= Ixx; // Inertia got from BodyGetMassMatrix
torque.y *= Iyy;
torque.z *= Izz;
torque = matrix.Rotate (torque);
NewtonBodyAddTorque (torque);
}
I hope that's correct.
To keep as simple as possible there's no interpolation stuff, which you can add later to remove some stiffness.
Are all vector operations clear to you? Length() is sqrt(x*x +y*y + z*z)
The only matrix ops are:
Rotate () = RotateVector() in Julios dMatrix.h = TrnsDir() in my code from older thread
Unrotate () = UnrotateVector() in Julios dMatrix.h = TinvDir() in my code from older thread
My matrix code from old threat is 20 years old and i wrote it when i was at the same level you are now.
I copy pasted most of it from resources found in internet, and didn't understood most of it, but that was ok.
You should do the same!
You can try to translate to basic, post code and ask where something is unclear.
EDIT:
To keep the "rotating velocity" trick,
replace the line
vector targetLinVel = curLinVel + globalSteeringLinVel;
with:
vector targetLinVel = matrix.frontVec * curLinVel.Length() + globalSteeringLinVel;
but that works only in forward direction for now.
EDIT2:
A better idea:
vector targetLinVel = matrix.frontVec * matrix.frontVec.Dot(curLinVel) + globalSteeringLinVel;
... handles backwards too