View Full Version : Detecting sphere moving out of platform ??
12-05-2011, 04:36 AM
Hey Folks !!
I am into serious trouble !!
As i am developing a small game, in which a sphere moves from series of platforms (basically rectangles).
As the sphere is out of those rectangles scope, it should fall down i.e, game must restart !!
Have implemented sphere falling down, But i 'm out of idea in detecting whether the sphere is on the platform or not !! I can hard code those checks, but it results in overhead !!
I 'm onto end of deadline of submitting my project, any ideas on how to implement it ??
Please !! REPLY SOON !!
12-05-2011, 05:09 AM
What does this have to do with OpenGL? Collision detection is something your application deals with. OpenGL is about putting things where you want them; it's up to your application to decide where they ought to go.
I can hard code those checks, but it results in overhead
Overhead? It's a sphere on some blocks. If doing that test requires any measurable CPU time, you're doing it wrong.
Also, can you end your sentences with a single period, instead of !! or ?? It makes your post needlessly difficult to read, and your overuse of !! isn't going to make people hurry up and answer you.
12-05-2011, 05:38 AM
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
- center C
- and radius r
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.
12-05-2011, 06:04 AM
Thanks for help Thokra & Alfonse, will make sure in future those mistakes wont be repeated.
I have done homework by myself, left with only this stuff as i am still newbie and this is my first project, finding it difficult to use the knowledge i have.
Thanks for guidance once again.
Will work on this and let you know the results.
12-05-2011, 07:48 AM
Hey Thokra, thanks for your reply.
But consider my situation.
| . |
Here . -> is sphere
From your pointers how can i check whether sphere is in the plane or out, as the sphere can move in any direction.
And one more thing, dot product of two orthogonal vectors is 0 as cos 90 is 0.
So, how your suggestions will help me ?
12-05-2011, 09:14 AM
First of all, the vector C - P will never be perpendicular to the normal unless the center of the sphere lies within the plane. But as soon as the sphere intersects the plane, the check will fail - this is long before the dot product will be 0.
I think you don't know how vector projection works. The point of projecting a vector onto another normalized vector is to get the length of the projected vector. If you project C - P onto N, you get the height of the sphere center above the plane.
The same goed for the projection of C - P onto X and Z. You'll get the x and z component of (C - P). With that you can easily check if (C - P).xz is within the the margins of the plane.
I really don't know how to explain it better.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.