Creation of a Damped Harmonic Oscillation

Hello Devs,

I am building an iPad app and a cube of scale 4 * 1 * 4 and the game is a top down view game. As the user, tilts the iPad I am trying to yield a Damped Harmonic Oscillation on the cube. Hence the oscillation would only be on 2 directions which is X Z. In layman terms, there would be a wobble on the cube.

Dissecting :

So if a user gives a input, I would calculate the target value and then apply it to a function which would calculate the required value and then oscillate the cube. I visualize to perform the oscillation by AddForceAtPosition on Vector3.down.

Can you devs help me with this?

Damped Harmonic Oscillation :

Additional Links for Reference:

http://phrogz.net/damped-spring-oscillations-in-javascript

http://wiki.unity3d.com/index.php/File:Mathfx-Bounce.png

Am I able to do maths forumla notation here? I don’t know…

The way I look at that graph is I see 3 functions.

2 quadratics, (by eye with a graph tool, but you could solve by algebra):
((x - 15)^2)/200
-((x - 15)^2)/200
1 sin wave
sin(x)

looking at your graph here is an approximation:

We are interested in the green line which is:
sin(5x)(((x - 15)^2)/200)

Assume 3 modifiers:
WobbleRate = 5
WobbleDecay = 15
WobbleAmplitude = 200

sin(WobbleRate * x) * (((x - WobbleDecay) ^ 2) / WobbleAmplitude)

And apply a cutoff for this function for input values of x < 0 and x > WobbleDecay.

float GetWobbleY(float t, float rate, float decay, float amp) // t = time
{
 t = Mathf.Max(0, t);
 t = Mathf.Min(decay, t);
 return sin(rate * t) * (((t - decay) ^ 2) / amp);
}

Use this function to see how much force to apply to the down vector y component.