# Virtual Sphere Question

This topic is 4679 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

##### Share on other sites
Hi... You may want to take a look at this thread. It discusses the same problem for orthogonal view, but the pseudo-code doesn't need to be altered, in order to compensate for perspective.
It's quite recent too, so you may want to keep an eye on it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634095
• Total Posts
3015502
×