Making a crude physics engine in a generic 6502 machine.

Made something in Assembly? Show it off, and tell us how it works...
Absolute beginners welcome... we want to see what you've done!
Forum rules
There is a special rule on this forum...
This forum is intended to offer equal amounts encouragement and constructive feedback...
Therefore, if you say 2 negative things about someones work, you must think of 2 or more equally positive things...

Eg: "Great first effort, the idea is absolutely fascinating... However I noticed a few bugs, and maybe the graphics could be improved..."

If you can't think of anything good to say, then don't say anything!

If we don't encourage newbie programmers they won't have the confidence and motivation to stick at it and become great programmers! *speaking from experience*
Post Reply
EvilSandwich
Posts: 9
Joined: Wed Nov 13, 2019 7:36 am

Making a crude physics engine in a generic 6502 machine.

Post by EvilSandwich » Tue Nov 26, 2019 3:44 pm

I gotta say, its super fun trying something and failing again and again until that one magical moment when the code is finally lines up in a way that makes what you're trying to do actually kinda work. Having that happen to me a few times while learning 6502 in the last couple months, I can totally see the appeal of this hobby.

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
How's it looking so far?

User avatar
akuyou
Posts: 328
Joined: Mon Apr 22, 2019 3:19 am

Re: Making a crude physics engine in a generic 6502 machine.

Post by akuyou » Sun Dec 01, 2019 6:37 am

Looks great... it looks like you've got the code structure nicely worked out for what you have planned next.

That's a neat little website for testing your ASM code! I'm sure it'll make things easier to learn 6502 having the assembler/simulator all in one place - I tend to prefer working with offline tools, but these online resources certainly have great benefits.

I'm looking forward to seeing how your little physics engine proceeds!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

EvilSandwich
Posts: 9
Joined: Wed Nov 13, 2019 7:36 am

Re: Making a crude physics engine in a generic 6502 machine.

Post by EvilSandwich » Thu Dec 05, 2019 4:28 pm

It's intended to be more of a teaching tool than a serious assembler, if you're wondering why I don't really use equates or plus and minus offsets that you would usually see assemblers being able to do.

Oh and also I have more than a couple projects going on with that virtual machine. Would you prefer I keep all my projects to one thread, even if they're unrelated to the physics engine or should I have a different thread for each one?

Post Reply

Return to “Show and Tell”