Animate a sine controlled npc

Get help using Construct 2

Post » Sun Sep 22, 2013 12:54 am

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!
B
68
S
24
G
11
Posts: 452
Reputation: 12,238

Post » Sun Sep 22, 2013 1:28 am

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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sun Sep 22, 2013 2:12 am

[QUOTE=tulamide] 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.[/QUOTE]

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?Draspur2013-09-22 02:26:31
B
68
S
24
G
11
Posts: 452
Reputation: 12,238

Post » Sun Sep 22, 2013 11:19 am

[QUOTE=Draspur]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?[/QUOTE] 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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279


Return to How do I....?

Who is online

Users browsing this forum: Demkamen and 13 guests