It is currently Wed Oct 18, 2017 12:48 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Apr 17, 2017 7:51 am 
Offline
User avatar

Joined: Wed Sep 21, 2016 12:08 pm
Posts: 33
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:
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


Top
 Profile  
 
PostPosted: Mon Apr 17, 2017 8:59 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19093
Location: NE Indiana, USA (NTSC)
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group