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

Moderators: Cry-Nic, Community Volunteers

### 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:
If I want to get yaw from a vector, I do this
Does anyone find it hard to read for other people? should it be like: (These functions below does not exist, I make them up)
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.

### 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)
As you see in the code
First, it converts from quaternion to yaw-pitch-roll (Ang3): quaternion --> Matrix33 --> Ang3
Then it applies the input (if any) into the yaw-pitch-roll
Then it converts from yaw-pitch-roll to quaternion: Ang3 --> Matrix33 --> quaternion
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.
Maybe I'm wrong, but I think it's ridiculously complicated.

### 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
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,
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)));`
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.
This is quite of a confusion, but I guess after changing to Matrix34 and Matrix33, the Gimbal Lock is eliminated?

### 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?

### 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 !

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

#7
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.

### 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 Brand Manager

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 Brand Manager

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 1 guest