How to create a homing missile?
Posted: Sun Aug 12, 2018 3:52 pm
I have a rocket that moves with constant velocity. The linear velocity is always applied to the -Z axis(moving it forward). I want to be able to generate torque impulses that nudge the orientation of the rocket towards the enemy ship. I have searched these (and other forums) for the answer and there are things that are still unclear to me. I've read about calculateVelocity in src/LinearMath/btTransformUtil.cpp (https://pybullet.org/Bullet/phpBB3/view ... ate#p22997) but it doesn't seem to to what I need it to do. The linear velocity is constant and the returned angular velocity does not help me with the needed impulse.
What I would like is a function that gets called every frame and adjusts the course. Right now I have the following (java code from libgdx using a bullet wrapper):
What I do is I take the missile position with its front vector and find the quaternion that represents the rotation needed to get from the front to pointing in the enemy direction. Then I find the angular velocity using the calculateVelocityQuaternion() function. I then use (in code not shown here) the angular velocity as a sort of direction in which to apply the torque and scale it to some values until it mostly follows the enemy ship. The problem with this (wrong) approach is that sometimes the missile is almost properly aligned but still gets an impulse to fully align, but instead of aligning it overshoots. After that, the impulse gets bigger so it overshoots even more in the other direction, completely missing the target spinning around like an idiot.
Any idea on how I can gently slow down correctly when the missile is almost on target?
Another constraint that I forgot about is that the missile has a maximum angular velocity, so the impulse must not make the missile rotate more distance per second than allowed. If that limit is reached then the missile should simply fly around the target, missing it.
[EDIT]: The game takes place in space and there is no gravity. But there is angular and linear damping so things don't spin and move endlessly. I also checked the first answer from https://answers.unity.com/questions/488 ... sort=votes but I am not able to find the equivalent values from PhysX in Bullet. Namely the inertiaTensorRotation and the inertiaTensor. It seems bullet only uses inverse values for these things.
What I would like is a function that gets called every frame and adjusts the course. Right now I have the following (java code from libgdx using a bullet wrapper):
Code: Select all
ENG_Vector4D entityPos = entityProperties.getNode().getPosition();
ENG_Quaternion entityOrientation = entityProperties.getNode().getOrientation();
ENG_Vector4D othEntityPosition = otherEntityProperties.getNode().getPosition();
ENG_Quaternion othEntityOrientation = otherEntityProperties.getNode().getOrientation();
ENG_Vector4D diff = otherEntityProperties.getNode().getPosition().subAsVec(entityProperties.getNode().getPosition());
diff.normalize();
ENG_Quaternion rotationTo = entityProperties.getNode().getLocalInverseZAxis().getRotationTo(diff);
btTransformUtil.calculateVelocityQuaternion(new Vector3(entityPos.x, entityPos.y, entityPos.z),
new Vector3(othEntityPosition.x, othEntityPosition.y, othEntityPosition.z),
new Quaternion(entityOrientation.x, entityOrientation.y, entityOrientation.z, entityOrientation.w),
new Quaternion(rotationTo.x, rotationTo.y, rotationTo.z, rotationTo.w),
MainApp.getMainThread().getCurrentElapsedTime(), linearVelocity, angularVelocity);
entityProperties.getRigidBody().applyTorqueImpulse(angularVelocity * some scaling); //This makes no sense, I know
Any idea on how I can gently slow down correctly when the missile is almost on target?
Another constraint that I forgot about is that the missile has a maximum angular velocity, so the impulse must not make the missile rotate more distance per second than allowed. If that limit is reached then the missile should simply fly around the target, missing it.
[EDIT]: The game takes place in space and there is no gravity. But there is angular and linear damping so things don't spin and move endlessly. I also checked the first answer from https://answers.unity.com/questions/488 ... sort=votes but I am not able to find the equivalent values from PhysX in Bullet. Namely the inertiaTensorRotation and the inertiaTensor. It seems bullet only uses inverse values for these things.