Jump to content
IGNORED

Calculate 4 point collision...


Recommended Posts

Hi all,

Before I spent a lot of minutes (or hours) of thinking about how to calculate a 4 point collision in assembly, I'm hoping maybe somebody has some sort of out-of-the box standard code to do the following (like in Basic or Python):

 

if (player_x+player_width)>enemy_x AND player_x<(enemy_x+enemy_width)
 
AND
 
if (player_y+player_height)>enemy_y and player_y<(enemy_y+enemy_height)
 
Then...

The x axis is from 0-319 pixels, so in assembly there is a low X byte end a High x byte ?
The y axis is just 0-192.
 
 
Thanks!!
 
 
Link to comment
Share on other sites

It'd be tricky. The code essentially computes -s2 < x1 - x2 < s1, and the analogous check on the Y axis. The trick is that biasing the values by s2-1 gives -1 < x1-x2+s2-1 < s1+s2-1 in signed arithmetic, which can then be converted to x1-x2+s2-1 < s1+s2-1 in unsigned arithmetic. It's more annoying to do this in 16-bit due to the 6502's weakness at it and needing to compute A+B-C on one side.

 

However, the implementation is as short as it is because it assumes that both object sizes are constants. Otherwise, both sides of the comparison would need to be computed. If you can assume that, then you can also check -s2 < x1 - x2 or x1 - x2 < s1 directly, based on the sign of the x1 - x2 difference (in the carry flag). Which should be something like SEC/LDA/SBC/TAX/LDA/SBC for the subtraction, then BCC/BCS to choose the side to check, then CPX+SBC+BCC/BCS for each branch.

 

If it's also the case that you have a lot of asymmetric checks -- one player vs. many objects -- then you can also precompute u1=x1+s2-1, and then only check u1-x2 < s1+s2-1.

 

  • Like 4
Link to comment
Share on other sites

  • 4 months later...

If you want to make this easier, my recommendation to make this only an 8-bit operation would be to just use half the X coordinate in the comparisons for both sides. This depends if being off by a pixel matters much to you, but assuming none of the actors in the game are more than 64px in side (so 192+64 overflows 8-bit), then it'd greatly speed things up. If you're storing a 16-bit X, you'd have to adjust each by something like copying the 16-bit value then using LSR/ROR to divide by 2:
LDA PlayerX + 1
LSR A ; We don't care about A anymore since this just sets carry
LDA PlayerX
ROR A
STA PlayerX8Bit

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