Get rotation angle from normalized 2D vector

Started by
49 comments, last by JoeJ 5 years, 9 months ago

32 bit windows XP?  It's kind of admirable you're targeting that, but that encompasses 0.22% of all Steam Users.  You can pretty safely target higher OS versions at this point.

 

(Other than that, I agree that fastest trig function is the one you don't call, and use the vector instead of the angle when you can)

 

EDIT2:  As this is for rendering, you could do all your work in shaders, which will be faster still.  Maybe treat each sphere/circle/node of your worms as an instance and utilize instancing to rotate them in the vertex shader.

Advertisement

Thanks again Joe, your code works.

Now i have a new problem, the lights are rotating also, how do i fix this ?

thanks

20 hours ago, ferrous said:

32 bit windows XP?  It's kind of admirable you're targeting that, but that encompasses 0.22% of all Steam Users.  You can pretty safely target higher OS versions at this point.

Its what i have, i dont have internet on my computer, to install new windows you need internet.

People can still play my game, persons without steam or internet do also exist.

There is no big market for 2D side scrollers, people who still play that use older game consoles, those are the people i aim for.

20 hours ago, ferrous said:

As this is for rendering, you could do all your work in shaders, which will be faster still.  Maybe treat each sphere/circle/node of your worms as an instance and utilize instancing to rotate them in the vertex shader.

I would like to, i dont want to learn a shader language, can it be done without ?

With shader you can also make skinned meshes right ?, i would like to make humans walk and move.

First i like to have something simple animated, i will make a new topic about that.

20 hours ago, JTippetts said:

atan2 returns values in the range -pi to pi, corresponding to -180 to +180 degrees. You can add pi to the result to get values in the range of 0 to 2pi.

Who telling you that is not correct, i am sure now.

You could try to make a function that rotates a enemy towards the player with atan2, then you see.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

Are you saying that the range of values returned by atan2 is not -pi to pi? I don't need to make a function that rotates anything to figure that one out. I can just run this:
 


#include <iostream>
#include <cmath>

int main() {
  std::cout << std::atan2(-1e-10, -1.0) << '\n';
  std::cout << std::atan2(+1e-10, -1.0) << '\n';
}

Or I can look at the documentation: http://www.cplusplus.com/reference/cmath/atan2/

 

Quote

Who telling you that is not correct, i am sure now.

In C++:

https://ideone.com/4DpoJf

In Lua:

https://ideone.com/oBZU7A

From Wikipedia: This produces results in the range (−π, π]...

I could go on. I mean, you could google this stuff to find out for sure. The internet is literally, like, right there. It's so easy these days, you don't even have to put pants on to do it.

I am not limiting the input in any way, maybe that is why then ?

Here is proof that learning from a book you become a parrot.

You should use it like i asked you to have my proof.

Lets stop this disscussion please, or let someone join the discussion.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

I just joined in. I gave you examples when the output is very close to -pi and very close to pi. Do you have examples when the output is *not* in the range from -pi to pi? Just a few lines of code, like I posted, would be enough.

 

Here is the code i use :

aimrot = -atan2f( x - player.x , y - player.y );

if( aimrot > D3DX_PI )aimrot -= D3DX_PI * 2.0f;

Its around the center of the universe, try it.

 

Please lets focus on why the lighting is not correct in the mesh, maybe it has to do with normals in the matrix ?

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

Try what? I don't know what values to use for x, y, player.x or player.y . Extract specific values passed to atan2 where you obtained something outside of [-pi,+pi] from it. Any additional code required will just obscure the issue, because the problem could be anywhere else.

 

I dont know the exact values, or might the D3DX_PI value incorrect ?

I am not trying to find these values, no time.

I wish it was true to avoid the if.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

Oh, I'm sorry. Of course, you don't have time to debug your own problem, so you expect us to help you for free. I see how that works...

This topic is closed to new replies.

Advertisement