# Virtual Sphere

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

## Recommended Posts

I am implementing a virtual sphere for controlling a globe w/ a perspective camera. I am using the following function to project a 2d mouse point onto the surface of the sphere. This function assumes an orthogonal projection. Does anybody know how to modify this function to work with a perspective projection? 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 < 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); }

##### Share on other sites
That code is very obfuscated... I wouldn't try modifying it, just start over from scratch for the perspective projection version. Easier than trying to figure out whats going on with the current code. The calculations are going to be pretty different anyways.

first you need to calculate the ray from the camera
to do this you look at the mouse coordinates' proportional distance from center of screen to edge of screen and multiply these values against the x and y axis of the camera local matrix (leaving the z component at 1)
This should give you a vector that points in the correct direction
you might need to compensate if your camera view cone is not 45* to each side... this is simple trigonometry

next that ray needs to be intersected with the sphere
thats just a quadratic equation.. work out the math details on paper

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011471
• ### Who's Online (See full list)

There are no registered users currently online

×