Hello again,
its been a while since my last post. Im sorry.
Im currently working on the ballistic trajectory for projectiles. Its working the way i want it to and the arrows fly in a parabolic path. There are still some problems remaining concerning the rotation of the arrow during the flight.
There seem to be a problem when the the radians value of the pitch and the yaw of the intial velocity-vector are equal or almost equal to each other and are around PI/2 . When beeing shot the arrow is facing upwards and is is only rotating once at highest piont on the fligth curve. Im was thinking that the Problem may a gimbal lock but this should not happen when quaternions are beeing used to represent the rotation. The other Problems that i have is that the direction of the rotation is always the same. The Arrow is always turning clockwise. The consequence is that between 0 -180 degrees the arrow is rotating away from the player as it should wheras the the arrow rotates towards the player when beeing shot between 180 and 360 degress. I guess the Problem here ist that the atan2 function returns radians values between +PI and -PI.
Here the code i am using to simulate the ballistic trajectory of the a bullet thats been fired. I know there are alot of different approaches around the internet for this exact problem and i tried a lot of them. None of them really worked from me.
Therefore based on the knowlegde i gained from my research i put up my own solution. This method is called every delta within my ProjectileSystem to update the rotation.
/** Sets the rotation of the Projectile according to its current velocity
*/
private void trajectory(RigidBody projectile, EntityRef entity){
Vector3f velocity = new Vector3f();
projectile.getInterpolationLinearVelocity(velocity);
Transform transform = new Transform();
projectile.getWorldTransform(transform);
//geting the angles of the current velocity vector
double pitch = Math.atan2(velocity.y, velocity.z);
double yaw = Math.atan2(velocity.x, velocity.z);
AxisAngle4f pitchAngle = new AxisAngle4f(1, 0, 0, (float)pitch);
AxisAngle4f yawAngle = new AxisAngle4f(0, 1, 0, (float)yaw);
Quat4f quatXRotation = new Quat4f();
Quat4f quatYRotation = new Quat4f();
quatXRotation.set(yawAngle);
quatYRotation.set(pitchAngle);
Quat4f rotation = new Quat4f();
//rotate
rotation.mul(quatXRotation,quatYRotation);
transform.setRotation(rotation);
projectile.setWorldTransform(transform);
}
If you see anything suspicios in my code please let me know. Thanks.
I guess i also have to have a closer look at the special cases of the Math.atan2 function as beeing mentioned here:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#atan2(double, double)
its been a while since my last post. Im sorry.
Im currently working on the ballistic trajectory for projectiles. Its working the way i want it to and the arrows fly in a parabolic path. There are still some problems remaining concerning the rotation of the arrow during the flight.
There seem to be a problem when the the radians value of the pitch and the yaw of the intial velocity-vector are equal or almost equal to each other and are around PI/2 . When beeing shot the arrow is facing upwards and is is only rotating once at highest piont on the fligth curve. Im was thinking that the Problem may a gimbal lock but this should not happen when quaternions are beeing used to represent the rotation. The other Problems that i have is that the direction of the rotation is always the same. The Arrow is always turning clockwise. The consequence is that between 0 -180 degrees the arrow is rotating away from the player as it should wheras the the arrow rotates towards the player when beeing shot between 180 and 360 degress. I guess the Problem here ist that the atan2 function returns radians values between +PI and -PI.
Here the code i am using to simulate the ballistic trajectory of the a bullet thats been fired. I know there are alot of different approaches around the internet for this exact problem and i tried a lot of them. None of them really worked from me.
Therefore based on the knowlegde i gained from my research i put up my own solution. This method is called every delta within my ProjectileSystem to update the rotation.
/** Sets the rotation of the Projectile according to its current velocity
*/
private void trajectory(RigidBody projectile, EntityRef entity){
Vector3f velocity = new Vector3f();
projectile.getInterpolationLinearVelocity(velocity);
Transform transform = new Transform();
projectile.getWorldTransform(transform);
//geting the angles of the current velocity vector
double pitch = Math.atan2(velocity.y, velocity.z);
double yaw = Math.atan2(velocity.x, velocity.z);
AxisAngle4f pitchAngle = new AxisAngle4f(1, 0, 0, (float)pitch);
AxisAngle4f yawAngle = new AxisAngle4f(0, 1, 0, (float)yaw);
Quat4f quatXRotation = new Quat4f();
Quat4f quatYRotation = new Quat4f();
quatXRotation.set(yawAngle);
quatYRotation.set(pitchAngle);
Quat4f rotation = new Quat4f();
//rotate
rotation.mul(quatXRotation,quatYRotation);
transform.setRotation(rotation);
projectile.setWorldTransform(transform);
}
If you see anything suspicios in my code please let me know. Thanks.
I guess i also have to have a closer look at the special cases of the Math.atan2 function as beeing mentioned here:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#atan2(double, double)