Skip to main content
GameDev.net gamedev.net
🔒 Locked

Yet another collision problem.

Started by Clancy Jan 11, 2002 at 2:33 AM 0 replies 550+ views
Original Post
Clancy
Clancy
I search this site for some collision code for a 2D game I am making, and everything posted will not work right. It is a tile base game, with the map : char MyMap[15][10]={ // tiles are 64x32 big 640x480=screensize {0,2,0,4,0,0,0,0,0,0}, {1,0,3,4,5,6,7,8,9,10}, ..and so on [0][0] = top left of my screen Now, my sprite (32x32 big) is at x=66 y=32. I do : int Cx=x/tilewidth; int Cy=y/tilehight; that equals Cx= 66/64=1 Cy=32/32=1. So far so good. since MyMap[Cy][Cx]==0. If I move up so that A)x=66, y=30, or if I move right so that B)x=127,y=32 then you can see the problem, since A) would be Cx=1,Cy=0 and B) Cx=1,Cy=1 which doesn''t take into account that it IS touching the non 0 map titles in both cases, since sprite is 32x32.
        1
  +-----+------+
2 + T1  +  T2  +  3
  +-----+------+
        4

case A)
  u**   1
  ***---+------+
2 cccT1 +  T2  +  3
  +-----+------+
        4

case B)
           1
      u*c---+------+
2     **c T1+  T2  +  3
      **c---+------+
            4
*=sprite
u=top of sprite (0,0)
c=collision here
 
If sprite comes down from 1 to 4 (A) it will not do collision until sprite passes the y coord of box (0,32,64...), and when sprite is going from 2 to 3(B), it will not do collision until sprite pass x coord of 0,64,128... To make long story short, what is best way to fix this problem? Would I have to do 4 checks for every tile? I mean, check sprite (0,0)[this is normal case] then check sprite (0+32,0)[this is the sprite moving RIGHT case B)] then check (0,0+32) [this is sprite moving down case A)] and finally (0+32,0+32)? Or am I doing this all wrong?
nickm
nickm
Nope, you''ll have to do four checks for collisions in this case. However, you don''t have to check for (0+32,0), but for (0+31,0), (0,0+31) and (0+31,0+31). For example, if map[2,2]=1, map[1,1]=0, and the player is at x=32 and y=32, a collision function which checks for 32+32 and 32+32 will output that there is a collision, and there isn''t.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.