random y position for automatic up/down using sine function
revoltandreverb
Member Posts: 159
I wanna make my actors go slightly up/down in the same spot. The background is spawning them using random(min,max) for x and y. On the actors itself I'm using self.Position.Y = 22"time"sin(game.Time"times"890)+384 to programmatically do an auto up down "loop" of an actor. As far as I can tell the 384 is the y position so I replaced the 384 with random(min,max) but then my actor goes up/down bezerk. I guess animating them would be an option but the sine function is much more convenient and looks sweet. I've used "times" since the asterisk is not showing.
Thanks!
Comments
Ok . . . .
Yep ! That sounds about right. This is what the expression is doing:
22 * sin ( game.Time * 890 ) + 384
'22 *' is multiplying the whole equation by 22.
'sin ( game.Time )' is the sine of the angle 'game.time' - and as game.time is constantly increasing then the angle than sin is operating on is constantly increasing, so sin is oscillating between -1 and +1, although like I say your '22*' bit is multiplying everything by 22 so in this case it is oscillating between -22 and +22.
You've also multiplied game.time by 890 times to speed up the angle on which sin is operating . . . if you left it as is (without multiplying it by something) then it would take 360 seconds (6 minutes!!) for the angle to make a complete revolution . . . so sin would take 6 minutes to go from -22 to +22 and then back down to -22 . . . and people would get bored, very bored.
And finally 384 is simply a number ! It happens to be half the height of a landscape iPad project, but it's still just a number . . . so if we have a value that is going from -22 to +22 then back down to -22 (and so on) and we add 384 on the end, then we've simply changed the result to a value that is going from 362 to 406 to 362 and so on . . . (which is 384-22 to 384+22 to 384-22 and so on).
So, adding 384 to our oscillating value will place it in the middle of a landscape iPad project . . . . but you don't want that, you want your position values to be random . . .
. . . . . . .
So . . . . spawn your oscillating actor randomly as you are doing at the moment . . .
Then in the actual actor that is being spawned make a 'real' attribute, call it whatever you like, but POS Y might be useful.
Then add a change attribute behaviour . . .
Change POS Y to self Y
This records the position it was spawned at into POS Y
Now in your constrain behaviour do this:
22 * sin(game.Time * 890)+POS Y
. . . . . . .
Socks! You are the man, worked perfectly... Thanks!!!
If you want to make them a little more random then I'd also recommend you change game.time to self.time . . . . with game.time they will all be moving up and down in sync with each other, with self.time they will be moving up and down depending on when they were spawned.
To make that variation even more pronounced you could add an 'integer' attribute into the actor, let's call it RandomOffset, then add a change attribute behaviour . . .
Change RandomOffset to Random (0,360)
Then change your constrain behaviour to this:
22 * sin ((game.Time * 890)+RandomOffset) +POS Y
Poetry lol. Thanks!