Skip to main content
GameDev.net gamedev.net
🔒 Locked

Virtual Sphere Question

Started by superahoy Mar 30, 2006 at 10:10 AM 1 replies 400+ views
Original Post
superahoy
superahoy
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); }
haphazardlynamed
haphazardlynamed
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
someusername
someusername
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.