**0**

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

Started by infectedbrain, Oct 14 2012 08:37 AM

5 replies to this topic

###
#1
Members - Reputation: **157**

Posted 14 October 2012 - 08:37 AM

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?

thanks in advance,

Dartos

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?

thanks in advance,

Dartos

Sponsor:

###
#2
Members - Reputation: **868**

Posted 14 October 2012 - 09:10 AM

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.

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.

###
#4
Members - Reputation: **868**

Posted 14 October 2012 - 12:46 PM

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.

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.

###
#5
Members - Reputation: **534**

Posted 18 October 2012 - 03:51 PM

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.

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.

###
#6
Members - Reputation: **938**

Posted 18 October 2012 - 08:20 PM

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.

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.

you know you program too much when you start ending sentences with semicolons;