Jump to content

Recommended Posts

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......"
 

Link to comment
https://forums.atariage.com/topic/374374-cv-basic-collision-help/
Share on other sites

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.

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.

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.

 

 

 

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

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.

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.

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

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.

 

 

  • Like 1
  • 2 weeks later...

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...