How to create a damped harmonic oscillation?


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.


The game is face negative Y axis or mathematically the camera is tilted 90 degrees across x axis.

Dissecting :

Considering just one direction, the graph would be like this -

alt text

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?

Thank you & Pleaseā€¦



Additional Links:

Hiya, so the graph you want is a SIN or COS wave, so you take time.Time, use it as a ramp, multiply time.Time by a multiplier number to get the speed you want on the sine oscillations, and then multpiply the sine itself by an envelope, in your case it is abit like an exponentially dampening/waning envelopeā€¦ you can make the waning envelope with some cheap exponential functions from the net, for example one way of making an curved decrease like that, is by taking a ramp (using a version of time.Time that resets to zero every time you want the envelope to retrigger) and doing a sqrt or a cube root or a square or a cube, like this :

link text

In other words, take time.time, sine or cos it, multiply the result by an envelope equation also based on a function of time.time.

if you want different speeds and amplitudes and periods for x and y axes of the crate, do 2 versions of time dot time, with different amounts added and multiplied to them, and apply each to Sin and then to x and y.

if you decide that sin and cos arent exactly right, you can pass cos through asin or sin through acos to have a triangle oscillation, or you can do a modulo of time.time and then make an absolute of that modulo which will also give you a triangle wave and is less processor hungry than sin. you can also multiply that triangle wave by itself to make it rounder. :slight_smile: