After finally getting a handle on some of the basics of 6502, I decided that I want to tackle something a little more ambitious for my next project. My first attempt at a super crude physics engine for a 2nd platformer. Nothing grand. Just a little white dot that can move back and forth, jump and land on a couple platforms.
If I can get three features working I'll consider it finished.
1. Collision Detection (I actually got that one working and I'm super proud of it, despite how crude and unoptimized it is since I can't figure out good ways to use the BIT opcode yet. So, I'm stuck using ham fisted Compare opcodes for now. I'll fix it later. I'm just happy that its working.)
2. Gravity (It actually works now but only independently from everything else. I'm still working out how attach it to the main game input loop without breaking everything lol)
3. Momentum (I haven't the foggiest idea how to pull this one off, but it'll be fun trying to find out.)
Once again, as with all my non-platform specific projects so far, this is designed to run on Nick Morgan's Easy 6502 virtual machine:
http://skilldrick.github.io/easy6502/
Keith's tools are freaking amazing when you need to work on actual real life hardware, but Nick Morgan's virtual machine is great to practice generic code due to its dirt simple "Baby's First Memory Map" setup.
I'm still figuring out the basics of the other subroutines, but in the meantime I coded a "framework" to seamlessly insert the collision and momentum subroutines later as I figure them out. Just to keep it all organized and save me the trouble later.
All it does so far is spawn a white dot and makes it fall, halting the program when it hits the floor.
Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initializing Physics test position, dot color and first spawn
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDX #$02 ;setting high and low byte of spawn
STX $02
LDX #$08
STX $01
LDA #$01 ;setting color
STA ($01),y ;spawning dot
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main Loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mainloop:
JSR gravity
JSR applymomentum ;Empty for now
JSR checkcollision ;Empty for now
JSR readkeys ;Empty for now
JMP mainloop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Gravity
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gravity:
LDX #$00 ;Clearing X register
CLC ;Clearing Carry
JSR delay ;Calling slow down delay
LDA #$00 ;Clearing last position
STA ($01),y
LDA $01 ;drop position 1 space
ADC #$20
STA $01
LDA #$01 ;redraw dot in new position
STA ($01),y
BCC returngrav ;if carry flag not set, fall again
LDA #$00 ;else, increment high byte, then back to fall
STA ($01),y
INC $02
LDX $02
CPX #$06 ;check if high byte is in range of display memory
BNE returngrav ;If Yes, continue falling
DEC $02 ;Else, Hit Floor and stop falling
LDA $01
SBC #$20
STA $01
LDA #$01
STA ($01),y
JMP end
returngrav:
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Placeholder Sub routines
;; To Be Populated Later
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
applymomentum:
RTS
checkcollision:
RTS
readkeys:
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Delay to allow human input and reaction time
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay: ;slowing down process
LDX #$a0
delayloop:
DEX
BNE delayloop
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Halt Program
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
end:
BRK