We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
billboarding math (Read 867 times)
billboarding math
Jan 11th, 2010, 8:51am
 
Hi everyone,

I'm trying to achieve billboarding with some particles in Processing. The particles are on the surface of a sphere, which can be rotated, and I want them to always face a particular point (ultimately the camera but I'm just trying to get the math right for now).

I can calculate the vector from the particle to the point, using Vec3D's getRotatedX and getRotatedY, and draw a line to it, which always points where it's supposed to. I then do some trig, perform the rotations and then draw a line out of the particle's Z-axis. If everything goes to plan that line should always point at the camera as well.

When the particles are just moving around on the surface, everything's OK. But the math just doesn't seem to work out when I rotate the sphere. I'm not sure what I should be calculating, to be honest. atan2 seemed right on paper but I'm struggling with this stuff and if anyone could help out or explain what I need to do here I'd really appreciate it.

What I've posted here is the closest to working. Both angles work fine on their own if I'm only rotating on that axis but start to go funny if I rotate both ways.

Code:

// get the camera position in the particles' coordinates
Vec3D track = cam.getRotatedX(xrot).getRotatedY(yrot);

pushMatrix();
// position of the particle
translate(position.x, position.y, position.z);

// vector from particle to camera
Vec3D delta = track.sub(position);

// draws fine all the time
line(0, 0, 0, delta.x, delta.y, delta.z);

// and here is presumably my problem...
float angleY = atan2(delta.x, delta.z);
float angleX = atan2(delta.y, delta.z);
               
rotateY(angleY);
rotateX(-angleX);

// draw line out perpendicular to the XY plane
// should be on top of the other line
line(0, 0, 0, 0, 0, 50);
Re: billboarding math
Reply #1 - Jan 11th, 2010, 11:38am
 
You should translate before you rotate, I think...

Something like this...
rotations = cam.getRotations();

pushMatrix();
 translate(150,0,0);
 rotateX(rotations[0]);
 rotateY(rotations[1]);
 rotateZ(rotations[2]);
 text("Billboarded text",0,0,0);
 popMatrix();
Page Index Toggle Pages: 1