Jump to content
IGNORED

Collision with HIT_BACKGROUND question


Recommended Posts

Not sure if this makes any sense, but currently in my Space Taxi game I have a detection where  if (col0 and HIT_BACKGROUND) then...

 

Which works fine as pretty much hitting any background will kill you. 

 

But what if on one level I wanted one graphic to not kill you (without using a sprite that is)... is it possible to detect a certain graphic and have it not be detected.

 

here is the code I am using..

 

 if (col0 and HIT_BACKGROUND) then
    
   #col_chk2 = #backtab((X-3)/8+((#y % 256)-1)/8*20-20)
   
   if nextcar = 0 then crash = 1

  end if

 

 

Link to comment
Share on other sites

1 hour ago, Brian's Man Cave said:

Not sure if this makes any sense, but currently in my Space Taxi game I have a detection where  if (col0 and HIT_BACKGROUND) then...

 

Which works fine as pretty much hitting any background will kill you. 

 

But what if on one level I wanted one graphic to not kill you (without using a sprite that is)... is it possible to detect a certain graphic and have it not be detected.

 

here is the code I am using..

 

 if (col0 and HIT_BACKGROUND) then
    
   #col_chk2 = #backtab((X-3)/8+((#y % 256)-1)/8*20-20)
   
   if nextcar = 0 then crash = 1

  end if

 

 


The hardware collision only tells you that the object collided with the background, but nothing else.  To discriminate between background elements, you will have to first check the collision (like you are doing), and then calculate the BACKTAB position directly underneath (the one that caused the collision) based on the sprite's position, and check what card it uses.


I hope that helps.

 

   dZ.

Link to comment
Share on other sites

One complication to my suggestion above is that the sprite may not be aligned to a background tile.  That means that it may overlap up to four tiles at any one time.  You will have to check each one to see which one (or ones) were the ones that triggered the collision.

 

Here's some code, off the top of my head.  I haven't even compiled this, so consider it for illustrative purposes only.  To that end, I include plenty of comments to describe what it is doing and why.

 

' ============================================================================================
' NOTES:
'
'   1. ScreenPos() is a macro in the "constants.bas" library.
'
'   2. Constants "CS_CARD_DATA_MASK" and "HIT_BACKGROUND" are in "constants.bas" library.
'
'   3. Assuming you are using FG/BG mode.  If you are using Color Stack mode instead,
'      you must change the card mask constant to "CS_CARD_DATA_MASK".
'
'   4. Constant "DEADLY_CARD" is the card number of a background tile that causes a collision.
'      The value is adjusted for BACKTAB, so it's multiplied by 8.
'
'   5. Variables "posx" and "posy" hold the sprite's coordinates.
'
'   6. Subroutine HandleCollision() is just a place-holder to handle the collision.
'      Substitute it with whatever you want to do at that point.
' ============================================================================================

    ' The sprite collided with background card, but ...
    '   Is it a valid collision?
    If (col0 And HIT_BACKGROUND) Then

        ' A sprite is contained in an 8x8 pixel block.
        ' Each corner may overlap a background card:
        '
        '      +-------+-------+-------+-------+
        '      |       |       |       |       |
        '      |   BG  |  1########2   |   BG  |
        '      |       |  #        #   |       |
        '      +-------+--# SPRITE #---+-------+
        '      |       |  #        #   |       |
        '      |   BG  |  3########4   |   BG  |
        '      |       |       |       |       |
        '      +-------+-------+-------+-------+

        ' Reset data pointer
        Restore CornerOffset

        ' Iterate through the four corners of the sprite.
        ' Note that we compute the corners based on the sprite's
        ' origin.
        '
        ' Also note that this supports the worst-case-scenario
        ' where the sprite overlaps four distinct tiles.  If the
        ' sprite overlaps fewer tiles, the same ones will be tested
        ' multiple times.
        For I = 0 To 3
            ' Get corner offsets from sprite's origin.
            Read offx, offy

            ' Compute sprite's corner coordinates in terms of
            ' BACKTAB column and row position:
            column = ((posx + offx) / 8)
            row    = ((posy + offy) / 8)

            ' Get the card data for the BACKTAB tile directly
            ' underneath this corner.
            btab   = #Backtab( ScreenPos(column, row) )

            ' Test if the data points to a "deadly card."
            ' We do not care if the sprite hit multiple "deadly
            ' cards" at once -- the first one we find is sufficient
            ' to trigger a valid collision.
            If ((btab And FGBG_CARD_DATA_MASK) = DEADLY_CARD) Then
                ' The sprite hit a "deadly card"!!!
                '   Handle it and bail out.
                Gosub HandleCollision
                Exit Next
            End If
        Next
    End If

' Table of corner offsets from sprite origin.
CornerOffset:
    Data 0, 0   ' Corner #1
    Data 8, 0   ' Corner #2
    Data 0, 8   ' Corner #3
    Data 8, 8   ' Corner #4

 

This may not be the best way, and I'm sure it could be optimized, but I believe it should work for what you need.

 

Feel free to ask any questions.  :)

 

    -dZ.

Edited by DZ-Jay
Link to comment
Share on other sites

The collision flags are delayed, so it is possible that your sprite could cross over a background tile, and by now it isn't over the tile.


For these type of collisions is better to read the background cards instead of the collisiion flags.

 

Another simple solution could be to use a sprite for that background tile.

 

  • Like 1
Link to comment
Share on other sites

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