Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Value calculation problem

dekon_17
Registered User
Quote
2023-02-26 16:20:08

Calculating rotation of character in radians gives me "-nan(ind)" when I move without using mouse look. It only happens once in a run (from what I experienced), but it is annoying and straight up bad. Thing is, it only returns such value from "Math.acos" part, whole thing without it gives normal number instead of this. Why can this happen?

Here's how this variable looks:
var rawX = Math.acos ((t1.x*t2.x + t1.y*t2.y + t1.z*t2.z) / (Math.sqrt (t1.x*t1.x + t1.y*t1.y + t1.z*t1.z) * Math.sqrt (t2.x*t2.x + t2.y*t2.y + t2.z*t2.z)));



just_in_case
Moderator
Quote
2023-02-26 18:59:59

You should make sure that the value you are using for Math.acos() is between 1 and -1 otherwise it will return "-nan"

hope that helps


okeoke
Registered User
Quote
2023-02-27 09:56:45

I guess, you can try to log the t1 and t2 values in case rawX is NaN, so you can catch the input which causes the error.

It actually throws NaN even if you have something like 1.00000001, which you can get if you have any other floating point operation with your t1 and t2 props before. So if it doesn't affect you in any way and you just don't want the error message to appear you can use a clamp function to force the input between -1 and 1. I don't recommend this, but it's something that I would do:)

function clamp(number, min, max) {
return Math.max(min, Math.min(number, max));
}

clamp(t1.x * t2.x + t1.y * t2.y + t1.z * t2.z, -1, 1);



dekon_17
Registered User
Quote
2023-02-27 15:16:59

Hey, so, knowing that it returns this weird value if it is larger than 1 or lower than -1, I basically capped the value:
...
var rawX1 = (t1.x*t2.x + t1.y*t2.y + t1.z*t2.z) / (Math.sqrt (t1.x*t1.x + t1.y*t1.y + t1.z*t1.z) * Math.sqrt (t2.x*t2.x + t2.y*t2.y + t2.z*t2.z));
if (Math.abs (rawX1) > 1)
rawX1 = sign (rawX1);

var rawX = Math.acos (rawX1);
...
function sign (number)
{
return (Math.abs (number) / number);
}

Don't know if this way is good or not, but nothing bad happened from it. In fact, the issue is solved (for now), so, works for me I guess. It also solved one other issue I had, but that is to be kinda expected.

Thanks guys!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact