Improve ObjectMove subroutines
From Sonic Retro
Revision as of 02:58, 28 June 2012 by KingofHarts (talk | contribs) (Was an optional step in one of RHS' guides, but that guide was already 12 steps... so I made this its own guide.)
(Guide written by redhotsonic)
Want to speed up your Sonic 2 hack?
In S2 and S3K, there's a subroutine called "ObjectMove" and another subroutine called "ObjectMoveandFall". ObjectMove allows objects to move about and "ObjectMoveandFall" does the same, except it applies gravity to it.
S3K has the same subroutines except the code is a bit shorter and therefore, it's faster. It may not look much faster, but again, with all the objects on screen, it has to apply it for every frame.
So, let's change our routines to work more like S3K's. To do this, it's really simple.
Go to "ObjectMoveAndFall:" and change this: <asm> ObjectMoveAndFall:
move.l x_pos(a0),d2 ; load x position move.l y_pos(a0),d3 ; load y position move.w x_vel(a0),d0 ; load x speed ext.l d0 asl.l #8,d0 ; shift velocity to line up with the middle 16 bits of the 32-bit position add.l d0,d2 ; add x speed to x position ; note this affects the subpixel position objoff_A(a0) = 2+x_pos(a0) move.w y_vel(a0),d0 ; load y speed addi.w #$38,y_vel(a0) ; increase vertical speed (apply gravity) ext.l d0 asl.l #8,d0 ; shift velocity to line up with the middle 16 bits of the 32-bit position add.l d0,d3 ; add old y speed to y position ; note this affects the subpixel position objoff_E(a0) = 2+y_pos(a0) move.l d2,x_pos(a0) ; store new x position move.l d3,y_pos(a0) ; store new y position rts
- End of function ObjectMoveAndFall
</asm>
To this: <asm> ObjectMoveAndFall:
move.w x_vel(a0),d0 ext.l d0 lsl.l #8,d0 add.l d0,x_pos(a0) move.w y_vel(a0),d0 addi.w #$38,y_vel(a0) ; apply gravity ext.l d0 lsl.l #8,d0 add.l d0,y_pos(a0) rts
- End of function ObjectMoveAndFall
</asm>
Next, go to "ObjectMove:" and change this: <asm> ObjectMove:
move.l x_pos(a0),d2 ; load x position move.l y_pos(a0),d3 ; load y position move.w x_vel(a0),d0 ; load horizontal speed ext.l d0 asl.l #8,d0 ; shift velocity to line up with the middle 16 bits of the 32-bit position add.l d0,d2 ; add to x-axis position ; note this affects the subpixel position objoff_A(a0) = 2+x_pos(a0) move.w y_vel(a0),d0 ; load vertical speed ext.l d0 asl.l #8,d0 ; shift velocity to line up with the middle 16 bits of the 32-bit position add.l d0,d3 ; add to y-axis position ; note this affects the subpixel position objoff_E(a0) = 2+y_pos(a0) move.l d2,x_pos(a0) ; update x-axis position move.l d3,y_pos(a0) ; update y-axis position rts
- End of function ObjectMove
</asm>
to this: <asm> ObjectMove:
move.w x_vel(a0),d0 ext.l d0 lsl.l #8,d0 add.l d0,x_pos(a0) move.w y_vel(a0),d0 ext.l d0 lsl.l #8,d0 add.l d0,y_pos(a0) rts
- End of function ObjectMove
</asm>
That's that. For any object that has to move, it will now do this coding a lot quicker each frame.