PDA

View Full Version : PointOnUnitSphere for Virtual Sphere question



superahoy
03-29-2006, 08:54 AM
I am using a virtual sphere controller for an application rendering a globe. When using an orthogonal projection, I am able to grab a point on the globe and have it track with the mouse very tightly. When using a perspective projection, the point on the globe gets away from the cursor. I feel this is because my pointOnUnitSphere assumes an orthogonal projection. How would I go about using a perspective projection?

Here is the code I am using
P is a ground point, center is always 0,0,0 and radius is just the size of the virtual sphere I am using, in this case the radius of the globe.

private void pointOnUnitSphere(Point3d p, Vector3d v) {
double vx = (p.getX() - this.center.getX()) / this.radius;
double vy = (p.getY() - this.center.getY()) / this.radius;

double lengthSquared = vx * vx + vy * vy;

// Project the point onto the sphere, assuming orthognal projection.
// Points beyond the virtual sphere are normalized onto edge of the sphere (where z = 0)
double vz = 0.0;

if (lengthSquared &lt 1.0)
vz = Math.sqrt(1.0 - lengthSquared);
else {
// past the edge of the sphere. Normalize onto edge of
// the sphere
double length = Math.sqrt(lengthSquared);
vx /= length;
vy /= length;
}

v.setVector(vx, vy, vz);
}