# 2D sprites in 3D world...

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

## Recommended Posts

Hi guys. I'm having a slight problem. I want to create a sprite in my 3d environment that would always face the camera. I want to use this sprite for things like smoke and fire. What I do is I draw a quad thats on the y-z plane, then rotate it to face teh camera position using polar coordinates. But for some reason it never works.. all the angles seem to be messed up. Here's the code:

float dx = camera->_xCam - _x;
float dy = camera->_yCam - _y;
float dz = camera->_zCam - _z;
float r = sqrt(dx*dx + dy*dy + dz*dz );

//Calculate the rotation around the y and z axis
_yrot = 90-acos(dz/r)*180/PI;
_zrot = atan2(dy, dx)*180/PI;

glPushMatrix();
glRotatef(_xrot, 1.0,0.0,0.0);
glRotatef(_yrot, 0.0,1.0,0.0);
glRotatef(_zrot, 0.0, 0.0,1.0);
glScalef( _xscale, _yscale, _zscale);

if( hasTexture ){
glEnable(GL_TEXTURE_2D);
texture->activateTexture();
} else {
glDisable(GL_TEXTURE_2D);
}

glTexCoord2f( 0.0,0.0);
glVertex3f(0.0,-0.5,-0.5);

glTexCoord2f( 0.0,1.0);
glVertex3f(0.0,-0.5,0.5);

glTexCoord2f( 1.0,1.0);
glVertex3f(0.0,0.5,0.5);

glTexCoord2f( 1.0,0.0);
glVertex3f(0.0,0.5,-0.5);
glEnd();

glPopMatrix();


If anyone knows whats wrong or has a better way to draw these sprites. Please let me know. Thank you. Gavin

##### Share on other sites
I didn't look further than this line, but here:
90-acos(dz/r)*180/PI;
You're mixing radians and degrees, which renders the results meaningless. So I'd fix that first and see if it improves things at all (it may not, since there may be other errors as well).

##### Share on other sites
I just spent a good 5 minutes running through the Open GL forum looking for a previous reply I made not too long ago, only to realise that it was in the Graphics Programming and Theory section...

Anyway, here's the stuff you're looking for.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 21
• 19
• 11
• 13
• 17
• ### Forum Statistics

• Total Topics
631697
• Total Posts
3001763
×

## Important Information

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!