# How do I make the sin wave vary in speed?

Get help using Construct 2

### » Fri Mar 28, 2014 3:38 am

Ive got a platform that goes up and down following the sin behavior and adding a certain magnitude.

I've got the platform atm moving in a vertical motion but i want to make it vary in the speed.

B
7
S
2
G
3
Posts: 31
Reputation: 2,446

### » Fri Mar 28, 2014 4:17 am

Reduce the period. Period is how long it takes to do 1 full cycle, so a smaller period will make it faster
B
87
S
22
G
15
Posts: 1,041
Reputation: 15,343

### » Fri Mar 28, 2014 5:05 am

Ah i should've rephrased that. I mean to make the speed of it be random. slow then fast etc
B
7
S
2
G
3
Posts: 31
Reputation: 2,446

### » Fri Mar 28, 2014 8:31 am

In you event sheet add something like

every X seconds -- set sine period to Sprite to random(range required)

or, maybe better ?

every X seconds -- set sine period to Sprite to Sprite.Sine.Period+random(4)-2 (which will alter the period by +/- 2 seconds

you might also want to set some limits so it doesn't get too fast/slow

(not tested)
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,902

### » Fri Mar 28, 2014 11:51 am

I don't use the built-in behaviors, so this is how I would do it.

One way to write the wave equation is:

D(x,t) = A sin(kx - wt + [theta])
where w = 2(pi)f

D is the displacement from the center, k is the spring constant, x is the x position on the wave, w is [2*pi*f], f is the frequency, t is the time, and theta is the phase angle.

Since we're only concerned with one spot on the wave and not the entire thing we can let theta be zero and x be pi/2 (the maximum of a sin wave). However, if you had multiple objects you wanted to randomly wave together but not in perfect sync the easiest way to accomplish this would be by playing with the phase angle.

Now, for the behavior you asked for, I think the easiest approach would be to vary the spring constant k and not the time t. All you would need to do is solve the wave equation on every frame (remember to use a proper delta-time value and calculate the current time rather than just counting frames) and move your object to whatever displacement you come up with. When you want to change how fast it's moving you can change the spring constant k value (which isn't so constant in this case....) and it'll start moving at the new speed on its own.
B
9
S
3
Posts: 37
Reputation: 749

### » Fri Mar 28, 2014 3:10 pm

now tested...

(latest beta can be found at the bottom of most pages)
You do not have the required permissions to view the files attached to this post.
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,902

### » Sun Mar 30, 2014 1:34 am

Hey guys thanks for the replies. All posts were helpful!
B
7
S
2
G
3
Posts: 31
Reputation: 2,446

### » Mon Mar 31, 2014 6:24 am

Another question @RamPackWobble, Is it possible to not have a range of random values? ie instead of 1-5 be its random between 1, 4, 5 or is this restricted for the clamp function?
B
7
S
2
G
3
Posts: 31
Reputation: 2,446