Animate a sine controlled npc

0 favourites
  • 4 posts
From the Asset Store
Fantasy RPG Human-NPC assets pack of 15 characters
  • I have a horizontal bird flying back and forth through sine, but I'm trying to find an effective way to flip it. No condition under sine seems to indicate the switch between forward and backward. Is there any way to do this without stupidly inefficient methods (Like having objects at the end of the bird's path to switch it or trying to time it).

    Thanks!

  • You can just do your own sine-based movement. You need a variable (here 'sinangle'). Set it to 0 initially.

    Calculate the center of the movement. Let's say x=200, while the total width of the movement is 300, for this example. The radius is half of the width = 150.

    every tick -> add n*360*dt to sinangle

    bird -> set x to 200 + sin(sinangle) * 150

    that will make it fly back and forth from 50 to 350. You have to experiment with n, it is the speed factor.

    Now that it runs, check sin(sinangle) for being close to +1 or -1. If it is +1 (or close to, might not exactly hit 1) the movement to the right is at its climax and will now turn to the left. If it's -1 exactly the other way round. Remember to use 'once while true' for those comparisons. That's all.

  • You can just do your own sine-based movement. You need a variable (here 'sinangle'). Set it to 0 initially.

    Calculate the center of the movement. Let's say x=200, while the total width of the movement is 300, for this example. The radius is half of the width = 150.

    every tick -> add n*360*dt to sinangle

    bird -> set x to 200 + sin(sinangle) * 150

    that will make it fly back and forth from 50 to 350. You have to experiment with n, it is the speed factor.

    Now that it runs, check sin(sinangle) for being close to +1 or -1. If it is +1 (or close to, might not exactly hit 1) the movement to the right is at its climax and will now turn to the left. If it's -1 exactly the other way round. Remember to use 'once while true' for those comparisons. That's all.

    Edit: I was able to adjust some of the variables, and go it to work perfectly. Thanks! One question, why did you use n*360, why 360?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Edit: I was able to adjust some of the variables, and go it to work perfectly. Thanks! One question, why did you use n*360, why 360?To make it easier to think in logical units. 360? is one full circle. Easy said, sine describes the position of the y-ordinate of a point on the unit circle at any angle.

    So, 360? represent one full cycle (in the bird's case one full movement from center to the right to the left and back to center). Multiplying with dt means "one full cycle per second".

    Now you have a good base to precisely control the speed.

    If n=0.5 it's half a cycle per second (or one full cycle every two seconds)

    If n=2 it's 2 full cycles per second

    etc.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)