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?
2D Shooter Homing Projectile
missile.position += speed*missile.mDirection will move it in both directions. You didn't post any info about how you actually move the missile.
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.
Position += theDirection * theSpeed* (float)theGameTime.ElapsedGameTime.TotalSeconds;
As far as I can tell it's the same as your recommendation.
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;
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;
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement