Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to replace my old integer based movement and collision system with a vector system, because its more precise and more flexible.

I want to implement wall sliding, such that if a player hits a wall diagonally, he will still slide along it.

The problem I'm running into is that all my hitboxes are just that- boxes. I use Rectangle2D to represent everything, and I don't know how to find the right normal.

See example

If the blue box is the player colliding with the red box wall, I'm not sure how to find the correct normal to subtract from the player's movement vector. Since the wall has 4 possible normals, I'm not sure how to quickly get that normal. I think I can do it using a bunch of distance tests to get the nearest two points, but that seems inefficient and possibly error prone.

Is there some better, faster way than just testing each point for distance and using the nearest two to create my vector?

share|improve this question
1  
Can't you simply check the position of the player compared to the wall? The normals don't change, they are either (-1,0)( left edge) , (1,0)(right edge), (0,-1)(top edge) (0,1)(bottom edge). And I have the feeling you're talking about the penetration depth/Resolution? You want to know how much you should push back the player? –  Sidar Sep 6 at 15:17
 
Not exactly - I want to alter the player's direction of movement so that he moves parallel to the wall after collision, instead of moving through the wall or stopping. To do that you have to remove the normal from the movement vector of the player. The question is determining which normal to use. –  BattleBarnes Sep 6 at 15:28
 
That said, my walls are all oriented along the x and y axis (no diagonal walls) which hadn't occurred to me, so all my normals will be the 4 you listed. That would make the distance test more reasonable. I was thinking that wouldn't work if there were diagonal walls, but there aren't. Thanks! –  BattleBarnes Sep 6 at 15:29
 
Like I said, you want the penetration depth, not the normal? You want to push your character out of the wall onto the edge. The normal vector is the vector that is perpendicular to said vector. The normals themselves don't exactly tell you how much they have to push back. All they do is give you a direction. As for Diagonal walls, if you are still interested, it's the slope vector ( subtraction end point of the slope from the startpoint of the slope ) and then you swap the components and negate one like so : (-y,x) or (y,-x). This is the vector perpendicular to the wall line. AKA the normal. –  Sidar Sep 6 at 16:14

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.