Hi, I want to create some kind of homing missiles.
I think I can get the direction vector where the missile has to head for with some code like that:
Vector3f dir = (targetposV3f.subtract(missileposV3f)).normalize();
And then increase the missile position in every update:
newMissilePos = missileposV3f.add(dir.mult(speed));
The problem now is, the missile has to turn around according to its current flight direction, which means I have to set its rotation quaternion somehow.
I have no clue how to calculate this rotation quaternion from the direction vector, can anybody help me?
You could use missle.lookAt(targetposV3f) - is that what you need?
Well…it seems I can't get it to work correctly…
If I see the enemy missiles from near I see that they're jittering like hell…seems they cannot decide in which direction they wanna look.
They also seem to show me their upperside, but they should show me their front side if they are chasing me.
But the chasing itself seem to work, the enemy missiles seem to fly toward my direction.
This is my code (physicsNode is the missile node and ship is target):
physicsNode.lookAt(ship.getNode().getWorldTranslation(), Vector3f.UNIT_Z);
physicsNode.updateWorldVectors();
physicsNode.setLinearVelocity(physicsNode.getLocalRotation().mult(Vector3f.UNIT_Z).mult(speed)); // speed is scalar
try it this way:
its important to update the missiles worldvectors, before calling lookAt(), at least it didn't work for me otherwise.
target.updateWorldVectors();
missile.updateWorldVectors();
missile.lookAt(target.getWorldTranslation(), Vector3f.UNIT_Y);
missile.setLinearVelocity(missile.getLocalRotation().getRotationColumn(2).mult(50));
and with slerp you could make a missile who find its target a bit slower :)
target.updateWorldVectors();
missile.updateWorldVectors();
Quaternion old = new Quaternion(missile.getLocalRotation());
missile.lookAt(target.getWorldTranslation(), Vector3f.UNIT_Y);
missile.getLocalRotation().slerp(old, 0.92f); // the higher the value, the slower missilesrotation
missile.setLinearVelocity(missile.getLocalRotation().getRotationColumn(2).mult(50));