# 2D Shooter Homing Projectile

This topic is 2848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone, I've almost finished a 2D space shooter I'm working on and would like one last piece of advice. I'm busy doing a homing projectile algorithm, and have run into a bit of a problem. At the moment the projectile moves towards the player on the Y axis, but doesn't move in either direction on the X axis.

At the moment I'm doing this every frame:

aMissile.mDirection = new Vector2(mPlayerShip.Position.X - aMissile.Position.X, mPlayerShip.Position.Y - aMissile.Position.Y);

aMissile.mDirection.Normalize();

How would I get it to also move towards the player's X position?

##### Share on other sites
missile.position += speed*missile.mDirection will move it in both directions. You didn't post any info about how you actually move the missile.

##### Share on other sites
At the moment I'm doing this every frame:

Position += theDirection * theSpeed* (float)theGameTime.ElapsedGameTime.TotalSeconds;

As far as I can tell it's the same as your recommendation.

##### Share on other sites
You should try something along the lines of this.

float x = (target.X - position.X);float y = (target.Y - position.Y);float angle = (float)Math.Atan2(y, x);Vector2 movement = new Vector2(	(float)(speed * Math.Cos(angle)),	(float)(speed * Math.Sin(angle)));position += movement;

##### Share on other sites
Like this, assuming missilePos, missileDir, and playerPos are Vector2 and missileSpeed and timeElapsed are floats:

// calculate missile coursemissileDir = playerPos - missilePos;missileDir.Normalize();missileDir *= missileSpeed;// update missile positionmissilePos += missileDir * timeElapsed;

##### Share on other sites
Quote:
 As far as I can tell it's the same as your recommendation.

Looks like it. So the problem must be with the player or missile positions or how you render it.

Have you checked the values?

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632650
• Total Posts
3007645
• ### Who's Online (See full list)

There are no registered users currently online

×