How To

Using SIN() to animate sprites


GAMELEGEND 2023-02-17 05:25 (Edited)

I was using SIN to create a range 33-35 and 49-51 to animate 2 sprites so I can have an animation that has 4 frames and make the 2nd character show up twice before the 1st frame gets shown again and I was wondering if it would be easier on the cpu to do what I did or to do (TIMER/n MOD 4) * 1 + char#

Time.nx | Open in app
2023-02-17 05:25

TheSailor 2023-02-17 07:02

I was just watching a YouTube video on how SIN and COSIN are definitely one of the game designers tools to use for enemy movement so it's interesting to see it used for animation as well 👍👍

G-9 2023-02-17 15:51

you can use modulo timer too

Timo 2023-02-17 18:59

The CPU cycle simulation of LowRes NX is very simple and even SIN/COS are just one cycle.

SP4CEBAR 2023-02-18 09:52 (Edited)


TIMER MOD 4 goes up in a linear fashion and after it reaches 3 it immediately goes down to zero

If you want it to work for negative numbers and decimal points, I learned from @McPepic that you can use TIMER - INT( TIMER / 4 ) instead

EDIT: TIMER - 4 * INT( TIMER / 4 )


SIN(A) wobbles smoothly between 1 and -1, starting at 0 at A=0, SIN() repeats itself at A=2*PI, and again at A=4*PI, and at A=6*PI, and so on
SIN(TIMER) won't be smooth because timer is an INT and it skips through the SIN() too fast
SIN(TIMER*0.01) will be way smoother, but it won't be easily predictable
SIN(TIMER*0.02*PI) will repeat itself at TIMER=100 which is more predictable

0.02*PI could be replaced by a constant variable that's equal to the same value in order to save some computation in your game loop

McPepic 2023-02-18 15:49

For the mod operation, you also need to multiply the number by 4 before subtracting the result from the timer. (Also, the timer is unsigned, so I don't think you need to use this method anyways)

SP4CEBAR 2023-02-18 16:54 (Edited)

@mcpepic thanks, chat doesn't have a compiler so I didn't realize my mistake

"Also, the timer is unsigned"

Yea, I didn't think about that either, I probably thought about a scenario where TIMER was multiplied by 0.1, but in that case you could probably use normal mod and multiply the 0.1 afterwards

Log in to reply.