# How can my program detect if the player hits a wall/floor ceiling

This topic is 1917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a little side scroller where this little guy bounce off of walls.

I am having this pretty big problem where I can't tell if the player is bouncing off a wall/floor/ceiling.

can anyone help me out with this?

Dartos

##### Share on other sites
I think we human, consider something as a wall floor or ceiling according to the orientation of the surface. so the normal.
a floor will have a normal with a y (vertical) component above sqrt(2)/2 (between -45 and 45°)
etc, doing 4 quadrants.
now if your issue was more about how to actually detect movement collision, that is another issue. you have the choice of using (time) sampled intersection tests, meaning you simulate your game in a real time loop, so you have a dt (that you can fix, cf. gaffer on games, fix your time step), at each dt you can test for box/box intersection with one box beeing the box of your character, and the other box a part of your level. you need to be able to cut your level elements into boxes. if you have slopes, then your intersection routine have to handle rotated boxes. When you detect an intersection, it is good to know the last known free position to reset the player there, and try to find an intermediate between this free position and this intersected position such that the new position is still free but closer to the wall.
Then the response part, determine a bouncing based on velocity and collision's object's normal.
you can use impulses or simply rough speed vector fiddling.

##### Share on other sites
Um.... if by boxes you are talking about a grid I have one of those. But that doesn't really help me tell if its on a wall or the floor.

##### Share on other sites
no, a grid should be used for lookup acceleration, i was talking about shape fitting. and to finish the idea I started up there:
wall left = normal.x > 0.7
wall right = normal.x < -0.7
ceiling = normal.y > 0.7
floor = normal.y < -0.7
where normal is the contact normal.

##### Share on other sites
Are you looking to specifically find out whether the player is bouncing off of a wall, a floor, or a ceiling?
Or are you just wondering how to handle collision vs. the wall, floor, and ceiling?

If it's the first case, you want to either give your walls, floors, and ceilings different properties telling them that they are in fact walls, floors, and ceilings. Alternatively, assuming all your objects are square tiles of some sort, you could find the shortest distance of penetration when the player collides and determine it that way (i.e. smallest penetration is x, and the player is to the left of this object = collision with right wall, smallest penetration is y and the player is above this object = collision with floor, etc.).

If it's the second case, use a bounding box check as stated above by Lightness.

##### Share on other sites
Assuming you could locate the point of contact in your collision test, consider this:

if the y coordinate of the contact point is depending on your origin gonna be at the top of the players bounding box if the collision is against a ceiling.
this inverse of the first case will be against a floor, the contact point is at the bottom of the bounding box.
if the point of contact is between the top and bottom of the players bounding box then the collision is against a wall.