In Game Physics, A Better Algorithm?

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
User avatar
Lucradan
Posts: 101
Joined: Wed Sep 21, 2016 12:08 pm

In Game Physics, A Better Algorithm?

Post by Lucradan »

I am trying to add a physics engine to a game I am making.

I defined the entity state by the following 16 BYTE VECTOR :

Position X => 2 BYTES (X and X.Sub)
Velocity X => 3 BYTES (VX, VX.Sub, VX.Direction)
Acceleration X => 3 BYTES (AX, AX.Sub, AX.Direction)

Position Y => 2 BYTES (Y and Y.Sub)
Velocity Y => 3 BYTES (VY, VY.Sub, VY.Direction)
Acceleration Y => 3 BYTES (AY, AY.Sub, AY.Direction)

All values are unsigned. The direction is either POSITIVE (1) or NEGATIVE (0).

Here is the algorithm I developed. I works, but I have the feeling it is way more complicated and inefficient than it needs to be. I'm looking for advice to make it better.

Code: Select all

LBL.UpdatePVA:
  LDA VX.Dir
  BNE LBL.SetXSubtract
    CLC
    LDA X.Sub
    ADC VX.Sub
    STA X.Sub
    LDA X
    ADC VX
    STA X
    JMP LBL.SetVX

LBL.SetXSubtract:
    SEC
    LDA X.Sub
    SBC VX.Sub
    STA X.Sub
    LDA X
    SBC VX
    STA X
    JMP LBL.SetVX

LBL.SetVX:
  LDA VX.Dir
  EOR AX.Dir
  BNE LBL.SetVXSubtract
    CLC
    LDA VX.Sub
    ADC AX.Sub
    STA VX.Sub
    LDA VX
    ADC AX
    STA VX
    JMP LBL.SetY

  LBL.SetVXSubtract:
    LDA VX
    CMP AX
    BNE LBL.SetVX.VXNEQAX
      LDA VX.Sub
      CMP AX.Sub
      BNE LBL.SetVX.VXNEQAXSub
        LDA #$00
        STA VX
        STA VX.Sub
        STA VX.Dir
        JMP LBL.SetY

LBL.SetVX.VXNEQAX:
   LDA VX
   CMP AX
     BCS LBL.SetVX.VXGTAX
       JMP LBL.SetVX.VXLTAX

LBL.SetVX.VXNEQAXSub:
   LDA VX.Sub
   CMP AX.Sub
     BCS LBL.SetVX.VXGTAX
       JMP LBL.SetVX.VXLTAX 

LBL.SetVX.VXGTAX:
    SEC
    LDA VX.Sub
    SBC AX.Sub
    STA VX.Sub
    LDA VX
    SBC AX
    STA VX
    JMP LBL.SetY

LBL.SetVX.VXLTAX:
    SEC
    LDA AX.Sub
    SBC VX.Sub
    STA VX.Sub
    LDA AX
    SBC VX
    STA VX
    LDA AX.Dir
    STA VX.Dir
    JMP LBL.SetY

LBL.SetY:
  LDA VY.Dir
  BNE LBL.SetYSubtract
    CLC
    LDA Y.Sub
    ADC VY.Sub
    STA Y.Sub
    LDA Y
    ADC VY
    STA Y
    JMP LBL.SetVY

  LBL.SetYSubtract:
    SEC
    LDA Y.Sub
    SBC VY.Sub
    STA Y.Sub
    LDA Y
    SBC VY
    STA Y
    JMP LBL.SetVY

LBL.SetVY:
  LDA VY.Dir
  EOR AY.Dir
  BNE LBL.SetVYSubtract
    CLC
    LDA VY.Sub
    ADC AY.Sub
    STA VY.Sub
    LDA VY
    ADC AY
    STA VY
    RTS

  LBL.SetVYSubtract:
    LDA VY
    CMP AY
    BNE LBL.SetVY.VYNEQAY
      LDA VY.Sub
      CMP AY.Sub
      BNE LBL.SetVY.VYNEQAYSub
        LDA #$00
        STA VY
        STA VY.Sub
        STA VY.Dir
        RTS

LBL.SetVY.VYNEQAY:
   LDA VY
   CMP AY
     BCS LBL.SetVY.VYGTAY
       JMP LBL.SetVY.VYLTAY

LBL.SetVY.VYNEQAYSub:
   LDA VY.Sub
   CMP AY.Sub
     BCS LBL.SetVY.VYGTAY
       JMP LBL.SetVY.VYLTAY 

LBL.SetVY.VYGTAY:
    SEC
    LDA VY.Sub
    SBC AY.Sub
    STA VY.Sub
    LDA VY
    SBC AY
    STA VY
    RTS

LBL.SetVY.VYLTAY:
    SEC
    LDA AY.Sub
    SBC VY.Sub
    STA VY.Sub
    LDA AY
    SBC VY
    STA VY
    LDA AY.Dir
    STA VY.Dir

RTS
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: In Game Physics, A Better Algorithm?

Post by tepples »

Direction can be stored in the high bit of velocity using two's complement, where $FF00 means -256. And you may not need to store acceleration separately, instead just adding something to velocity every frame.
Post Reply