I started a game programmer. I am having trouble with creating the movement of the ball. Linear motion of the ball is very easy course. However, at higher level I want them to move along the zigzag and sin(x). I know the equation of motion of the zigzag is y = sin (x). If the ball moving in this way, it will only move in one direction only.

At the time the ball is moving straight, I want translate to zigzag or sin(x) line! Everything is too difficult for me. Is there a function or a way to create animations like that? I desperately needed help.

# Movement of the ball along the zigzag and sin(x)

Page 1 of 1## 3 Replies - 2462 Views - Last Post: 23 July 2012 - 01:26 AM

##
**Replies To:** Movement of the ball along the zigzag and sin(x)

### #2

## Re: Movement of the ball along the zigzag and sin(x)

Posted 22 July 2012 - 07:37 AM

I have no idea. So, I'm guessing and trying to come up with a creative solution here. But I think I have an idea.

My idea is to use two vectors. The first vector represents the forward velocity. The second vector is always perpendicular to the first and represents displacement from the line of travel that the first vector creates. The second vector is always 90 degree from the first, but changes in magnitude/length based on a sine wave.

I'm imagining that if you add the two vectors together to give you a new vector, you can add that to the position of the object every frame and get the object to oscilate in a roughly zig-zag pattern.

By controlling the amplitude and frequency of the wave you can control the pattern of movement.

http://en.wikipedia.org/wiki/Sine_wave

For some really crazy movement combine multiple sin waves of different frequency together to make one wave.

Once you have your sine wave, multiply the side facing vector by the output of the wave. That will be a vector times a scalar and cause the length/magnitude of the side facing vector to grow and contract in both the positive and negative directions.

So you will have your weaving oscillation from the side facing vector, but by adding it to the forward facing vector it should cause it to move mostly in a straight line.

I haven't tried this, so you may have to play around with the concept a little to get it to actually work.

My idea is to use two vectors. The first vector represents the forward velocity. The second vector is always perpendicular to the first and represents displacement from the line of travel that the first vector creates. The second vector is always 90 degree from the first, but changes in magnitude/length based on a sine wave.

I'm imagining that if you add the two vectors together to give you a new vector, you can add that to the position of the object every frame and get the object to oscilate in a roughly zig-zag pattern.

By controlling the amplitude and frequency of the wave you can control the pattern of movement.

http://en.wikipedia.org/wiki/Sine_wave

For some really crazy movement combine multiple sin waves of different frequency together to make one wave.

Once you have your sine wave, multiply the side facing vector by the output of the wave. That will be a vector times a scalar and cause the length/magnitude of the side facing vector to grow and contract in both the positive and negative directions.

So you will have your weaving oscillation from the side facing vector, but by adding it to the forward facing vector it should cause it to move mostly in a straight line.

I haven't tried this, so you may have to play around with the concept a little to get it to actually work.

### #3

## Re: Movement of the ball along the zigzag and sin(x)

Posted 22 July 2012 - 08:24 AM

Incidently, what you want is a triangle wave rather than a sine wave, in order to get a true "zig-zag" pattern.

You could combine different sine waves of different frequency and amplitude, but you may never get a truely perfect triangle wave that way but rather a close approximation.

A different function rather than sine may produce a more perfect triangle wave with fewer CPU cycles.

I found a forum thread that gives a function to calculate a triangle wave on the StackOverflow forum, but I don't want to give a direct link if that might offend since it's a "competing" forum.

But if you google "triangle wave equation" you should find a formula.

You may want to experiment with different types of waves such as sawtooth, square, reverse sawtooth, etc. You can generate all of those with enought sine waves, or at least a close approximation. But there may be easier ways to get them than sine.

You could combine different sine waves of different frequency and amplitude, but you may never get a truely perfect triangle wave that way but rather a close approximation.

A different function rather than sine may produce a more perfect triangle wave with fewer CPU cycles.

I found a forum thread that gives a function to calculate a triangle wave on the StackOverflow forum, but I don't want to give a direct link if that might offend since it's a "competing" forum.

But if you google "triangle wave equation" you should find a formula.

You may want to experiment with different types of waves such as sawtooth, square, reverse sawtooth, etc. You can generate all of those with enought sine waves, or at least a close approximation. But there may be easier ways to get them than sine.

### #4

## Re: Movement of the ball along the zigzag and sin(x)

Posted 23 July 2012 - 01:26 AM

Thank the sincere comments from you. Maybe I should learn more about this sine wave. It comes down to physics very deep. But if you understand it can be applied in many applications interesting game.

Page 1 of 1