Mike Lee Posted October 24 Share Posted October 24 I've been tinkering with Oscar's book and basically took example3 on page 19 and added 1 sprite in order to get collision down. The goal is to get collision detection from any angle but I'm missing something. I've gotten it "close" but it should be a lot better. I was using ABS for both x and y positions and that got me closer than I was. The crude code below is just something to help me understand collision detection better. Can anyone shed some light on what I should be doing in this case? I know it's just 2 sprites on the screen and I'd use a for/next loop for multiple sprite checks but for now I just want to get the basics down. ' Collision test start: CLS BORDER 4 PRINT AT 392, "Press ANY button" DO WAIT c = CONT1 LOOP WHILE c DO WAIT c = CONT1 LOOP WHILE c = 0 CLS ' Define sprite 0 as a smiling face, others are enemies DEFINE SPRITE 0,2,bitmaps ' Player/Enemy coords x = 120 y = 176 frownie1x = 20 frownie1y = 100 BORDER 0 main_loop: SPRITE 0, y - 1, x, 0, 10 SPRITE 1, frownie1y, frownie1x, 4, 8 WAIT ' Control Player IF cont1.up THEN IF y > 0 THEN y = y - 1 IF cont1.down THEN IF y < 176 THEN y = y + 1 IF cont1.left THEN IF x > 0 THEN x = x - 1 IF cont1.right THEN IF X < 240 THEN x = x + 1 ' Collision Detection IF ABS(frownie1y - y) < 8 AND ABS(frownie1x - x) < 8 THEN GOTO start GOTO main_loop bitmaps: BITMAP "......XXXX......" BITMAP "....XXXXXXXX...." BITMAP "...XXXXXXXXXX..." BITMAP "..XXXXXXXXXXXX.." BITMAP ".XXXXXXXXXXXXXX." BITMAP ".XXXXXXXXXXXXXX." BITMAP "XXXX..XXXX..XXXX" BITMAP "XXXX..XXXX..XXXX" BITMAP "XXXXXXXXXXXXXXXX" BITMAP "XXXXXXXXXXXXXXXX" BITMAP ".XXXXXXXXXXXXXX." BITMAP ".XXX..XXXX..XXX." BITMAP "..XXX......XXX.." BITMAP "...XXX....XXX..." BITMAP "....XXXXXXXX...." BITMAP "......XXXX......" BITMAP "......XXXX......" BITMAP "....XXXXXXXX...." BITMAP "...XXXXXXXXXX..." BITMAP "..XXXXXXXXXXXX.." BITMAP ".XXXXXXXXXXXXXX." BITMAP ".XXXXXXXXXXXXXX." BITMAP "XXXX..XXXX..XXXX" BITMAP "XXXX..XXXX..XXXX" BITMAP "XXXXXXXXXXXXXXXX" BITMAP "XXXXXXXXXXXXXXXX" BITMAP ".XXXX......XXXX." BITMAP ".XXX........XXX." BITMAP "..X.XXXXXXXX.X.." BITMAP "...XXXXXXXXXXX.." BITMAP "....XXXXXXXX...." BITMAP "......XXXX......" Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/ Share on other sites More sharing options...
+5-11under Posted October 24 Share Posted October 24 If the sprites were square, you could try <16 instead of <8 (number representing the sprite height and width). If you want to be generous for the player (slight touch doesn't count if enemy touches you), you could try <15 or even a lower number. If you want to be generous for the player (near touch counts for example if your bullet touches an enemy), you could try <17 or even a higher number. Because your sprites are round, you would need to adjust the numbers lower, such as perhaps the <8 you already have. If that simple <? doesn't work well enough for you, with the non-square shapes, you'll either need to make the math more complicated, or use the ColecoVision's collision detection. Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554412 Share on other sites More sharing options...
Mike Lee Posted October 25 Author Share Posted October 25 I've tried < 16 which worked ok on the right side of the sprite but the left was way off. It's my understanding ColecoVision doesn't have collision detection hence the need for ABS math. Can you provide more info on this? Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554458 Share on other sites More sharing options...
+5-11under Posted October 25 Share Posted October 25 Collision: I thought it had it. I don't see it in the Basic manual, though. I'd have to look in the C programming guide. It probably doesn't matter, but I like putting brackets around complex math, such as: IF (ABS(frownie1y - y < 8 ) AND (ABS(frownie1x - x < 8 ) THEN GOTO start Sprite0 also is in position y-1, but when you compare to the enemy location, there's no corresponding -1. Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554463 Share on other sites More sharing options...
+nanochess Posted October 25 Share Posted October 25 8 minutes ago, 5-11under said: Collision: I thought it had it. I don't see it in the Basic manual, though. I'd have to look in the C programming guide. It probably doesn't matter, but I like putting brackets around complex math, such as: IF (ABS(frownie1y - y < 8 ) AND (ABS(frownie1x - x < 8 ) THEN GOTO start Sprite0 also is in position y-1, but when you compare to the enemy location, there's no corresponding -1. Because of the 8-bit arithmetic allowed by CVBasic, I would recommend adding doing it like: ABS(frownie1y + 1 - y) < 8 The + 1 part of the expression "extends" the value to 16-bit, otherwise the signed 8-bit value isn't enough for the 256 pixel range. For example, if frownie1y is 0 and Y is 255 would fail without the + 1. Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554468 Share on other sites More sharing options...
Lillapojkenpåön Posted October 25 Share Posted October 25 I tried with a traditional hitbox smileycolor = 10 IF (y + 16) > frownie1y THEN IF y < (frownie1y + 16) THEN IF (x + 16) > frownie1x THEN IF x < (frownie1x + 16) THEN smileycolor = 8 END IF END IF END IF END IF It works great in my game with 16-bit variables, but I can't make it work with 8-bit variables for some reason, maybe you can figure it out Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554490 Share on other sites More sharing options...
Mike Lee Posted October 25 Author Share Posted October 25 22 hours ago, nanochess said: Because of the 8-bit arithmetic allowed by CVBasic, I would recommend adding doing it like: ABS(frownie1y + 1 - y) < 8 The + 1 part of the expression "extends" the value to 16-bit, otherwise the signed 8-bit value isn't enough for the 256 pixel range. For example, if frownie1y is 0 and Y is 255 would fail without the + 1. Thanks Nano. This did make a big difference but I am noticing some points in the collision where the 2 sprites are almost completely on top of each other before it detects it. Is it because the sprites are both round? I tried lowering the value 8 down but that seemed to make it worse so 8 seems to be the magic number. Is there anything else I can do to improve it? I'm not looking for pixel perfect, just close enough it doesn't look like an obvious hit but doesn't pick it up. Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5554995 Share on other sites More sharing options...
+5-11under Posted October 26 Share Posted October 26 1 hour ago, Mike Lee said: Thanks Nano. This did make a big difference but I am noticing some points in the collision where the 2 sprites are almost completely on top of each other before it detects it. Is it because the sprites are both round? I tried lowering the value 8 down but that seemed to make it worse so 8 seems to be the magic number. Is there anything else I can do to improve it? I'm not looking for pixel perfect, just close enough it doesn't look like an obvious hit but doesn't pick it up. It should work exactly as you expect it should, or there's something wrong. If the X values and Y values are within the range of each other, it should register as a collision. If your number is <8, you should be able to see that on the screen. It might be helpful to continually print all the numbers on the screen, so you can compare them, to make sure it's working as expected. Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5555035 Share on other sites More sharing options...
Mike Lee Posted October 26 Author Share Posted October 26 11 hours ago, 5-11under said: It should work exactly as you expect it should, or there's something wrong. If the X values and Y values are within the range of each other, it should register as a collision. If your number is <8, you should be able to see that on the screen. It might be helpful to continually print all the numbers on the screen, so you can compare them, to make sure it's working as expected. I'll try that. Thanks Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5555228 Share on other sites More sharing options...
Lillapojkenpåön Posted October 26 Share Posted October 26 This works perfectly IF ABS((frownie1y + 0) - y) < 16 AND ABS((frownie1x + 0) - x) < 16 THEN but the hitbox is square ofcourse, so the corners will seem to detect hits too early since there's no pixels there Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5555304 Share on other sites More sharing options...
+nanochess Posted October 29 Share Posted October 29 On 10/26/2024 at 9:23 AM, Lillapojkenpåön said: This works perfectly IF ABS((frownie1y + 0) - y) < 16 AND ABS((frownie1x + 0) - x) < 16 THEN but the hitbox is square ofcourse, so the corners will seem to detect hits too early since there's no pixels there You can reduce the value 16 to 12, or 10. 1 Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5557172 Share on other sites More sharing options...
Mike Lee Posted November 12 Author Share Posted November 12 Sorry for the late reply on this. Thanks for the input everyone. I got the results I was after and actually finished the little project. It was a good learning experience. Off to the next project! 2 Quote Link to comment https://forums.atariage.com/topic/374374-cv-basic-collision-help/#findComment-5564500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.