• Create Account

## Making simple floating/drifting animation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Ilmiont  Members

211
Like
1Likes
Like

Posted 28 August 2014 - 01:12 AM

Hello all,

I'm a bit stumped creating something realistic.

We've all been to the sea and noted that little bit of seaweed gently floating around in the water... never really getting anywhere, just aimlessly moving back and forth.

How could I recreate that in code? Specifically, for a jellyfish controlled by the player so you can move it, slowly, but when you're not moving its just "floating" about. Ideas?

Ilmiont

### #2Charon  Members

661
Like
0Likes
Like

Posted 28 August 2014 - 05:19 AM

What about a time-based function which calculates a sinus-waveform?

You can then add its output to your jellifishs' x and/or y coordinate whenever you are not moving it yourself. (you maybe have to scale the output for both axis differently so it looks nicer)

Regards

Markus

chaos, panic and disorder - my work here is finished

### #3Ilmiont  Members

211
Like
0Likes
Like

Posted 28 August 2014 - 07:08 AM

Woah, sorry I'm lost. I'm not too hot on the maths stuff, is it possible you can explain it in simpler terms? *Heads to Google*

### #4Norman Barrows  Members

6307
Like
1Likes
Like

Posted 28 August 2014 - 07:42 AM

What about a time-based function which calculates a sinus-waveform?

Woah, sorry I'm lost. I'm not too hot on the maths stuff, is it possible you can explain it in simpler terms?

this is one common way to do it.   in fact, it might qualify as a "game design pattern" ! <g>.

a simple example:

lets say your jellyfish's position is given by x,y,z.

then you have a frame counter, or timer, that increments over time, giving you an elapsed time value et.

to make the jellyfish "float", you calculate the jellyfish's final position as:

x_final = x+sin(et)

y_final = y+sin(et)

z_final = z+sin(et)

to mix it up a bit, in some of the formulas you can use cos() instead of sin(), you can multiply et by a constant, and/or multiply sin(et) or cos(et) by a constant.

sin() and cos() will be 1/4 cycle out of phase with each other. multiplying ET by a constant will shift the phase of the wave. multiplying sin(et) or cos(et) by a constant will change the amplitude of the floating action (change how far it moves back and forth).

you'll probably want to multiply sin(et) or cos(et) by the same or similar values, so it floats about the same amount in all directions.

mixing it up a bit you might end up with something like:

x_final = x + c1 * sin(et * c2)

y_final = y + c1 * cos(et * c3)

z_final = z + c1 * sin(et * c4)

where c1 through c4 are constants whose values are determined by experimentation.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #5Olof Hedman  Members

5701
Like
1Likes
Like

Posted 28 August 2014 - 08:15 AM

And to make it look a bit more realistic, you can add together several sin-waves with different frequency and amplitude.

something like this:

x_final = x + a1 * sin(et * f1) + a2 * sin(et * f2) + a3 * sin(et * f3) + ...

Though, in code, this is best done in a for loop, then you can add a bunch of them together.

The biggest amplitude and frequency constants is the most important to tweak, then you can calculate the rest by random subdivision. ( get random value between 0.3-0.7 (or something), then calculate f2 = f1*rand_value, and so on)

Edit: Actually, for each step, you want to increase the frequency, but decrease the amplitude. So you get longer big waves, mixed with shorter small waves.  so for each step multiply the f-values with a random value around 2, and the a-values with a random value around 0.5

Edited by Olof Hedman, 28 August 2014 - 01:39 PM.

### #6aregee  Members

1072
Like
1Likes
Like

Posted 28 August 2014 - 12:13 PM

Notice that the sine functions takes radians.  That means a full rotation, or period is 2*PI radians.  That again means that if you want 360 steps before completing a full circle, or period, you need to divide by 360.

So 360 steps per second, with time t and amplitude 10 will be:

(Edit: 360 steps per second would mean 1/360 seconds per step, and you would still have one revolution per second)

position = x + 10 * sin(t * ((2 * pi) / 360))

For the other dimensions, y and z, you may perhaps want to have another phase, which is just to add a value between 0 and 2 * PI inside the sine function.  This prevents the wiggling to be on a line in the space or plane you want to move.

If you want to have a certain amounts of wiggles per second, you can instead just multiply with 2 * pi instead, like this:

position = x + 10 * sin(t * 2 * PI * wiggles)


If wiggles is 10, you will have 10 wiggles per second, if wiggles is 1/2, you will have half a wiggle per second.

The general function is:

f(time) = offset + amplitude * sin(time * 2 * PI * frequency + phase)


Edited by aregee, 28 August 2014 - 12:21 PM.

### #7TehWardy  Members

145
Like
0Likes
Like

Posted 03 September 2014 - 09:20 AM

Lol this feels like another perlin like conversation

Gotta love waveform based math ... so not easy !!!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.