Jump to content
  • Advertisement
Sign in to follow this  
LewisElliott

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.

If you intended to correct an error in the post then please contact us.

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


Thanks for your time, Lewis.[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

[quote name='dpadam450' timestamp='1323664271' post='4892992']
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

I've got a little more information here:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!