So the concept is you have a parent entity representing a spaceship, and child entities that represent thrusters/engines. The script on the parent entity finds all the child engines, and if they are “firing” it uses RigidBody.ApplyForce(Vector3 thrust, Vector3 localOffset) to add the engine’s force to the spaceship momentum.
Sounded easy enough. Start with a Vector3 with the magnitude of the engine’s power, rotate it by the engine’s world-space rotation quaternion, and there’s your thrust vector. Then the localOffset is just the local translation between the parent ship and the child engine. So when firing the thruster (from the parent entity):
// Start with engine local-space vector with magnitude of engine power. var forceVector = engine.Entity.Transform.LocalMatrix.Forward * power; // HOW TO ROTATE??? Things I've tried: // Attempt 1: Probably not world-space? engine.Entity.Transform.Rotation.Rotate(ref forceVector); // Attempt 2: Trying to get world-space engine quaternion var rot = engine.Entity.Transform.Rotation; engine.Entity.Transform.LocalToWorld(..., ref Quaternion rot, ...); rot.Rotate(ref forceVector); // Attempt 3: Trying to get world-space engine quaternion another way? engine.Entity.Transform.WorldMatrix.Decompose(out _, out Quaternion rot, out _); rot.Rotate(ref forceVector); // Attempt 4: Maybe get WorldMatrix forward instead? forceVector = engine.Entity.WorldMatrix.Forward * power // Finally, apply the force physics.ApplyForce(forceVector, engine.Entity.Transform.Position);
All of these produce weird results like only moving forward in world-space, or rotating wildly. Clearly I only have a loose grasp on matrix transforms because there’s probably a really simple way of doing this. Can anyone help point out what I’m doing wrong here?