I am writing a 2d game and I need to write a function to point a sprite (a textured quad) at a position on the screen (x,y in pixels), I know some math is involved, but I don’t have a clue where to start, could you please give me a hint, thanks!
you could use glWritePixels,
but using a texture is more faster,
since it’s accelerated by 3D cards.
so create a quad in front of you and put
the texture on it. You’all also have to configure
texturing mode and load image.
One way would be to find out the angle your quad’s origin point makes with the target point. Then simply use glRotate to rotate the quad accordingly.
Below is a function which determines the 2D 360 degree angle a vector makes. The vector is defined as starting at “origin” and ending at “p”. Let me know if you find errors.
</font><blockquote><font size=“1” face=“Verdana, Arial”>code:</font><hr /><pre style=“font-size:x-small; font-family: monospace;”>// Results are in radians!!!
float angle_360( float ox, float oy, float px, float py)
// 180 -----±–X-> 0 degrees
// If point is at origin, then return zero radians. float vlen = sqrt( px*px + ox*ox); if( 0.0f == vlen) return 0; float vx = (px - ox) / vlen; float vy = (py - oy) / vlen; // Find the angle between -------X--> vector and "v" vector. // // Angle = (vx, vy) dot (1, 0) // ------------------------ //
I can not edit my post. but the “sqrt” line is completely wrong. It should be
float vlen = sqrt( (px - ox)*(px - ox) + (py - oy)*(py - oy));
I don’t know what I was thinking.
The whole post is messed, just go to
thanks a lot for your help!