arrgh I hate maths, and clearly I need to love it but.,. I thought this might be a common issue, but a search on here found only issues relating to rotation around a y or z axis, not an arbitary forward axis.
Help, Im stuck on someting in my space game again, getting my ships flying around from point to point is fine, but I now want to take control of some thips and have them roate as well as turn under user control
I have my Orientation of my ships, from GetRigidBody()->getOrientation();
and I can use conjugate and slerp to point the ship to a particular point in space ok.. so turning and pointing at a new point is easy, I'm just using the Y axis (yaw) and a bit of pitch
But what I really need to do is rotate around the forward axis, spinning left and right (roll), to allow for some steering behaviour from the user on the keyboard/mouse, I also want to pull up and down (pitch) This will give me the ability to make tight turns fighter style
But nothing I try seems to let me just rotate along the forward axis.. or combine RPY? I only have control of the yaw. but setting a target orientation to aim for.
Im genuinly crap at this can any one help?
Rotation around the forward axis
-
- Posts: 10
- Joined: Tue Jan 18, 2022 10:29 am
- Location: Guangzhou, China
Re: Rotation around the forward axis
Hi,
Could you put up your code here? otherwise it's hard to tell what you have done wrong. It's probably the problems of calculations with rotation matrix/ quaternions.
Could you put up your code here? otherwise it's hard to tell what you have done wrong. It's probably the problems of calculations with rotation matrix/ quaternions.
-
- Posts: 46
- Joined: Sat Jul 29, 2017 9:19 pm
Re: Rotation around the forward axis
its a bit messy at the moment, since it isn't working I'm just trying things out, also I have to switch to GLM to do some of the maths then back to bullet which isn't nice.
this does produce some level of roll, but also a lot of pitch so... yeah I'm doing it totally wrong and no idea what I'm doing.
Code: Select all
// now lets change orientation?
// can't do this in bullet
// tp is a target position to move left or right around the y
glm::quat targetorientation = glm::conjugate(glm::quat(glm::lookAt(tp, WhoAmI->WorldPosition, glm::vec3(0, 1, 0))));
#ifdef DEBUG
if (Dt > 0.9f) Dt = 0.1f; // avoid break/stepping delays
#endif
glm::quat gcurr = glm::quat(Current.getW(), Current.getX(), Current.getY(), Current.getZ());
glm::quat intermediateientation = glm::slerp(gcurr, targetorientation, Dt / ShipsAttributeData.TurningRate); // need to incorporate some inertia/mass into his turn later
// so we turned twaard the new tp with this... but how do we roll him? repeat on the forward axis?
targetorientation = glm::conjugate(glm::quat(glm::lookAt(WhoAmI->WorldPosition+RightGLM, WhoAmI->WorldPosition, ForwardGLM)));
intermediateientation = glm::slerp(intermediateientation, targetorientation, Dt / ShipsAttributeData.TurningRate); // need to incorporate some inertia/mass into his turn later
btQuaternion btQuatlook;
btQuatlook.setX(intermediateientation.x);
btQuatlook.setY(intermediateientation.y);
btQuatlook.setZ(intermediateientation.z);
btQuatlook.setW(intermediateientation.w);
WhoAmI->MyPhysObj->SetOrientation(btQuatlook);
-
- Posts: 10
- Joined: Tue Jan 18, 2022 10:29 am
- Location: Guangzhou, China
Re: Rotation around the forward axis
my suggestions of a naive way would be using euler angles.
step 1: user inputs
step 2: calculate values of pitch, yaw , roll of current frame (Yes, you need to figure out what the angles are and combine them)
step 3: get Euler angle rotations
step 4: construct rotation matrix from Euler angles
step 4: Or construct quaternion from Euler angles
step 5: apply quaternion to the rigidbody transform.
All you need is to google the convert Euler angles to quaternion part.
You may also want to check out the Unity3D::Quaternion::Slerp function.
Hope this would help.
Edit: if you want to add separate roll on top of the slerping. Then you need to construct another matrix/quaternion that only represents the roll change, then concatenate the matrix/quaternion before applying it to the transform.
step 1: user inputs
step 2: calculate values of pitch, yaw , roll of current frame (Yes, you need to figure out what the angles are and combine them)
step 3: get Euler angle rotations
step 4: construct rotation matrix from Euler angles
step 4: Or construct quaternion from Euler angles
step 5: apply quaternion to the rigidbody transform.
All you need is to google the convert Euler angles to quaternion part.
You may also want to check out the Unity3D::Quaternion::Slerp function.
Hope this would help.
Edit: if you want to add separate roll on top of the slerping. Then you need to construct another matrix/quaternion that only represents the roll change, then concatenate the matrix/quaternion before applying it to the transform.
-
- Posts: 46
- Joined: Sat Jul 29, 2017 9:19 pm
Re: Rotation around the forward axis
slerping is fine when it comes to the actiual transition of the point we are heading to where we want to head I can easily put a target point the left/right/up/down of the ship and the slerp will take it there controlled but the Delta time and rate of movment.
Its just the roll that escapes me..
However setting up eulers is possible, if I rotate around the Z and then recalulate the orientation as a quat it might work...
will give it a try.
Its just the roll that escapes me..
However setting up eulers is possible, if I rotate around the Z and then recalulate the orientation as a quat it might work...
will give it a try.