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.

I'm creating a simple top down shooter, right now I have an enemy which simply follows the player. I'd like to change things up and have the enemies move towards the player but in a wave like motion. I have looked at some similar questions like this but they don't take into account for the Y changing. How can I simulate a wave like pattern for my enemies whilst they are homing into their target.

Edit:

Sample code

In my update method I have the following:

            Vector2 trackingPos = position - target;
            trackingPos.Normalize();
            position -= trackingPos * elaspedTime  * speed;
share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Here is the solution I would try

Vector2 dir = target - position; // direction
dir.Normalize();
Vector2 perp( -dir.y, dir.x ); // perpendicular

float waveAmp = 0.05f; // adjust if needed
float waveAngle = elapsedTime * 3.14f * 2; // adjust if needed
Vector2 wave = perp * sin(waveAngle) * waveAmp;

Vector2 vel = dir * speed;
position += vel * elaspedTime + wave;
share|improve this answer
    
I've tried this, but the enemies are still just homing in at me, without the wave like motion. –  Sun Sep 21 '12 at 21:24
    
i didn't know your "scale". increase the waveAmp to something larger like 5.0 and other values. –  milkplus Sep 22 '12 at 4:27
add comment

Assuming you have your logic for homing actually working, I would just have an offset variable (float) that you update over time for how much oscillation you want (for a simple test case I would simply do something like sin( elapsedTime ) until you figure out exactly what you want. Then just add a vector with a length of that offset value and a direction that's perpendicular to your enemy's forward vector (assuming forward is pointing towards its target) and put it there.

share|improve this answer
    
Thanks for the reply but I'm having a little difficulty understanding your answer, some pseudo code would help since my math isn't to great. –  Sun Sep 17 '12 at 15:52
add comment

Here's what I would use:

//Change 800 to whatever number you like - that's how much it bows out
position.X = position_x_origin - (800 * (float)Math.Sin(((float)gameTime.TotalGameTime.TotalSeconds - timeSpawned) 
   * MathHelper.Pi / 2));
//This causes the change in Y                   
Velocity = Vector3.Down * speed;
share|improve this answer
add comment

You should take a look at Bézier curves, they're easy to implement and offer lots of possibilities.

Bézier curves on Wikipedia

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.