- Home /

# How to create a damped harmonic oscillation?

Hello,

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.

**GameView:**

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 -

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...

regards,

Karsnen

**Additional Links:**

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

why don't you just take the board and use a spring component ?

(look in the menus for "spring", also read the doco on it. you can get to the doco by clicking the doco (book) button on the component)

Hmmm.. fattie. I think I did try that too. But I will try it in a different manner now.

Fattie, I was not able to get the required through springs. Hence I changed the approach and the question.

Why not directly use transform.Rotate() or assign transform.eulerAngles? Use somethig like (sin t) / t function.

**Answer** by MountDoomTeam
·
Oct 09, 2012 at 08:55 PM

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 :

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. :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.