# OpenGL Make an image face the direction its travelling

This topic is 2559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

[font="Consolas"][font="Consolas"]Hi, I'm trying to make an image face the direction its going in. I'm using c sharp and opengl. I store both my bugs position and velocity as 2d vectors. Now I constantly update the position of the bug (as I cant see a way around that, can you?) so I think I need to do atan2 on the unit vectors of the velocity vector y, and velocity vector x, convert it to degrees, then rotate the bug by that but it hasn't been working, so I removed it.

Can anyone show me how to do that?

At the moment I'm trying to do that in the draw method in the world class, but I'm struggling with the unit vector part, does anyone have any suggestions/ input on how I could do this or a better way to do this?

Here is my relevant code:

http://pastebin.com/SH7NAu35

##### Share on other sites
float angle = 180.0/3.14*atan(current_weapon->direction_y/current_weapon->direction_x);
if(current_weapon->direction_x < 0)
{
angle = 180 + angle;
}

##### Share on other sites
As I'm updating the position every second this won't work, I need to do the atan2 of the unit vectors of (getVelocity.Y, getVelocity.X). I just don't understand how to do that?

##### Share on other sites
That is what the code I just posted.

angle = 180.0/3.14*atan(current_weapon->direction_y/current_weapon->direction_x);

##### Share on other sites

That is what the code I just posted.

angle = 180.0/3.14*atan(current_weapon->direction_y/current_weapon->direction_x);

[font="Consolas"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]double[/font][/font][/font][font="Consolas"][font="Consolas"] angle = 180.0 / 3.14 * [/font][/font][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"]Math[/font][/font][/font][font="Consolas"][font="Consolas"].Atan(current_weapon-> bug.getVel().Y , current_weapon-> bug.getVel().X);

[/font][/font][/font]
What is: [font="Consolas"]current_weapon[/font]
and what does: [font="Consolas"]-> mean?[/font]

Cheers

##### Share on other sites

That is what the code I just posted.

angle = 180.0/3.14*atan(current_weapon->direction_y/current_weapon->direction_x);

[font="Consolas"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]double[/font][/font][/font][font="Consolas"][font="Consolas"] angle = 180.0 / 3.14 * [/font][/font][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"]Math[/font][/font][/font][font="Consolas"][font="Consolas"].Atan(current_weapon-> bug.getVel().Y , current_weapon-> bug.getVel().X);

[/font][/font][/font]
What is: [font="Consolas"]current_weapon[/font]
and what does: [font="Consolas"]-> mean?[/font]

Cheers
[/quote]

http://msdn.microsof...v=vs.80%29.aspx

its only for unmanaged/unsafe code in C# so you probably want a . instead

basically x->y is equivalent to (*x).y
(*x) dereferences the pointer "x" (giving you the object the pointer points to)

##### Share on other sites
Thanks for the reply, presuming his currentWeapon is the weapons position I would have to do this:

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]double[/font][/font][/font][font="Consolas"][font="Consolas"] angle = 180.0 / 3.14 * [/font][/font][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"][font="Consolas"][color="#2b91af"]Math[/font][/font][/font][font="Consolas"][font="Consolas"].Atan(bug.getPos()-> bug.getVel().Y, bug.getPos()->bug.getVel().X);

But that is giving me an error, how would I implement what you just said into that?

I can use getPos to do:

bug.getPos().X
bug.getPos().Y

Which returns the current X and Y values.

Cheers

[/font][/font]

##### Share on other sites
You need to get a book on coding and take step by step because the fact that you are this lost you need to take a step back and get something that will teach you step by step.

double angle = 180.0 / 3.14 * Math.Atan(bug.getPos()-> bug.getVel().Y, bug.getPos()->bug.getVel().X);
Why are you putting in position when my code takes atan(velocityY/velocityX). You threw in 2 extra pieces of data and then called the positions as if they are pointers?

You also need to look up the C sharp Atan function and see if it gives radians or degrees. With radians you need the 180/3.14, with degrees you don't. If you still have trouble, then spend a few bucks and buy a used C# book.

##### Share on other sites
Try reading this blog. Its from XNA but its in c# and the syntax and terms should be familiar to you. This helped me learn a few things about using atan2 for direction of sprites.
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Direction_to_Angle.php

Ill take a look at your linked code and see if I can apply this to your code.

Thanks,

##### Share on other sites

Try reading this blog. Its from XNA but its in c# and the syntax and terms should be familiar to you. This helped me learn a few things about using atan2 for direction of sprites.
http://www.riemers.n...on_to_Angle.php

Ill take a look at your linked code and see if I can apply this to your code.

Thanks,

Thanks for the reply, Having spoken with my lecturer he has confirmed with me I need to find the length of my vectors then use this to find the unit vector, then put that into atan2. So I'm using this to find the length:

[font="Arial,"]public double length(Vector2d pV){ return Math.Sqrt(pV.X * pV.X + pV.Y * pV.Y);}[/font]and then in my draw:

[font="Arial,"][/font][font=Arial,]double angle = 180.0 / 3.14 * Math.Atan2(bug.getVel().length(bug.getVel()) * bug.getVel().Y, bug.getVel().length(bug.getVel()) * bug.getVel().X);[/font]
To find the unit vector I'm taking the length, then multiplying it by the current velocity.X or velocity.Y. Is that right?

http://stackoverflow.com/questions/8478259/rotating-an-image-to-face-the-direction-its-travelling-in-atan2-and-unit-vecto

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013708
×