# Check if body is on the ground

In my game I have an quite irregular terrain at the bottom and a round ball which can be controlled by the keyboard. Is there any way to check, if the ball is currently on the terrain? This is very important to know because the ball should only be able to be controlled when it is on the ground.

I tried to use the OnCollision event of the ball body, but it only gets fired on the first occurrence.... For my problem a continuous type of collision-detection is needed.

• I'm not familiar with Farseer Physics but i assume simple raycast from the center of ball to the ground should do the trick. At least, this is how people doing it with Bullet Physics. Do the raycast and then check if distance from ball's center to the collision point is equal to the radius of the ball. Make sure that your ray doesn't hitting the ball itself. Jul 22 '15 at 11:10
• @Ocelot Wouldn't that be like writing a second collison detection? It shouldn't be very CPU-intensive too.. Jul 22 '15 at 11:59
• As far as I know, properly implemented raycasts are pretty fast. Jul 22 '15 at 12:06

try OnSeparation.

Pseudo c# code:

private void OnSeparation(....
{
landed=false;
}
private void OnCollision(....
{
landed=true;
}


You could make a trigger at the bottom of the ball and then check if the floor is in this trigger.

• Box2D calls them sensors. Jul 29 '15 at 17:12

If there's gravity applied to your object you could check if the Y velocity==0 after you've done your collisions. It's maybe a bit fake it if you can't make it but in most situations it should work

I'm unfamiliar with Farseer, however in my own physics engine I handle this by getting an array of collisions which occurred in a given time-step and using the result of the Dot Product of the collision normal with the positive Y axis to see if any collision surface is within a certain angle of being parallel to the floor.

Raycasting is probably a better bet (especially if you are expecting many simultaneous collisions- the overhead from a check like this might be too much). Just putting an alternative out there- if this is possible with Farseer.