Chaining movements

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Draculinio
Posts: 7
Joined: Sun Jan 08, 2023 1:03 pm

Chaining movements

Post by Draculinio »

Newbie question:

I want to make a simple jump, that is just moving the character up some substacting to y position and then fall adding to y position

so, let's say that first step will be something like this:

LDA $0200
CLC
SBC #$01
STA $0200
.
.
.
.


Then second step will be something like this:

LDA $0200
CLC
ADC #$01
STA $0200

When I put it all together I see the first part being executed but not the second part.
I am a complete newbie in this, started last week, learning how assembler works while doing and trying things

Thank you!!!
User avatar
Individualised
Posts: 310
Joined: Mon Sep 05, 2022 6:46 am

Re: Chaining movements

Post by Individualised »

Are you sure the second part is not executing, i.e. are you looking at the code in a debugger? If it Is being skipped over somehow, you'd need to provide us with whatever is happening in between those snippets of code. If you're not already using it, I recommend using Mesen as your NES emulator/debugger as it is a very extensive debugger compared to other emulators. There, you can step through the code and see each and every instruction that executes one by one.

Also, generally you want to set the carry flag before doing a subtraction, not clear it, though keep in mind I'm also just learning 6502 assembly myself currently.
Draculinio
Posts: 7
Joined: Sun Jan 08, 2023 1:03 pm

Re: Chaining movements

Post by Draculinio »

It i part of te same "function". So one part is after the other. Maybe something is needed in the middle

I will try the debug as you said and will bring what happened here.

Thank you!
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Chaining movements

Post by Fiskbit »

Do you think the second part isn't working because the sprite is moving up 1 pixel per frame? Your code currently subtracts 2 and adds 1, so the net movement is up 1. This is because SBC subtracts 1 more when carry is clear, the opposite of how it is for ADC. This is an artifact of how SBC is implemented in the CPU.

Instead, your code should SEC before SBC.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Chaining movements

Post by Oziphantom »

you can't make your game as a bunch of locked procedures. You have to make functions that enter and exit every frame and do the update the need. Otherwise when the player jumps the whole game will freeze.

Finite State Machines (FSMs) or what you need to research and understand. Then you have a state for jump up and a state for fall down. And to chain them you transition from one state to the next.
Draculinio
Posts: 7
Joined: Sun Jan 08, 2023 1:03 pm

Re: Chaining movements

Post by Draculinio »

Following all the advices you are giving me here, I am trying to go frame-by-frame, now something is missing and that is some kind of "wait" between frames. So, that is the next question, what is the best way for that?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Chaining movements

Post by tokumaru »

You have to think of the game logic in terms of "ticks", not as a continuous program. If you simply write all the actions in sequence, they will all be executed at once. Game objects normally have states, and in each state you only update the object 1 frame worth of actions.

In your simple example, you could have a "jumping" state and a "falling" state. The object logic, that runs once per frame, could look something like this:

Code: Select all

JUMPING_STATE = 0
FALLING_STATE = 1

  lda PlayerState
  cmp #JUMPING_STATE
  bne :+
  jump HandleJumping
: cmp #FALLING_STATE
  bne :+
  jmp HandleFalling
: ;(other state tests here)

HandleJumping:

  ; decrements position
  sec
  lda ObjectY
  sbc #$01
  sta ObjectY

  ;switch states if high enough
  cmp #$88
  bne :+
  lda #FALLING_STATE
  sta PlayerState
:

  ;finishes object logic
  rts

HandleFalling:

  ;increments position
  clc
  lda ObjectY
  adc #$01
  sta ObjectY

  ;switches states if low enough
  cmp #$90
  bne :+
  lda #STANDING_STATE
  sta PlayerState
:

  ;finishes object logic
  rts
Most games wouldn't handle jumps like this though, because jumping is not as much something an object does as it's the result of physics acting on it. In most games, in most logic states, objects will have the gravity acceleration added to their Y speed, and their Y velocity added to their Y position. Floor collisions prevent the object from sinking into the floor. Jumping is just a matter of setting the object's Y velocity to a negative value large enough to overcome the force of gravity for a while.

But generally speaking, chaining actions is all about object states, each with its own actions and logic for switching to other states.
User avatar
Individualised
Posts: 310
Joined: Mon Sep 05, 2022 6:46 am

Re: Chaining movements

Post by Individualised »

tokumaru wrote: Mon Jan 09, 2023 10:34 am Most games wouldn't handle jumps like this though, because jumping is not as much something an object does as it's the result of physics acting on it. In most games, in most logic states, objects will have the gravity acceleration added to their Y speed, and their Y velocity added to their Y position. Floor collisions prevent the object from sinking into the floor. Jumping is just a matter of setting the object's Y velocity to a negative value large enough to overcome the force of gravity for a while.

But generally speaking, chaining actions is all about object states, each with its own actions and logic for switching to other states.
I assumed that this was for something like an infinite runner (think Google Dino Run) so having smooth, physics-based jumps wasn't important.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Chaining movements

Post by Pokun »

Many pre-SMB games like Donkey Kong, Mario Bros and Kung-fu has very simple physics with constant jump arcs, movement speed and knockback. Also some later games like the Castlevania series continues using simple physics, so advanced physics is definitely not a requirement for a good game.

Acceleration-based physics like what SMB or Gimmick uses is not super hard but it's also not a beginner project. You need to learn signed comparisons and fixed point math among other things.
Fixed point math will probably be needed even with simple physics, because it allows you to move at speeds other than integer pixel/frame which is way too coarse. Thankfully fixed point math may be as easy as using two bytes for the value, one for the integer part and one for the fractional part and use normal 16-bit math with it (so the integer byte only increments/decrements when the fractional byte overflows/underflows).
Draculinio
Posts: 7
Joined: Sun Jan 08, 2023 1:03 pm

Re: Chaining movements

Post by Draculinio »

tokumaru wrote: Mon Jan 09, 2023 10:34 am Most games wouldn't handle jumps like this though, because jumping is not as much something an object does as it's the result of physics acting on it. In most games, in most logic states, objects will have the gravity acceleration added to their Y speed, and their Y velocity added to their Y position. Floor collisions prevent the object from sinking into the floor. Jumping is just a matter of setting the object's Y velocity to a negative value large enough to overcome the force of gravity for a while.

But generally speaking, chaining actions is all about object states, each with its own actions and logic for switching to other states.
Completely agree, I just wanted to start with something simple and start complicating it as I learn. It is a very interesting point of view.

Thank you!
Draculinio
Posts: 7
Joined: Sun Jan 08, 2023 1:03 pm

Re: Chaining movements

Post by Draculinio »

Individualised wrote: Mon Jan 09, 2023 12:33 pm
I assumed that this was for something like an infinite runner (think Google Dino Run) so having smooth, physics-based jumps wasn't important.
For now it is the only thing I need, Frame 1, the character is up, frame 2, the character is down.

From there I can do more things but first, step 1.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Chaining movements

Post by Oziphantom »

Draculinio wrote: Mon Jan 09, 2023 10:03 am Following all the advices you are giving me here, I am trying to go frame-by-frame, now something is missing and that is some kind of "wait" between frames. So, that is the next question, what is the best way for that?
the NMI is your "new frame" event. So you either call your code directly in the NMI, or set a flag in the NMI that it happened and then wait for the flag in the main loop, then clear it.

i.e

Code: Select all

.. in the NMI
inc NMIHappened
..exit the NMI



mainLoop
lda NMIHappened
beq mainLoop
lda #0
sta NMIHappened
.. call your update code here
Post Reply