0 Favourites

# Sine behavior

• 13 posts
• 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.

• 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.

• 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

• bullet movement downwards

sine movement horizonal

• 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?

• 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)

• 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?

• 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.

• 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 period

• ## Buy Construct 3

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

Construct 3 users don't see these ads
• 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.

• MindFAQ! Thank you for your help, and I have tried your formula, but something is not working and I am not sure where I went wrong.

Basically the enemies started oscillating in a uniform vertical direction really fast.

<img src="http://img706.imageshack.us/img706/9313/bqqb.png" border="0" />

(Strafer is my enemy sprite that I want the weaving to be implemented)

It seemed to work fine when I got rid of the A (period) and B (amplitude) modifiers and getting rid of lastX and lastY. Except when I try to modify its A and B, that's where it goes haywire.

I will post a video if you need/ don't quite understand what I am talking about.

Thanks : )

• 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.

I didn't see this. If you want it to fly into another direction with the bullet behaviour, just modify the angle or the angle of motion of your sprite. An angle of 90 will make it move downwards, 270 upwards. If you change the angle, your picture will be rotated, if you only change the angle of motion, the picture will not rotate.

@DRTIAN: the on created event only triggers once when the object is created. We need to modify lastx and lasty each tick after the set position ran (just place them after it). Also I had a typo in my formula, instead of the 50 at set y (not sure where the 50 came from), it needs to be B if I overview this correctly. Hope it works now.

• awesome, thanks a million mindfaQ, you're the best !!! thanks for all your help, it works like a charm now

• 13 posts
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
 Similar Topics Posts Views Last Post 0 Favourites [BEHAVIOR] Chipmunk Physics by 20 Jul, 2014 1 ... 43 44 45 673 82,521 3 Jun, 2018 0 Favourites [Behavior] LiteTween by 13 Jun, 2012 1 ... 43 44 45 672 169,926 12 Jun, 2018 0 Favourites [Plugin]Board, Layout2Board [Behavior] Grid Move by 7 May, 2012 1 ... 36 37 38 564 105,244 25 Dec, 2017