I have searched the forums for an answer but couldn't find what i was looking for.
I have 2 Spatial instances. I want them to orbit each other. Each at their prefered distance, with different turningspeeds for yaw, pitch and roll.
I got distance calculations and movement figured out, but i'm stuck at rotating at different turningspeeds for x,y and z axis.
Kind of a Spatial.lookAt() with a speed modifier.
Any pointers ?
I think i need something as below.
Quaternion pitch = new Quaternion();
Quaternion yaw = new Quaternion();
Quaternion roll = new Quaternion();
pitch.fromAngleAxis(pitchAngle*pitchTurnSpeed, new Vector3f(1,0,0) );
yaw.fromAngleAxis(yawAngle*yawTurnSpeed, new Vector3f(0,1,0) );
roll.fromAngleAxis(rollAngle*rollTurnSpeed, new Vector3f(0,0,1) );
spatial.setLocalRotation(pitch);
spatial.setLocalRotation(yaw);
spatial.setLocalRotation(roll);
But i have no clue how to determine pitchAngle, yawAngle and rollAngle. I think i need the Vector3f which describes the line between both Spatials. I guess this is basic 3d vector math but i don't have much experience with this.
if u r just trying to model something like a dual-star system, u can use spatial transformers to implement this easily.
if this is what u want, i can drop a few lines of code here.
neakor said:
if u r just trying to model something like a dual-star system, u can use spatial transformers to implement this easily.
if this is what u want, i can drop a few lines of code here.
No, no dual star system. :) non-user controlled combatting space ships is the idea ;)
EDIT: above code snippet was not working after all. I still had a "lookAt" abit lower in my code /blush
so u want two things rotate about the same center? if so heres the code.
Node ship1 = new Node("Ship1");
ship1.setLocalTranslation(new Vector3f(10,0,10);
Node ship2 = new Node("Ship2");
ship2.setLocalTranslation(new Vector3f(-10,0,-10);
ship1.attachChild(ship1Mesh);
ship1Mesh.setLocalTranslation(new Vector3f(0,0,0);
ship2.attachChild(ship2Mesh);
ship2Mesh.setLocalTranslation(new Vector3f(0,0,0);
Node center = new Node("Center");
center.setLocalTranslation(new Vector3f());
center.attachChild(ship1);
center.attachChild(ship2);
rootNode.attchChild(center);
float time0 = 0f;
float time1 = 5f;
float time2 = 10;
float time3 = 15f;
SpatialTransformer st = new SpatialTransformer(1);
st.setActive(true);
st.setRepeat(Controller.WRAP);
st.setObject(center, 0, -1);
Quaternion q0 = new Quaternion();
q0.fromAngleAxis(0, new Vector3f(0,1,0));
Quaternion q1 = new Quaternion();
q1.fromAngleAxis(120*FastMath.DEG_TO_RAD, new Vector3f(0,1,0));
Quaternion q2 = new Quaternion();
q2.fromAngleAxis(240*FastMath.DEG_TO_RAD, new Vector3f(0,1,0));
Quaternion q3 = new Quaternion();
q3.fromAngleAxis(360*FastMath.DEG_TO_RAD, new Vector3f(0,1,0));
st.setRotation(time0, q0);
st.setRotation(time1, q1);
st.setRotation(time2, q2);
st.setRotation(time3, q3);
st.setInterpolateMissing();
center.addController(st);
latest version i'm using. I have no clue if the subtract makes sense, but it seems to work. Still no go on the turningspeed(0.00000001f)
Vector3f difference = spatial.getLocalTranslation().subtract(target.getLocalTranslation());
float pitchAngle = difference.x;
float yawAngle = difference.y;
float rollAngle = difference.z;
Quaternion pitch = new Quaternion();
Quaternion yaw = new Quaternion();
Quaternion roll = new Quaternion();
pitch.fromAngleAxis(pitchAngle*0.00000001f, new Vector3f(1f,0,0) );
yaw.fromAngleAxis(yawAngle*0.00000001f, new Vector3f(0,1f,0) );
roll.fromAngleAxis(rollAngle*0.00000001f, new Vector3f(0,0,1f) );
spatial.setLocalRotation(pitch);
spatial.setLocalRotation(yaw);
spatial.setLocalRotation(roll);
EDIT: above code snippet was not working after all. I still had a "lookAt" abit lower in my code /blush
neakor said:
so u want two things rotate about the same center? if so heres the code.
No not really. Not about the same center. I think i can handle the orbitting. I'm struggling with limiting the speed of rotation around the Spatial's own Local Axis.
Following code works apart from the turnSpeed variable not being used. I have no clue where to put it into the equation. (don't mind the inefficient creation of Vectors)
public void rotateTowards(Spatial self,Spatial target,Vector3f upVector,float turnSpeed) {
Vector3f direction = new Vector3f();
direction.set( target.getLocalTranslation() ).subtractLocal( self.getWorldTranslation() );
Vector3f tmpZaxis = new Vector3f();
Vector3f tmpXaxis = new Vector3f();
Vector3f tmpYaxis = new Vector3f();
tmpZaxis.set( direction ).normalizeLocal();
tmpXaxis.set( upVector ).crossLocal( direction ).normalizeLocal();
tmpYaxis.set( direction ).crossLocal( tmpXaxis ).normalizeLocal();
self.getLocalRotation().fromAxes( tmpXaxis, tmpYaxis, tmpZaxis );
}
i think i've got it now:
public void rotateTowards(Spatial self,Spatial target,Vector3f upVector,float turnSpeed) {
Vector3f direction = new Vector3f();
direction.set( target.getLocalTranslation() ).subtractLocal( self.getWorldTranslation() );
Vector3f tmpZaxis = new Vector3f();
Vector3f tmpXaxis = new Vector3f();
Vector3f tmpYaxis = new Vector3f();
tmpZaxis.set( direction ).normalizeLocal();
tmpXaxis.set( upVector ).crossLocal( direction ).normalizeLocal();
tmpYaxis.set( direction ).crossLocal( tmpXaxis ).normalizeLocal();
Quaternion newRot = new Quaternion();
newRot.fromAxes( tmpXaxis, tmpYaxis, tmpZaxis);
self.getLocalRotation().slerp(newRot, turnSpeed);
}
All axis share the same turning speed though, but for now that's good enough :)
EDIT: removed obsolete line of code