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);