Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So, I'm spawning a horizontal line of enemies that I would like to have move in a nice wave. Currently I tried:

 Enemy.position.X += Enemy.velocity.X;
 Enemy.position.Y += -(float)Math.Cos(Enemy.position.X / 200) * 5;

This...kind of works. But the wave is not a true wave. The top and bottom of one pass are not the same (e.g. 5 for the top, and -5 for the bottom (I don't mean literal points, I just meant that it's not symmetrical)).

Is there a better way to do this? I would like the whole line to move in a wave, so it looks fluid. By that, I mean that it should look like each enemy is "following" the one in front of it. The code I posted does have this fluidity to it, but like I said, it's not a perfect wave.

Any ideas?

Thanks in advance.

share|improve this question
    
Is your window size 200? –  Byte56 Mar 29 '12 at 2:14
    
How is it not a perfect wave? Wolfram Alpha suggests your formula is just fine. Could you post a screenshot? –  Jonathan Hobbs Mar 29 '12 at 2:15
    
No, no. My window size is 1280x720. I should have specified. That's just my arbitrary number to constrain the wave so it's not giant. –  electroflame Mar 29 '12 at 2:15
    
This is the wave you should expect for your window size –  Byte56 Mar 29 '12 at 2:24
    
Now that I'm looking at the functions you guys provided, it is a little strange that it doesn't work. Give me a moment to try something. EDIT: It works fine if I just spawn 1 enemy. It's only the trailing enemies that have issues. It looks like the trailing enemies are a bit "lower" to start. –  electroflame Mar 29 '12 at 2:27
add comment

2 Answers

up vote 3 down vote accepted

Perhaps it's as simple as:

Enemy.position.X += Enemy.velocity.X;
Enemy.position.Y = -(float)Math.Cos(Enemy.position.X / 200) * 5;

Notice the Enemy.position.Y is just using = not +=

EDIT

Try with this instead:

Enemy.position.Y = -(float)Math.Cos(Enemy.position.X / 100);

EDIT again

With offset to put 0 at the middle of your window:

Enemy.position.Y = -(float)Math.Cos(Enemy.position.X / 100) + ScreenHeight/2;

OR

Enemy.position.Y = StartPosition.Y + (-(float)Math.Cos(position.X / 100) * 100); 

As you found that to work.

share|improve this answer
    
Unfortunately, that doesn't work. That eliminates the wave entirely, and the enemies just move across the top of the screen (0 on the Y) –  electroflame Mar 29 '12 at 2:34
    
That's pretty strange. Clearly the Y value couldn't equal zero, what are the Y values being set to? This is what should be happening with the new setting. –  Byte56 Mar 29 '12 at 2:40
    
Alright, now we're getting somewhere. With the /100 I can now see that, while still being 'centered' around 0, it is still doing a very slight wave. So now maybe I need to offset it? –  electroflame Mar 29 '12 at 2:53
    
Success! Here's the new line: position.Y = OriginalPosition.Y + (-(float)Math.Cos(position.X / 100) * 100); This makes a fantastic wave. Many thanks, Byte. You nudged me in the right direction. If you change your answer to relect the line I just stated, I'll mark you as the answer. ;) –  electroflame Mar 29 '12 at 2:57
add comment

It should be as simple as: Enemy.position.Y = -(float)Math.Sin(Enemy.position.X), no?

This assumes you have a wave of enemies that move horizontally. The path of motion would be the exact same as the plot of y = sin x, or rather Enemy.position.Y = sin(Enemy.position.X).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.