4
\$\begingroup\$

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;
\$\endgroup\$

4 Answers 4

2
\$\begingroup\$

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;
\$\endgroup\$
2
  • \$\begingroup\$ I've tried this, but the enemies are still just homing in at me, without the wave like motion. \$\endgroup\$
    – Sun
    Commented Sep 21, 2012 at 21:24
  • \$\begingroup\$ i didn't know your "scale". increase the waveAmp to something larger like 5.0 and other values. \$\endgroup\$
    – milkplus
    Commented Sep 22, 2012 at 4:27
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Sun
    Commented Sep 17, 2012 at 15:52
0
\$\begingroup\$

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;
\$\endgroup\$
-2
\$\begingroup\$

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

Bézier curves on Wikipedia

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .