Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine?

#1
For now, if I want to get angle between 2 vector (2D), is write this code:

Code: Select all

float dot = faceDir.x*m_lstFaceDir.x + faceDir.y*m_lstFaceDir.y;//dot product of faceDir and m_lstFaceDir
float det = faceDir.x*m_lstFaceDir.y - faceDir.y*m_lstFaceDir.x;//determinant
float angle = atan2(det, dot);//atan2(y, x) or atan2(sin, cos)
If I want to get yaw from a vector, I do this

Code: Select all

Ang3 ypr;
ypr.x = -atan2(faceDir.x, faceDir.y); //Get yaw
ypr.y = 0;
ypr.z = 0;
Does anyone find it hard to read for other people? should it be like: (These functions below does not exist, I make them up)

Code: Select all

float crymath_util::GetYaw(Vec2 vec);//Get yaw
float crymath_util::GetAngle(Vec2 vec1, Vec2 vec2);//Get angle form by vec1 & vec2
I hope CryEngine provide the common use math functions like that, so it make the client code more clean (I don't have to write & re-write a bunch of code and require other people that may or may not have massive math knowledge to think to understand, I think clean code is code that doesn't require people think much to understand (comment frequently and capsule common code into functions should help))

Also I don't understand why use quaternion for rotation instead of Ang3 (yaw, pitch, roll)? I think that quaternion is too complicated. (it's not a complaint, it's a question) Please show me the power of quaternion that Ang3 can't archive, if any.
If I want to rotate the yaw of the character 2 degree to the left, what should I do with quaternion?

If there already is the math utility functions like I mentions above, please show them to me, I really need them.
Last edited by HDN on Wed Jan 24, 2018 2:55 pm, edited 1 time in total.
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#2
Also I don't understand why use quaternion for rotation instead of Ang3 (yaw, pitch, roll)? I think that quaternion is too complicated. (it's not a complaint, it's a question) Please show me the power of quaternion that Ang3 can't archive, if any.
I haven't seen the advantage of quaternion, but I've already seen the bullshit it throw into my face.
For example, this code (source here) (without this source, I can't figure out how to do simple thing like rotate a camera, so thanks, I guess)

Code: Select all

void CPlayerComponent::UpdateCamera(float frameTime)
{
// Start with updating look orientation from the latest input
Matrix33 cam33(m_camOrientation);

Ang3 ypr = CCamera::CreateAnglesYPR(cam33);

if (!m_mouseDeltaRotation.IsZero())
{
const float rotationSpeed = 0.002f;

ypr.x += m_mouseDeltaRotation.x * rotationSpeed;

const float rotationLimitsMinPitch = -1.2;
const float rotationLimitsMaxPitch = 0.05f;

// TODO: Perform soft clamp here instead of hard wall, should reduce rot speed in this direction when close to limit.
ypr.y = CLAMP(ypr.y + m_mouseDeltaRotation.y * rotationSpeed, rotationLimitsMinPitch, rotationLimitsMaxPitch);

// Look direction needs to be synced to server to calculate the movement in
// the right direction.
m_camOrientation = Quat(CCamera::CreateOrientationYPR(ypr));
NetMarkAspectsDirty(eEA_GameClientD);

// Reset every frame
m_mouseDeltaRotation = ZERO;
}

// Start with changing view rotation to the requested mouse look orientation
Matrix34 localTransform = IDENTITY;
localTransform.SetRotation33(Matrix33(m_pEntity->GetWorldRotation().GetInverted()) * CCamera::CreateOrientationYPR(ypr));

// Offset the player along the forward axis (normally back)
// Also offset upwards
//localTransform.SetTranslation(-localTransform.GetColumn1() * viewDistance);
Vec3 offset;
const Vec3 upOffset(0, 0, 1.7f);
offset = (-localTransform.GetColumn1()) * 2.5f + upOffset;

localTransform.SetTranslation(offset);

m_pCameraComponent->SetTransformMatrix(localTransform);
}
As you see in the code
First, it converts from quaternion to yaw-pitch-roll (Ang3): quaternion --> Matrix33 --> Ang3

Code: Select all

Matrix33 cam33(m_camOrientation);

Ang3 ypr = CCamera::CreateAnglesYPR(cam33);
Then it applies the input (if any) into the yaw-pitch-roll

Code: Select all

if (!m_mouseDeltaRotation.IsZero())
{
const float rotationSpeed = 0.002f;

ypr.x += m_mouseDeltaRotation.x * rotationSpeed;

const float rotationLimitsMinPitch = -1.2;
const float rotationLimitsMaxPitch = 0.05f;

// TODO: Perform soft clamp here instead of hard wall, should reduce rot speed in this direction when close to limit.
ypr.y = CLAMP(ypr.y + m_mouseDeltaRotation.y * rotationSpeed, rotationLimitsMinPitch, rotationLimitsMaxPitch);
Then it converts from yaw-pitch-roll to quaternion: Ang3 --> Matrix33 --> quaternion

Code: Select all

m_camOrientation = Quat(CCamera::CreateOrientationYPR(ypr));
Then to apply the yaw-pitch-roll into the camera, it does it through Matrix34 and Matrix33. See, it doesn't even use quaternion to apply rotation into the camera, but uses yaw-pitch-roll to do that. quaternion is just like something to show off and scare people off.

Code: Select all

// Start with changing view rotation to the requested mouse look orientation
Matrix34 localTransform = IDENTITY;
localTransform.SetRotation33(Matrix33(m_pEntity->GetWorldRotation().GetInverted()) * CCamera::CreateOrientationYPR(ypr));
//...
m_pCameraComponent->SetTransformMatrix(localTransform);
Maybe I'm wrong, but I think it's ridiculously complicated.
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#3
Maybe I'm wrong, but I think it's ridiculously complicated.
It is supposed to be ridiculously complicated, yet it has its own advantage, check Unity's post
Benefit: Quaternion rotations do not suffer from Gimbal Lock.
Unity also uses quaternion
Unity uses Quaternions internally, but shows values of the equivalent Euler angles in the inspector to make it easy for you to edit.
Also I don't understand why use quaternion for rotation instead of Ang3 (yaw, pitch, roll)? I think that quaternion is too complicated. (it's not a complaint, it's a question) Please show me the power of quaternion that Ang3 can't archive, if any.
Here
In Unity all Game Object rotations are stored internally as Quaternions, because the benefits outweigh the limitations.
And here is an interesting comparison,
The Difference Between Euler Angles and Quaternions
Euler Angles

Euler angles have a simpler representation, that being three angle values for X, Y and Z that are applied sequentially. To apply a Euler rotation to a particular object, each rotation value is applied in turn, as a rotation around its corresponding axis.
  • Benefit: Euler angles have an intuitive “human readable” format, consisting of three angles.
  • Benefit: Euler angles can represent the rotation from one orientation to another through a turn of more than 180 degrees
  • Limitation: Euler angles suffer from Gimbal Lock. When applying the three rotations in turn, it is possible for the first or second rotation to result in the third axis pointing in the same direction as one of the previous axes. This means a “degree of freedom” has been lost, because the third rotation value cannot be applied around a unique axis.
-------------------------------
Quaternions

Quaternions can be used to represent the orientation or rotation of an object. This representation internally consists of four numbers (referenced in Unity as x, y, z & w) however these numbers don’t represent angles or axes and you never normally need to access them directly. Unless you are particularly interested in delving into the mathematics of Quaternions, you only really need to know that a Quaternion represents a rotation in 3D space and you will never normally need to know or modify the x, y & z properties.

In the same way that a Vector can represent either a position or a direction (where the direction is measured from the origin), a Quaternion can represent either an orientation or a rotation - where the rotation is measured from the rotational “origin” or “Identity”. It because the rotation is measured in this way - from one orientation to another - that a quaternion can’t represent a rotation beyond 180 degrees.
  • Benefit: Quaternion rotations do not suffer from Gimbal Lock.
  • Limitation: A single quaternion cannot represent a rotation exceeding 180 degrees in any direction.
  • Limitation: The numeric representation of a Quaternion is not intuitively understandable.
If I want to rotate the yaw of the character 2 degree to the left, what should I do with quaternion?
Easy,

Code: Select all

m_pEntity->SetRotation(m_pEntity->GetRotation()*Quat::CreateRotationZ(DEG2RAD(2)));
But about this
Then to apply the yaw-pitch-roll into the camera, it does it through Matrix34 and Matrix33. See, it doesn't even use quaternion to apply rotation into the camera, but uses yaw-pitch-roll to do that. quaternion is just like something to show off and scare people off.

Code: Select all

// Start with changing view rotation to the requested mouse look orientation
Matrix34 localTransform = IDENTITY;
localTransform.SetRotation33(Matrix33(m_pEntity->GetWorldRotation().GetInverted()) * CCamera::CreateOrientationYPR(ypr));
//...
m_pCameraComponent->SetTransformMatrix(localTransform);
This is quite of a confusion, but I guess after changing to Matrix34 and Matrix33, the Gimbal Lock is eliminated?
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#4
The angle between two vectors should just be the dot product. This is scaled from -1.0 to 1.0 which is actually perfect for plugging into many calculations. Convert it to some other scalar if you really need to. Let me Google that for you. That's a pretty 'mathy' page, but at least it will have factual and useful information.

To rotate an entity by a quaternion you simply multiply it's current rotation by the rotation you wish to apply. Set the new rotation to the result of that.

It's pretty normal to take input from the player as degrees / radians and then convert that to a quaternion. As stated, quaternions help prevent gimbal lock and are in a very efficient form, taking only 4 floats to represent them. That's why they are used everywhere for this.

The code you are having trouble to understand is making a matrix out of the current quat, and turning that into Euler angles so it can perform a clamp on the amount of pitch allowed. It's far easier to reason about this in Euler angles than the 4 dimensional math of the quaternion. Also, you want to break it out into components so you can apply the restriction to just the one part that needs it. The movement deltas are applied and then the whole thing is turned back into quats or a matrix, whichever is the most convenient. There's some leeway on the programmer's side too on which approach they take.

Spend an hour or so to work through this course and it will help you immensely.

Finally, you will get a slap on the wrist if you link to Unity or the Unreal web sites, so I suggest not doing that in future.

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#5
The angle between two vectors should just be the dot product. This is scaled from -1.0 to 1.0 which is actually perfect for plugging into many calculations. Convert it to some other scalar if you really need to. Let me Google that for you. That's a pretty 'mathy' page, but at least it will have factual and useful information.

To rotate an entity by a quaternion you simply multiply it's current rotation by the rotation you wish to apply. Set the new rotation to the result of that.

It's pretty normal to take input from the player as degrees / radians and then convert that to a quaternion. As stated, quaternions help prevent gimbal lock and are in a very efficient form, taking only 4 floats to represent them. That's why they are used everywhere for this.

The code you are having trouble to understand is making a matrix out of the current quat, and turning that into Euler angles so it can perform a clamp on the amount of pitch allowed. It's far easier to reason about this in Euler angles than the 4 dimensional math of the quaternion. Also, you want to break it out into components so you can apply the restriction to just the one part that needs it. The movement deltas are applied and then the whole thing is turned back into quats or a matrix, whichever is the most convenient. There's some leeway on the programmer's side too on which approach they take.

Spend an hour or so to work through this course and it will help you immensely.
Thank ivanhawkes, actually, at first, I have a little trouble, with these math thing, but that's not the point, it's not about me. The point of the topic is finding a way to make the client code easiest to understand for everyone. Of course I can understand that dot can be used to find the angle (I added the example code to find angle between 2 vector in the first post), but I worry that other people have hard time reading & writing the code like this. Of course, I can create a simple math helper library to suit my need, but I just think it could be a good idea to suggest CryEngine create the official math helper library so that make it more comfortable for everyone to deal with this.
Finally, you will get a slap on the wrist if you link to Unity or the Unreal web sites, so I suggest not doing that in future.
With all due respect, I have no mean to offend anyone. I only try to share what I've found so that other people don't have to find it again. Sorry if I ignorantly violated some rule, but I've tried to read the forum rules before posting... but I didn't find that it's forbidden to share link from Unity or Unreal web sites. Am I missing something?
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#6
Nope, it's not forbidden. Don't get so official :) It's just about people being in love with cryengine so much, that they don't want to hear about any other engine, so they get angry. Some people seem to forget or understand, that games were made for entertainment, and to have fun.
Cryengine tutorial videos on my Youtube channel! Check it out !
https://www.youtube.com/user/MusicForLifePL20

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#8
I received an official warning notice a month or two back in response to a link I posted to the Unity site. I'm not out to bash Unity, just letting you know that they don't like links to it here on the forums.
Actually, your post was advertisement for a third-party service offered on the Unity forums in response to someone who requested CRYENGINE specific services - with your remark that anybody could get what the respective person asked for cheaper when using Unity and following the unverified link to the Unity forums.

That pretty much violates Rule 1 of our content rules:
• Do not include spam or third-party [commercial or non-commercial] advertisement of any sort in your posts.
It was blatant advertisement for a competing service simply implying that what was asked for would not be possible in CRYENGINE without giving any room for discussion. So yes, _that_justifies a slap on the wrist. Linking to general coding tutorials or comparing certain facts between industry standard factions is legit - as long as the general discussion revolves around CE and you aren't bashing CE or its competitors in the process.

Context! Context is important. :)

Please carry on folks, I see nothing wrist-slap-worthy in this thread so far except for what was just wrist-slapped back into context.
Nic
CRYENGINE Community Developer

I'm manning the CRYENGINE stations on Facebook & Twitter as well, so come and say hi!
My personal Twittering may contain nuts.

Get to know me.

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#9
Sadly, irony doesn't convey well in text form. The post was an ironic response to a person asking for someone to build him a complete third person shooter game for something like $250, which is an absurd request no matter which engine was selected. The joke part of that was the off-loading of them onto the Unity community instead of having them hang around here. I guess I should have added some smilies or something ^.^ Ah well, live and learn.

Thanks for the clarification of the point!

Re: Is there a math utility (help to do things like: get angle between 2 vector, get yaw from a vector,...) in CryEngine

#10
Sadly, irony doesn't convey well in text form. The post was an ironic response to a person asking for someone to build him a complete third person shooter game for something like $250, which is an absurd request no matter which engine was selected. The joke part of that was the off-loading of them onto the Unity community instead of having them hang around here. I guess I should have added some smilies or something ^.^ Ah well, live and learn.

Thanks for the clarification of the point!
Humor is really hard to convey in written text indeed. ;) I get your point now, but yeah, just making a fair point as of why it is a fairly unrealistic request no matter the tool would have come a long way in this case.

Additionally, that would actually be on the borderline of violating another rule:
• No name shaming or call-outing of other users, people, brands or competitors.
We don't want to be killjoys but we believe in a professional co-existence and more diversity on the market. Seeing people being very passionate about CRYENGINE makes us all happy, of course. But fairness and respect should be valued above all else in all communication in this forum. So thank you all for your understanding.

------------------------

Moderation Note:
Now let's end this off-topic discussion and get back to topic - if any of you has any questions about our rules and how they are enforced, just ping me via PM. Because we're about to break another rule and hijack this thread to discuss something completely different and that's not okay. I don't want to be disrespectful to the thread owner.
Nic
CRYENGINE Community Developer

I'm manning the CRYENGINE stations on Facebook & Twitter as well, so come and say hi!
My personal Twittering may contain nuts.

Get to know me.

Who is online

Users browsing this forum: No registered users and 3 guests