I’m trying to set up a physics-based “smart” flight system where you give a desired velocity and angular velocity and it uses its thrusters to match (I have another component to do more useful things like give it a position/angle rather than velocities). I’ve got all of the translational stuff down, but I’m having trouble figuring out how to calculate the force necessary per thruster to give it the desired angular velocity. Below is the commented translational code + the FixedUpdate that tells each of the thrusters what to do. Yes, the code is unoptimized and yes, I’m doing some stupid things for my own convenience. Sue me.
void AdjustVelocity(Propulsion rocket, Vector3 tgVel)
{
rocket.Inp.Forward = 0;
Vector3 up = Vector3.up; // convenience shit, left over from when I was deciding whether or not I wanted to do everything in world space or object space (hint: I chose world space)
Vector3 fwd = Vector3.forward;
Vector3 right = Vector3.right;
RelativeVelocity = (rocket.rig.velocity); // debug/convenience shit - rocket.rig == the system's rigidbody
AngularVelocity = (rocket.rig.angularVelocity);
if (rocket.rig.useGravity)
{
RelativeVelocity += (Physics.gravity / 4f); // make translations account for gravity - dunno why dividing by 4 works
}
rocket.Inp.Forward += (tgVel.z - RelativeVelocity.z) * Vector3.Dot(rocket.Origin.forward, fwd); // tell the rocket's input controller what to do
rocket.Inp.Forward += (tgVel.x - RelativeVelocity.x) * Vector3.Dot(rocket.Origin.forward, right);
rocket.Inp.Forward += (tgVel.y - RelativeVelocity.y) * Vector3.Dot(rocket.Origin.forward, up);
}
void AdjustAngle(Propulsion rocket, Vector3 tgAng)
{
// snip help
}
void FixedUpdate()
{
for (int i = 0; i < Holders.Count; i++) // Holders == List<Propulsion> == all thrusters attached to the root rigidbody
{
Propulsion rocket = Holders[i]; // Propulsion == thruster class
if (!rocket || !rocket.enabled)
continue;
rocket.Inp.Forward = 0; // rocket.Inp == input class, used in various places to
// Adjust for angular velocity
AdjustAngle(rocket, TargetAngularVelocity); // set up rotational forces
rocket.ApplyForces(); // apply rotational forces
// Adjust for translational velocity
AdjustVelocity(rocket, TargetVelocity); // set up translational forces
rocket.ApplyForces(); // apply translational forces, done twice to accurately counteract rotations
}
}
The angular acceleration would be the angular difference between TargetAngularVelocity and rocket.rig.angularVelocity, but the question remains - how should I go about determining the force required to get there? I tried using F=T/(L*sin(theta)) where F = the force and T = the torque, but I’ve got a feeling I was doing it wrong - rotational dynamics isn’t exactly my strong point in physics. I spent a large portion of today kind of shooting in the dark to try to make it work, so any help would be greatly appreciated.