Important: The following is based on two plains; x & y - therefore z is always 0.
Given a target vector, and the following available variables for an (Node) object in space (NB: theres no need to use them all):
- a velocity vector,
- an angle of direction (current heading) as a float,
- a local translation vector
- a rotation magnitude float (defines how fast the object may rotate)
How can I alter the local rotation, in accordance with the rotation magnitude, so that the object begins to rotate towards the target vector. (And stop once it is pointing towards it).
I will be monitoring the node with a controller which does the following during its update method.
public void update(float time) {
Vector3f prey = target.getWorldTranslation();
Vector3f predator = missile.getWorldTranslation();
if(!predator.equals(prey)) {
u.set(prey);
u.subtractLocal(predator).normalizeLocal();
uv.set(u);
uv.multLocal(missile.getVelocity());
vr.set(target.getVelocity());
vr.subtractLocal(u.mult(missile.getVelocity()));
sr.set(prey);
sr.subtractLocal(predator);
float tc = sr.length() / vr.length();
tv.set(target.getVelocity());
tv.multLocal(tc);
st.set(prey);
st.addLocal(tv);
// st now represents the target location vector to move the missile towards
// insert code to alter rotation of missile
}
}
The code was edited from an AI article in the wiki for steering behaviours to remove creating any Vector3f object.
public class Missile extends Node {
private Vector3f velocity;
private float degreeOfRotation;
private float headingAngle;
public Missile(float degreeOfRotation, float headingAngle) {
this.degreeOfRotation = degreeOfRotation;
this.headingAngle = headingAngle;
getLocalRotation().fromAngleAxis(FastMath.DEG_TO_RAD * headingAngle, Vector3f.UNIT_Z);
}
public Vector3f getVeocity() {
return velocity;
}
public float getHeadingAngle() {
return headingAngle;
}
public float getDegreeOfRotation() {
return degreeOfRotation;
}
}
Thanks!