Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

graveyard filla

how to rotate a quad in OpenGL (2D)

This topic is 5238 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

high, im working on a top down futuristic 2d RPG in c++ / OpenGL. im currently working on the combat system. how it works is, the player / enemey will "lock on" a target, and when they fire it will auto-aim and fly towards that target.. so far i only have made a SMG, a pistol, and a flamethrower. the bullet (for smg/pistol) and particle effect (for flamethrower) are small and pretty much seamless. however, i want to add a rocket launcher to the game. it will be bigger, slower, and of course blow the shit out of things ... anyway, since the bullet and flamethrower images were so small, i didnt have to rotate them. no matter what direction/angle they were shot in, it would look right. but lets say i make a rocket image that looks like this : === well, this is how it should look going completely strait left or right, but how would i go about making it look nice going north, south, ne, se, and basically any angle possible (since i calcualte a strait path it could go on any angle really). also, i would have have to somehow angle the texture also to make it look right, im thinking. please keep in mind i dont have very good math knowledge, which i realize this is about. i dont know what sin or cos are, but im thinking this is where ill need it. i only know very basic algabra (up to logarithms in math 178 in college).. thanks for any help!! and if you give a forumla, please try to explain how it works a little. thanks!!

Share this post


Link to post
Share on other sites
Advertisement
Once you learn some trig (sin, cos, etc.) this problem will be a lot easier for you. But in the meantime...

First of all, you don''t have to worry about rotating the texture. If you rotate the quad, the texture coordinates will rotate with it and you''ll be fine.

When you render your rocket, you''ll want to push the current matrix onto the stack (which saves it), translate and rotate appropriately, render the rocket, then pop the matrix back off (which restores it). Something like:

glPushMatrix();
glTranslate(rocket.x, rocket.y, rocket.z);
glRotate(rocket.angle);
DrawObject(rocket);
glPopMatrix();

(I think I got the translation and rotation in the right order...)

So your problem is to figure out the appropriate angle based on your player position and target position.

Now that I''ve written all this, I realize I''m actually not sure about the trig required to do this in 2d. But I''ll go ahead and guess and let someone else correct me if necessary.

If you subtract your player position from your target position, you get a vector from your player to your target. The delta x and delta y of this vector are the lengths of two sides of a right triangle whose hypotenuse is the vector itself. These two lengths can also be thought of as the sin and cos of an angle, scaled by the length of the hypotenuse.

Then, there''s a trig function (arccos2? atan2?) that (I think) takes a sin and cos as arguments and returns an angle. I think that''s what you want.

Also, remember that you will get the angle in radians, but you need to convert it to degrees before submitting it to OpenGL.

Now, somebody please fill in the gaps/corret the mistakes in my explanation!

Share this post


Link to post
Share on other sites


[and if any of that is wrong, please correct me]

If you don't know basic trig, it would be very beneficial for these types of things for you to go to the library and get a basic easy to read math book or scour the internet and try to get the basic concepts down. You'll run into it quite a bit as you go along.

The easiest thing to do would be to get the angle when the missile is launched and store it, calculate the normalized vector and store that too, then just inch it forward every frame until it makes contact. This is how i work my laser shots, moving objects, and particles (my particles do not store their vectors though - yet).

If you store the angle, you can easily rotate the quad it's drawn onto with the process jyk gave you. if you store the vector, you don't have to recompute it every frame, and if you want a heat-seeking missile, you'll have to reacquire the the angle and vector every frame (expensive if you have many of them)

[edited by - leiavoia on June 11, 2004 12:26:41 PM]

Share this post


Link to post
Share on other sites
Seeing as how you''re using OpenGL, you can use it to do all of the rotating and stuff insteading having to calculate it yourself.
quote:

glPushMatrix();
glTranslate(rocket.x, rocket.y, rocket.z);
glRotate(rocket.angle);
DrawObject(rocket);
glPopMatrix();

(I think I got the translation and rotation in the right order...)


Don''t you have to rotate it before you translate it? I''m just going off on a hunch here, because from what I remember OpenGL rotates an object around the origin, so doing that would move the object to its position and then rotate it around the origin; although I don''t know how opengl handles translating and rotating in ortho mode.

Share this post


Link to post
Share on other sites
Your right about the rotation/translation.

But you still need to calculate the angle and such since the missile will ultimately need to know where it is and where it is going in true World Space. And that Rotate() function still needs to know how much to rotate by.

Share this post


Link to post
Share on other sites
im really confused why the first post wasnt mine...just go here: http://nehe.gamedev.net he'll explain rotating and such...its a simple function that takes up one line in code. Not saying your answer will be one line but sin/cos arent very practical in this case IMHO

"Carpe Diem!"





[edited by - redrabbit on June 11, 2004 2:43:18 PM]

Share this post


Link to post
Share on other sites
"Don''t you have to rotate it before you translate it?"

True. Although I think the way OpenGL matrices are set up, the transformations actually happen in reverse order. So if you say:

glTranslate()
glRotate()

It actually rotates first and translates second.

Once again, I''m not positive about this :-)

Share this post


Link to post
Share on other sites
ok, let me get this strait (bear in mind im very very poor at math)

first step is find the "slope" of the 2 objects. this is done by
(X2 - X1, Y2 - Y1)

first off, why is this the slope of the line? in school, they told us the slope of a line is Y2 - Y1 / X2 - X1, so which is right?

ok, so now i have the slope, then i just find the distance by doing sqrt(sx*sx + sy * sy);

ok, so now i have the distance in pixels. now i just have to 'normalize' the vector. well, that page didnt explain normalizing at all... BUT, i actually do this in my game when i fire a bullet / other projectile, so that it will fire in a strait path. i got the formula to do this off the board, didnt understand it, just copied and pasted to make my bullets fire strait

anyway, all i do to normalize the vector is do

float normaled = 1 / distance; ?

now, to set the velocity to this projectile (over time, where Time_Passed is the time since last frame, and xVel and yVel are just hard-coded as 1000 or something (for my bullets, also remember sx/sy are slope x/y), i do:

yVelocity = sy * (normaled*yVel * time.Time_Passed);
xVelocity = sx * (normaled*xVel * time.Time_Passed);

ok, so now i know my velocity to travel. all i do is move at this velocty each frame, and my bullet will sail to its target...

ok, now on to rotating the rockets -

"If you subtract your player position from your target position, you get a vector from your player to your target. The delta x and delta y of this vector are the lengths of two sides of a right triangle whose hypotenuse is the vector itself. These two lengths can also be thought of as the sin and cos of an angle, scaled by the length of the hypotenuse."

what do you mean "delta x and delta y of the vector" ???

looking at leai's drawing, all i have to do is

float angle = tan( deltaX / deltaY);
angle = RadToDeg(angle);
glRotate(angle);
DrawMyRocket();

so does this look right at all to you? thanks for anymore help!!!

[edited by - graveyard filla on June 11, 2004 7:29:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by jyk
"Don't you have to rotate it before you translate it?"

True. Although I think the way OpenGL matrices are set up, the transformations actually happen in reverse order. So if you say:

glTranslate()
glRotate()

It actually rotates first and translates second.

Once again, I'm not positive about this :-)


It depends on your view, if you take a grand fixed coordinate system approach, transformations work in reverse, if you take a multiple coordinate system approach (where each object has coordinate system attach to it & can be relative to others) transformations work in natural order which what OpenGL's real view of it but you can still use grand fixed coordinate view.

[edited by - snk_kid on June 11, 2004 7:44:17 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!