# Oscilating between 2 values (in shader)

This topic is 4215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My aim is to have a float value representing a vertex's height oscillate between a minimum and maximum value smoothly over time. It will start at the minimum value at the start of the program. I have two floats MIN and MAX describing the minimum and maximum value of a vertex's y, and the float CHANGEPERSECOND telling us how much the vertex changes in a second. I also have the float AGE which tells us the age of the vertex. 1.0f being one second. 100.0f being 100 hundred seconds old, etc. So, given these details in the shader, what equation would give us the correct y value for our vertex? I appreciate any help. I am not a great mathematician, and I thought you guys would be the perfect ones to ask. I am looking to plug this into my shader (HLSL fx), so a reply which gives a little code snippet I could use would be greatly appreciated. There's a danger I may not understand a reply that uses some of the more complicated math notation. (I know, no genius here). [Edited by - Funso on March 9, 2007 11:48:34 AM]

##### Share on other sites
Do you want it to be wavey or jagged? If you're starting at min and want it to go to max, you could use something like:

aplitude = max - min

middle = amplitude/2.0f

Vertex.y = middle - amplitude*cos(2*pi*(age*frequency))

cos has a frequency of 2*pi, which is why that's there. As you can see if you want a frequency of 1, you'll get it oscillating once per second (that's from down to up and back down again). Using -cos because you said you wanted it to start at min.

If you want it to just be spiky, you'd want something like:

Vertex.y = min (in your preparation function).
updown = 1.0f;

Vertex.y += CHANGEPERSECOND * timedelta * updown

if (Vertex.y >= max)
{
updown = -1.0f;
}
else if (Vertex.y <= min)
{
updown = 1.0f;
}

That would be a basic way of doing it. When it hits the top, updown because -1.0f, so that the next frame it'll subtract the bit rather than adding it.

It's crude, but it'll work.

Timedelta should obviously be in seconds. If your timedelta is in milliseconds, divide it by 1000.0f.

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 18
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632605
• Total Posts
3007371

×