This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi all,
I'm working on a video game and am trying to make a shader for it that makes my in game trees respond to wind.
I have one formula that tries to do this and almost succeeds. My problem is that it's been 20 years since I've needed to look in to mathematical formulas.
I have, in game, a windX and windY speed. These are used to move vertex positions (sort of like a picture frame for the sprite to sit on) on my sprites.
So the formula I have handles three things:
1- vertex X movement:
windEffectX = windX * sin(time offset);
time and offset are generated whilst the game is running. That white arrow for wind speed gradually changes position in game and so do the trees as a result.
2- 'Pinching' of x positions:
maxGap = 4.0; //max range to deflect x position (bending trees)
gap = -windY * maxGap; //calculate the current pinch
gap = clamp(gap, -maxGap, maxGap); //clamp to prevent skewing too far
The idea here is that if the windY speed increases, wind blows the tree towards the camera, so the top vertices are supposed to drift apart and make the top half of the tree look bigger, and the inverse is true too. The "clamp" function just prevents the variable from going above 4 or below -4, so I don't accidentally end up with giga trees.
3- Bending back and forth:
bendX = mix(windEffectX, windEffectX / 2.0, 1.0);
So the idea with this is that the higher the wind speed, the faster the x position will drift between its current position and half of that. To make it look like the trees are bending in the wind. "mix" is a function that interpolates between the first and second values at the rate of the third value.
As it is so far, the x positions of each sprites top vertexes move and pinch, but i just can't get the bending part working and I'm not sure why.
This is the formula that is then applied to each of the individual vertex positions:
top left vertex
newPosition = vec3(windEffectX gap bendX, windY, 0.0);
top right vertex
newPosition = vec3(windEffectX - gap bendX, windY, 0.0);
The code requires a 3 dimension vector to update each vertices x, y and z positions in game.
I've been working on this for nearly a week straight and might have tunnel vision on it. Any help would be greatly appreciated.
Thanks
Here's a screenshot too:
Subreddit
Post Details
- Posted
- 6 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/askmath/com...