I’m working on the ship. Right now, I am trying to stabilizing. I have the following code. The last two lines gets the linear velocity of the parent rigid body and applys a impulse to its lateral movement. The question is the linear velocity the local velocity of the rigid body. I feel like I should be doing the linear velocity x and y multiplied by the rotation of the rigid body.
Ideally I would like make it stabilize based on the fixed vector and rotation meaning the parent rigid body. For example, if in a collision box trigger a station. It use the collision box vector and rotation. If in space zero gravity outside maybe to a target and if no target no stabilization since space has no up and down direction.
void PropulsionThrusterComponent::OnUpdate(float timeStep) {
// If thrust active is true
if (m_bThrust) {
// Check if a parent rigid body exist
if (!m_ParentRigidBody) {
// Get Components
m_ParentRigidBody = m_pNode->GetParent()->GetComponent<
RigidBodyComponent>();
// If failed getting the parent rigid body return
if (!m_ParentRigidBody) {
return;
}
}
// Turn off Kinematic
if (m_ParentRigidBody->IsKinematic()) {
m_ParentRigidBody->SetKinematic(false);
}
// Apply force up
m_Impulse = Vector3::BACK * m_ThrustLevel;
// Get Rotations
m_ParentWorldRotation = m_pNode->GetParent()->GetWorldRotation();
m_ChildRotation = m_pNode->GetRotation();
// Create force level
m_Force = (m_ParentWorldRotation * (m_ChildRotation * m_Impulse));
m_ParentRigidBody->ApplyImpulse(m_Force * timeStep,
Vector3(m_pNode->GetPosition().x_,m_pNode->GetPosition().y_,
m_pNode->GetPosition().z_));
// // Get angular Velocity
// Vector3 currentAngularVelocity =m_ParentRigidBody->GetAngularVelocity();
// double RadiusToDegree = 360 * (3.14159 / 2);
//
// float stability = 0.5f;
// float speed = 0.2f;
//
// Quaternion RotationFromAngleAxis = Quaternion();
//
// RotationFromAngleAxis.FromAngleAxis(
// currentAngularVelocity.LengthSquared() * RadiusToDegree
// * stability / speed, currentAngularVelocity);
//
// Vector3 predictedUp = RotationFromAngleAxis * Vector3::UP;
//
// // Calculate a torqueVector
// Vector3 torqueVector = predictedUp.CrossProduct(Vector3::UP);
//
// m_ParentRigidBody->ApplyImpulse(torqueVector * speed);
// Get linear velocity
Vector3 linearVelocity = m_ParentRigidBody->GetLinearVelocity();
// Get rigid body rotation and change linear assuming it's a local linear velocty
linearVelocity = m_ParentRigidBody->GetRotation()*linearVelocity;
// Inverse forward and reverse
m_ParentRigidBody->ApplyImpulse(Vector3(-(linearVelocity.x_), 0, -(linearVelocity.z_)));
}
}