NES ASM Tips – Box Collision

One of the most important parts in any game, code wise, is to have some form of collision detection between sprites. This is Assembly Code snippet ( using NESASM3) should help you out when it comes to box to box collision and uses little space, so should be quite fast. We’re only using the A register AND 9 bits of RAM for simplicity.

Here we go, first with the variables being declared in Zero Page:

spriteCollider1Top = $40 ; SPRITE COLLIDER 1 TOP
spriteCollider1Left = $41 ; SPRITE COLLIDER 1 LEFT
spriteCollider1Width = $42 ; SPRITE COLLIDER 1 WIDTH
spriteCollider1Height = $43 ; SPRITE COLLIDER 1 HEIGHT

spriteCollider2Top = $44 ; SPRITE COLLIDER 2 TOP
spriteCollider2Left = $45 ; SPRITE COLLIDER 2 LEFT
spriteCollider2Width = $46 ; SPRITE COLLIDER 2 WIDTH
spriteCollider2Height = $47 ; SPRITE COLLIDER 2 HEIGHT

spriteCollisionResult = $48 ; SPRITE COLLISION RESULT - 0 = NO COLLISION, 1 = COLLISION

Next Up, The Function Itself, with an explanation of what we’re doing:

;COLLISION DETECTION BETWEEN SPRITES - RETURNS THE RESULT AS 0 OR 1
DetectCollisions:
PHA ; STORE OUR CURRENT A REGISTER ON THE STACK
LDA #$00
STA spriteCollisionResult ;INITIALIZE spriteCollisionResult to 0 (no collision found)
; START FIRST CASE
CLC                     ; CLEAR OUR CARRY FLAG
LDA spriteCollider1Left ; LOAD A WITH X1
ADC spriteCollider1Width ; ADD THE W1 TO X1
CMP spriteCollider2Left ; COMPARE X2 WITH A
BMI FinishCollisionCheckBetter ; IF GREATER, THEN SKIP THE REST, RETURN 0

; START 2ND CASE
CLC ; CLEAR OUR CARRY FLAG
LDA spriteCollider2Left ;LOAD A WITH X2
ADC spriteCollider2Width ; ADD W2 TO X2
CMP spriteCollider1Left ; COMPARE WITH LEFT OF X1
BMI FinishCollisionCheckBetter ; IF LESS THEN, SKIP THE REST, RETURN 0

; START THIRD BRACKET
CLC ; CLEAR OUR CARRY FLAG
LDA spriteCollider1Top ; LOAD A WITH Y1
ADC spriteCollider1Height ; ADD H1 TO Y1
CMP spriteCollider2Top ; COMPARE Y2 WITH A
BMI FinishCollisionCheckBetter ; IF LESS THEN, SKIP THE REST, RETURN 0

; START FOURTH BRACKET
CLC ; CLEAR OUR CARRY FLAG
LDA spriteCollider2Top ; LOAD A WITH Y2
ADC spriteCollider2Height ; ADD H2 TO Y2
CMP spriteCollider1Top ; COMPARE Y1 WITH A
BMI FinishCollisionCheckBetter ; IF LESS THEN, SKIP THE REST, RETURN 0

;THERE WAS A COLLISION! - STORE OUR COLLISION RESULT
LDA #$01
STA spriteCollisionResult 

FinishCollisionCheckBetter:
PLA ; RESTORE OUR CURRENT A REGISTER FROM THE STACK

RTS ; RETURN TO MAIN GAME LOOP

So, to use this effectively, simply set your sprite bits up appropriately, then Jump to the function (well, subroutine) by calling:

JSR DetectCollisions

If there is a collision between the two points, spriteCollision Result should contain a value of 1, otherwise it’s just a 0

Hopefully, this has helped someone out there get past one of the stumbling blocks with NES ASM development.

Happy coding everyone!

How To Get Started with Gameboy Development using GBDK

So, you’ve decided to make a Homebrew Gameboy Game using GBDK, Good for you! Let’s get you up and started with some of the vital tools that you’ll need.

  1. GBDK – GBDK Download & Install Guide
    ( I installed my copy to C:\gbdk and will refer to that location throughout this series of tutorials.)
  2. A Text Editor. Programmers Notepad is pretty nice and FREE
  3. BGB (Gameboy Emulator, again FREE and the best one out there.)

Please note that I’ve only done GBDK coding on my Windows Machine so some installation bits may vary depending on your choice of Operating System.

That’s it, that’s the bare minimum you’ll need to get started and get used to bare minimums, because the Gameboy, despite being a well-balanced machine, isn’t powerful, if you’re used to Unity or other modern development tools, this will be quite the step back. Let’s take a brief look at the specs

Yay Gameboy!CPU Speed: 4.19 MHz ( or 0.000419 GHz if you prefer)
RAM: 8kb – Expandable to 32kb depending on your ROM Carts settings

And we’re looking at ROM Sizes between 32kb to I think 1Mb. Not much space, and to start we’ll be dealing with 32kb ROM Size only.

Everything Graphical is Tile-based (8×8) and we have a screen resolution of 160 x 144 Pixels, In 4 shades of grey.

And yes, you can still make a game, running at nearly 60FPS on this, cool huh?

Tomorrow we’ll write our first program which will cover that most important thing, getting something on-screen with a few lines of code and running, it’ll be great!