Wednesday, April 15, 2015

mathematics - How to move an object up and down like a wave?


I am trying to move my object up and down using cos and sin, but I have problem understanding how does the sin and cos work, so I can make my object move up and down like a wave frequently


Init Time starts from value of 16.00 it keeps on increment up to 17.065



initTime += msec * 0.001;
float x = cos(initTime* 2.46) * 2075;
float y = sin (initTime* 2.888) * 200;

My object goes down and up, then goes back to its initial position, then goes up and down once again, I want it to be like a wave



Answer



I still think you need to understand basic trigonometry. But here is a simple introduction of how to use sin and cos to simulate a wave.


The basic wave formula is:



f(t) = A * sin( 2 * pi * f * t + phase )




Where:




  • f is the frequency, which controls the number of times the waves repeats per unit time, f = 1/P where P is the number of periods.

  • A is the amplitude of the wave, which represents the highest and lowest points of the wave.

  • 2*pi is a scaling factor to be able to deal with the equation in terms of frequency and periods. (in other words, make it easier and more intuitive).

  • t is a parameter that represents time, or any other varying quantity.




enter image description here


The relation between a cos and a sin wave is only pi/2 phase shift.



cos(t) = sin(t + pi/2 );



What you only need to do to make your object move, is to vary t over time, inject it into the wave equation, and multiply your object possibly the Y position with the computed result from the wave equation.


Implementing this is straightforward


float initx = 0;
float inity = 0;
Object.x = initx+ time;

Object.y = inity+ (a * sin(2 * PI * f * t + phase));

But keep in mind that this will not simulate how an object would float on water. The topic would be much more complicated. This will only simulate a sine wave.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...