Advertisement

Clipping in 2d

Started by November 08, 2000 04:19 PM
0 comments, last by Rev Conor 24 years, 2 months ago
Take any tile-based platform/side scrolling game, such as Mario or Megaman. Now notice the fact that the character can''t walk through solid objects, such as walls and floors. Even if you push against a solid wall, even if you are travelling at any angle, the player won''t move through. How the hell did the programmers get this to work efficiently? I''ve tried a couple algorithms but they''ve all got weird problems. I''m looking for the One True Way to do 2d collisions. NOT just the collision part (checking for overlapping rectangles, that''s trivial) but the _reaction_ to the collision. I.E. moving the player back to where they''re supposed to be. Basically, I''ve found that it all boils down to this: Given two intersecting rectangles, the player and a static nonmoving object; and given the player''s rectangle and velocity before the collision happened, how do you determine without error which side of the static rectangle the player hit first? My first real solution was to determine the centerpoint of the static rect and the player rect and check the angle between the two points. Then, that angle would be compared to two others: the angle between the top left side and center points of the static rect, and the angle between the top right side and center points of the static rect. (You don''t need to find anything else due to the symmetry of the static rectangle). However, this had one serious problem, which was that it would sometimes detect a top or bottom collision against a very small static rectangle. (Giving me an unwanted ''stair'' effect where the player would hop on top of thin bricks.) So, once again. Given the player''s rect, and a solid object static rect, as well as the player''s rect before the collision and the current x and y velocities of the player, how do you decide what side of the static rectangle the player hit first? Interpolation between collisions, keeping a history of player coordinates, backing up along a vector until there''s no more collision are just hacks. I wanna know if anybody has come up with a proven, mathematical answer for this. It''d certainly save my ass; I''m getting behind schedule pondering this one.
Do you allow sub-tile movement?

In most 2d side-scrollers like megaman & mario brothers you can''t stop anywhere between two tiles. You either move into a tile, or you don''t. The animation has several frames that animate the sub-tile movement, but you can''t stop half-way between tile 1 & 2.

  //psuedo...Get_user_Input();invisible_player=actual_player;pretend_move_player(invisible_player)if(!collision(invisible_player))really_move_player();else//do nothing!;  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement