How can my program detect if the player hits a wall/floor ceiling,
Members - Reputation: 157
Posted 14 October 2012 - 08:37 AM
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,
Members - Reputation: 912
Posted 14 October 2012 - 09:10 AM
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.
Members - Reputation: 912
Posted 14 October 2012 - 12:46 PM
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.
Members - Reputation: 534
Posted 18 October 2012 - 03:51 PM
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.
Members - Reputation: 952
Posted 18 October 2012 - 08:20 PM
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;