#### Archived

This topic is now archived and is closed to further replies.

# map collision detection

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

## Recommended Posts

##### Share on other sites
Looks like you'll want to use the "four corners" technique. What you want to do is check each corner of your sprite for a collision with a wall tile. This is best set up in a boolean return function, something like this:

{Let's assume 1 is used to denote a wall}
  function HitWall(Sprite: TSprite; Map: TMap;): Boolean;begin//Hitwall returns false by default Hitwall := false;//Check Upper-Left Corner Collision if Map.Grid[Sprite.X div Map.Tile.Width][Sprite.Y div Map.Tile.Height] = 1 then Hitwall := true;//Check Upper-Right Corner Collision if Map.Grid[(Sprite.X + Sprite.Width) div Map.Tile.Width][Sprite.Y div Map.Tile.Height] = 1 then Hitwall := true;//Check Lower-Left Corner Collision if Map.Grid[Sprite.X div Map.Tile.Width][(Sprite.Y + Sprite.Height) div Map.Tile.Height] = 1 then Hitwall := true;//Check Lower-Right Corner Collision if Map.Grid[(Sprite.X + Sprite.Width) div Map.Tile.Width][(Sprite.Y + Sprite.Height) div Map.Tile.Height] = 1 then Hitwall := true;end; * Keep in mind if you are using DelphiX's intrinsic sprites you might need to tuncate your sprite X and Y coordinates before attempting to div them as such: (trunc(Sprite.X) div Sprite.Width)* Note that the Hitwall function expects your sprite's width and height to be one less than that of your actual width and height. So if your Sprite's width is 16, that's actually 0 to 15, so the Sprite.Width used in the function should be 15 (just subtract one if necessary). If you use 16, and for example your tiles are 16x16, and let's say you are moving east, and you encounter a map section where there is a wall directly above your sprite, and directly below your sprite, you will be stopped. When instead you should have been allowed to pass through the opening between to top and bottom walls. (Did that make sense?)Anyways now you can use the Hitwall function to check whether or not the next move you make should actually happen, as such: var OldX,OldY: Double;...//Save Previous Coordinates OldX := Pacman.X; OldY := Pacman.Y;//Attempt to move Sprite Pacman.Move;//If Sprite hit a wall return it//to it's previous coordinates if Hitwall(Pacman, Board) then begin Pacman.X := OldX; Pacman.Y := OldY; end;...

Edited by - Xorcist on August 8, 2001 5:00:37 PM

##### Share on other sites
hm, sounds really logical. I''m going to try it as soon as possible ! It''s really interesting how "simple" this problem can be solved. If you just get the idea ...
Thank you very much for your answer, you helped me alot !

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002040
×