It is currently Thu Apr 26, 2018 8:13 pm

 All times are UTC - 7 hours

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: In Game Physics, A Better Algorithm?Posted: Mon Apr 17, 2017 7:51 am

Joined: Wed Sep 21, 2016 12:08 pm
Posts: 79
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
STA X.Sub
LDA X
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
STA VX.Sub
LDA VX
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
STA Y.Sub
LDA Y
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
STA VY.Sub
LDA VY
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

 Post subject: Re: In Game Physics, A Better Algorithm?Posted: Mon Apr 17, 2017 8:59 am

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19966
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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 5 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki