Adding to Alfonse: Please don’t urge people to reply soon as this forum isn’t some paid technical support division and thus such a request will get you nothing.
Another thing: The posting guidelines explicitly state that no one here should be burdened with your homework and judging from your post, this is exactly what you’re asking here. Still, if you’re stuck and have little time left you shouldn’t fail if you’ve honestly tried.
I’ll try to help you from the top of my head. Please take a pen and paper and visualize the math if you’re getting nowhere. Also, I’ll only give you the basic math to make sure the sphere is above the plane and the projected center is inside the plane margins. This won’t look to good, as a falling sphere will penetrate the plane. You need to invest some thoughts in how to let is smoothly drop of the planes.
Assuming your planes are rectangular and are all situated in the xz-Plane of the euclidian space, they may be defined by
- center P
- a surface normal N (normalized!)
- and scaling factors sx and sz
and a sphere with
the first question is if the sphere is above or on the plane.
This is true if the length of the projected difference vector of the sphere and the plane center is greater than or equal to the sphere radius(showing in code section for better layout):
dot((C - P), N) >= r
If this check fails, you don’t need to perform further computations. A dot product is cheap, especially if you have only few objects in the scene. The next step isn’t much more expensive either.
If the sphere is still above or on the plane, you can simply do two more checks to verify that the projected center is still inside of the plane’s margins.
Since your planes span within the xz-plane, you can project the difference vector onto the x and z base vectors
- X = (1, 0, 0)
- and Z = (0, 0, 1)
If the absolute length of the projected difference vector onto X and Z is smaller than sx and sz respectively, then the sphere center is inside the plane margins. This, of course, assumes that the plane is a rectangle and that it is equally sized with respect to the center point(i.e. abs(-sx * X) = sx * X).
abs(dot(C - P, X)) <= sx && abs(dot(C - P, Z)) <= sz
This can be simplified to the following function(in GLSL syntax):
vec3 Difference = C - P;
if(dot(Difference, N) >= radius)
return abs(dot(Difference, X)) <= sx && abs(dot(Difference, Z)) <= sz;
This can be further simplyfied since we know that we’re always dealing with the euclidian basis represented by (X, N, Z) but you should first try to understand the general case before optimizing it for this special case.