Archived

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

sakky

View angle of a billboard

Recommended Posts

I’ve been thinking about using billboards in my project to spice things up a bit. My question is how do you get the angle of an object from the angle of a viewer? Forgive me, I’m lacking in my math skills department right now. My best assumption is something like right triangle trigonometry. For my purpose, your view point would be the origin and you calculate the object’s angle as you would any other point in space. But I specifically remember a formula that finds an angle between to points in space. Isn’t it the dot product or something similar to that? I will also need to know the distance between the two objects so I can use the correct scaling. I know that I can use the Pythagorean formula to find the distance, so that problem is solved. I’ve forgot the formula for the angle between to points though.

Share this post


Link to post
Share on other sites
Well, I''m using this code I got from a site.... hope it werks for ya:

//------------------------------------------------------------------------------

// BillBoard routines

//------------------------------------------------------------------------------

void buildRot(float theta, float x, float y, float z, float m[16]) {
float d = x*x + y*y + z*z;
float ct = cosf(DEGTORAD(theta)), st = sinf(DEGTORAD(theta));

/* normalize */
if (d > 0) {
d = 1/d;
x *= d;
y *= d;
z *= d;
}

m[ 0] = 1; m[ 1] = 0; m[ 2] = 0; m[ 3] = 0;
m[ 4] = 0; m[ 5] = 1; m[ 6] = 0; m[ 7] = 0;
m[ 8] = 0; m[ 9] = 0; m[10] = 1; m[11] = 0;
m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;

/* R = uu'' + cos(theta)*(I-uu'') + sin(theta)*S
*
* S = 0 -z y u'' = (x, y, z)
* z 0 -x
* -y x 0
*/


m[0] = x*x + ct*(1-x*x) + st*0;
m[4] = x*y + ct*(0-x*y) + st*-z;
m[8] = x*z + ct*(0-x*z) + st*y;

m[1] = y*x + ct*(0-y*x) + st*z;
m[5] = y*y + ct*(1-y*y) + st*0;
m[9] = y*z + ct*(0-y*z) + st*-x;

m[2] = z*x + ct*(0-z*x) + st*-y;
m[6] = z*y + ct*(0-z*y) + st*x;
m[10]= z*z + ct*(1-z*z) + st*0;
}

static void BeginBillboard(void) {
glPushMatrix();
float mat[16];

glGetFloatv(GL_MODELVIEW_MATRIX, mat);
buildRot(RADTODEG( atan2f(mat[8], mat[10])*-1 ), 0, 1, 0, mat);
glMultMatrixf(mat);
}

static void EndBillboard(void) {
glPopMatrix();
}

Use BeginBillBoard(), draw the sprites, and then use EndBillBoard() . Those functions will calculate the viewing angle and make ur sprite always face the cam.

Share this post


Link to post
Share on other sites