Sign in to follow this  
MetaKnight

Direction to get to Point on Sphere

Recommended Posts

MetaKnight    258
OKay here is the problem: I have a sphere and an ant landed on top of it Collision keeps the ant from moving into the sphere I want the ant to go towards a point on the sphere I can get the direction vector, but unless it's close the ant just trys to go though the sphere. So how do I take this direction vector and make it a tanget vector on the sphere using the sphere normal?

Share this post


Link to post
Share on other sites
averisk    146
Well there might be an easier way, but the way I'd do it:

You can get the tangent plane at the point on the sphere where the ant is at by using the sphere normal as the plane normal and then the ant position as the point (plane is defined by a point and a normal)

Now, calculate the point on this plane closest to the desired point.

The new direction vector for the ant can be calculated by subtracting the ant position from this point and normalizing

Share this post


Link to post
Share on other sites
gamechampionx    140
The easiest way to go about this is simply use the vector from the sphere's center to the point of collision as the normal.

This is easiest of your sphere is centered at (0, 0, 0).

So, if the radius is 1 for example, then you have x^2 + y^2 + z^2 = 1^2 = 1.

So if the point of collision is (a, b, c), it obviously satisfies a^2 + b^2 + c^2 = 1. Now, the normal would be the vector (a, b, c), so the plane would be:

ax + by + cz = d, and you can get d easily since a = x, b = y, and c = z.
x^2 + y^2 + z^2 = d, so d happens to be the radius of the sphere squared.

Now, any vector on that plane will be tangent to the sphere. You can find the two direction vectors that define the plane by substituting two values of z, y and z, and finding the third. Obviously, if you have any zero coefficients, you would substitue their associated variables first to ensure the plane you're finding exists. Finally, knowing the first direction vector of the tangent plane (call it u), take u X (a, b, c) to find the second direction vector (v).

Any linear combinations of u and v, when added to the vector (a, b, c) will be on the plane tangent to sphere at point (a, b, c). Knowing that, you can move around the sphere, recalculating direction often, and any way you choose relative to the plane.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this