• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
neeker

atan2 inconsistencies

10 posts in this topic

Hi,

 

I'm using atan2 from math.h and I'm getting weird results.  I call it once using a set of parameters and I get the result of 0 degrees (which is correct), but when I call it again with the exact same parameters I get the result of -180 or +180 degrees.  Is this a known issue and are there work arounds?

0

Share this post


Link to post
Share on other sites

Could you show a code showing the issue? Because 0 is pretty different from +/- 180, that would not be a mere known issue, it would be an explosive bug... I am inclined to believe that there is a bug in your code...

1

Share this post


Link to post
Share on other sites
   Logf("vEyePosition = %g, %g, %g", vEyePosition.x, vEyePosition.y, vEyePosition.z);
   Logf("vStartLookAt = %g, %g, %g", vStartLookAt.x, vStartLookAt.y, vStartLookAt.z);
   fViewRotY = atan2(vEyePosition.y - vStartLookAt.y, vStartLookAt.z - vEyePosition.z);
   fViewRotX =atan2(vStartLookAt.x - vEyePosition.x, vStartLookAt.z - vEyePosition.z);
   Logf("Post fVeiwRotX = %g", fViewRotX);

The log messages are:

 

vEyePosition = 1.57361e-007, 50, 9.7
vStartLookAt = 1.57361e-007, 0, 9.7
fVeiwRotX = -9.68575e-008

 

...and...

 

vEyePosition = 1.57361e-007, 50, 9.7
vStartLookAt = 1.57361e-007, 0, 9.7
fVeiwRotX = 3.14159

respectively. 
 

0

Share this post


Link to post
Share on other sites

You are passing numbers that are very very close to 0. Chances are you are passing a positive or zero value as difference of z coordinates in the first case and a negative value in the second case.

0

Share this post


Link to post
Share on other sites

atan2() does not dice ;) It is probable that the values shown by Logf() are rounded when being displayed, i.e. not all significant bits are shown. You may try to enhance the output resolution, or try to output the values as layered uint32_t like in

    *(static_cast<uint32_t*>( &vEyePosition.x))

to see whether they differ.

 

The expected minimal difference in the values then cause a minimal positive or negative value, resp., what then means 0° or 180°, resp.

 

This is a typical problem with differences of floating point numbers.

Edited by haegarr
1

Share this post


Link to post
Share on other sites

Log the actual values being passed in to atan2 - the differences - and it'll be clear what's going on. Your logs have not proven that it is being called with the "exact same parameters".

0

Share this post


Link to post
Share on other sites

atan2() does not dice ;) It is probable that the values shown by Logf() are rounded when being displayed, i.e. not all significant bits are shown. You may try to enhance the output resolution, or try to output the values as layered uint32_t like in

    *(static_cast<uint32_t*>( &vEyePosition.x))

to see whether they differ.

 

The expected minimal difference in the values then cause a minimal positive or negative value, resp., what then means 0° or 180°, resp.

 

This is a typical problem with differences of floating point numbers.

 

Good idea with the casting; that revealed that there are (very slight) differences.

0

Share this post


Link to post
Share on other sites

To give a more visual answer, CppReference has this beautiful little image:

 

285px-math-atan2.png

 

All the trig functions have boundaries that you need to be aware of, and floating point is always an approximation. Any time the approximation gets close to a boundary, unexpected results often follow.

1

Share this post


Link to post
Share on other sites

Problems might arise when you are with boundary values, as said by the previous posters. I guess you are passing -0, 0 sometimes and 0,0 other times, as said by Alvaro.

 

Nevertheless, I don't think the problem is just that. Usually when you have problems with atan2 is because sometimes you get 180 and sometimes -180, which in the end are pretty different representations but for the same angle.

 

When your problems are between 0 and 180 (for example), it kind of suggests that you are doing something mathematically wrong.

 

Let my try to explain it.

 

Atan2(y,x) returns an angle a so that tan(a) = y/x.

 

This is important, because we have that:

 

tan(a) = sin(a)/cos(a) = y/x.

 

Now, this does *not* mean that y needs to be a sine or x need to be a cosine. But USUALLY when you need to call atan2 your two parameters are going to be exactly that. Or at least follow the same proportion (meaning x^2 + y^2 = 1). 

 

So to wrap up: if you are sure your formula is correct, well, by all means go with it. But I would double check if I were you... 

Edited by Javier Meseguer de Paz
0

Share this post


Link to post
Share on other sites


When your problems are between 0 and 180 (for example), it kind of suggests that you are doing something mathematically wrong.

Not necessarily. The resulting angle is just the direction of a vector, and there is a magnitude, too. The magnitude is tiny, e.g. less than 10-10 in the OP's example. With the angle being 0° the vector points along the positive x axis and is very close to 0, and with the angle being 180° it points along the negative x axis but is still very close to 0. It is just so that due to the tiny magnitude and the fact that the mean value is 0, little inaccuracies cause the sign to toggle. Atan2 (as well as atan) depends on the sign of its argument(s) due to the symmetry of sine. Although the angle seems to alter dramatically, it is not necessarily a problem if also the magnitude is considered.

0

Share this post


Link to post
Share on other sites

 


When your problems are between 0 and 180 (for example), it kind of suggests that you are doing something mathematically wrong.

Not necessarily. The resulting angle is just the direction of a vector, and there is a magnitude, too. The magnitude is tiny, e.g. less than 10-10 in the OP's example. With the angle being 0° the vector points along the positive x axis and is very close to 0, and with the angle being 180° it points along the negative x axis but is still very close to 0. It is just so that due to the tiny magnitude and the fact that the mean value is 0, little inaccuracies cause the sign to toggle. Atan2 (as well as atan) depends on the sign of its argument(s) due to the symmetry of sine. Although the angle seems to alter dramatically, it is not necessarily a problem if also the magnitude is considered.

 

 

Yes, that's the reason the function is behaving the way it is doing it, I completely understand that. 

 

What I was saying is that, the same way having a program with a lots of downcastings suggest a problem with the design of the program, having atan2 being so unstable suggest a problem with the formula being used. For example, that a vector that should have been normalized wasn't, which can be a problem elsewhere also.

 

Maybe it is only my case because I almost always end up working with normalized vectors whenever I need to also work with angles. But I thought this was a pretty general case... Anyway, I said "suggest" because, as you point out, there might be plenty of reasons to pass parameters to atan2 which causes erratic behaviour such as this.

 

Maybe I should have said that it suggested problems to me; I am a little paranoid with checking my formulas :P

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0