Jump to content
• Advertisement

#### Archived

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

# Yet another collision problem.

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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?

#### Share this post

##### Share on other sites
Advertisement
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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632988
• Total Posts
3009737
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!