Sine behavior

Get help using Construct 2

Post » Tue Nov 05, 2013 5:04 am

i looked and couldnt find an answer to my question:

basically i want a character to move vertically but not in a straight path. in another words, i dont want to character to go straight down but weave left and right as it goes down in a Sine wave...but dont know how to use the Sine behavior to get it done. any help or capx example would be GREATLY appreciated.

thanks a million, in advance.
B
136
S
42
G
10
Posts: 134
Reputation: 13,776

Post » Tue Nov 05, 2013 5:05 am

clarification...when i wrote above "weave left and right" i did not mean changing the angle the sprite is displayed (i know how to do that with the sine behavior) rather i meant actually change the path-traveled so its not straight.
B
136
S
42
G
10
Posts: 134
Reputation: 13,776

Post » Tue Nov 05, 2013 5:24 am

I was just about to post this question too and found this thread. I want to know the exact same thing. Basically I want an object to move straight while strafing left and right in a weaving motion. I only found "forwards and backwards" but wondering if there is a movement for "sideways".

Thanks
B
9
S
2
Posts: 7
Reputation: 914

Post » Tue Nov 05, 2013 12:29 pm

bullet movement downwards
sine movement horizonal
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Nov 05, 2013 1:48 pm

Cool. But is there a way for sine to move relative to the direction of the bullet? Like if it was moving at 45 degrees, can it do sine movement relative to its angle of direction?
B
9
S
2
Posts: 7
Reputation: 914

Post » Tue Nov 05, 2013 2:34 pm

Easiest thing to do that is without the sine behavior, but instead:

every tick: sprite set position to x = sprite.X+sin(time*360)*sin(Sprite.Angle) and y = sprite.y-cos(time*360-90)*cos(Sprite.Angle)

if you don't want all sines to be in sync, use an instance variable for each sprite and add dt every tick to it (so ones that you created later will not be in the same phase as the others)
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Nov 05, 2013 8:24 pm

in your formula: sprite set position to x = sprite.X+sin(time*360)*sin(Sprite.Angle) and y = sprite.y-cos(time*360-90)*cos(Sprite.Angle)....what does "time" mean? is that a variable i have to keep track off; or is that a C2 function that will supply some information automatically? also, if i want to make the movement more (or less) pronounced (more/less wiggle) what do i change in the formula?
B
136
S
42
G
10
Posts: 134
Reputation: 13,776

Post » Tue Nov 05, 2013 10:00 pm

sprite.X+sin(A*time*360)*sin(Sprite.Angle)*B

A = how fast it will swing, 1 = once up and down per second,l 2 = twice, etc.
B = how large the swing will be, this doesn't translate directly into pixels; I strongly suggest to use x*dt where x is some number to make it independable from frame rate.

Time is just the time elapsed in the game (this is affected by time scale (so if you pause and set the time scale to 0, the movement won't happen)). If you have further questions look up information about time and dt in Construct 2.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Nov 05, 2013 10:28 pm

I've refined it, so the amplitude becomes more predictable and I think the movement in general more reliable:

position every tick:
X: sprite.X+sin(A*time*360)*sin(Sprite.Angle)*B-sprite.lastx
Y: sprite.y-cos(2*time*360-90)*cos(Sprite.Angle)*B+sprite.lasty
after that set 2 instance variables
lastx = sin(A*time*360)*sin(Sprite.Angle)*B
lasty = cos(A*time*360-90)*cos(Sprite.Angle)*B

B is your amplitude, A your periodmindfaQ2013-11-09 02:21:18
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Wed Nov 06, 2013 2:48 am

mindFAQ you're the best. i have no idea what the math does; but it works and exactly as i wanted it. i have one last question: if i add the code to a sprite and give the sprite the bullet-behavior then the sprite moves left to right. i want to have it move vertically. how can i do that? that's the last part of the puzzle. can you please help? thanks again for all your help.
B
136
S
42
G
10
Posts: 134
Reputation: 13,776

Next

Return to How do I....?

Who is online

Users browsing this forum: Artcadev, kainrazial and 9 guests