Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:53:55+00:00 2026-06-12T17:53:55+00:00

I have an enemy ship and a player ship. I would like the enemy

  • 0

I have an enemy ship and a player ship.

I would like the enemy ship to always fly towards the player in a direct line, at all times, even when the player is moving.

I’m going about this using the Ray class for C# XNA.

I have two vector coordinates, the position/origin of the ray (the players current position), and the Direction of the ray (the enemy’s current position). I’d like the enemy’s position to gradually move towards the players position.

I have this code so far.

enemyPlayerTrack.Position = playerPos;
enemyPlayerTrack.Direction = enemyPos;

I’m unsure whether I need another vector for velocity or not.

In the end, the enemy will be drawn to the screen with a new position with this code:

enemyWorldMatrix = Matrix.CreateTranslation(new Vector3(x, y, z));

Without a mathematical background, I’m having trouble creating a velocity to bridge the two vectors closer and closer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:53:56+00:00Added an answer on June 12, 2026 at 5:53 pm

    We choose some speed s. Then the direction of the player from the enemy is:

    dir_x = player_x - enemy_x
    dir_y = player_y - enemy_y
    

    Overall speed s = sqrt(vel_x^2 + vel_y)^2, so we scale the dir vector to give us the speed:

    factor = s / sqrt(dir_x^2 + dir_y^2)
    vel_x = dir_x * factor
    vel_y = dir_y * factor
    

    So now the enemy will always fly at the same speed, directed towards the player. But if the player is near the enemy, the enemy will overshoot and keep bouncing back and forth over the player. So we limit the speed:

    distance = sqrt(dir_x^2 + dir_y^2)
    delay_to_reaching_player = 2    // some measure of time
    enemy_speed = min(s, distance/delay_to_reaching_player)
    

    This way, by setting the delay, the enemy will slow down as it approaches the player, once it gets close enough to stop moving at its maximum speed (s).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem: there is this enemy which rotates to my player. While
I have a hierarchy like this: Scene  - gameWorld (CCLayer Z:0)     - player (CCSprite
I have a piece of running java software that is jammed. I would like
In my game, all game objects extend an Entity class. All enemy objects have
I have an Entity baseclass which the classes Player and Enemy Inherit. Both player
I have an Entity baseclass which the classes Player and Enemy Inherit. class Entity
I have near about 255 image frames for background animation, 99 frames of enemy
I made shoot em up like game.But I have only one ememy which fallows
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have created enemies that chase the player around a game, but there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.