Calculation of speed between two points

I have an enemy ship and a player ship.

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

I am going to do this using the Ray class for C # XNA.

I have two vector coordinates, the position / origin of the beam (the current position of the players) and the direction of the beam (the current position of the enemy). I would like the position of the enemy to gradually move to the position of the players.

I have this code so far.

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

I'm not sure if I need another vector for speed or not.

In the end, the adversary will be drawn on the screen with a new position using this code:

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

Without a mathematical background, I am having problems creating speed to bring two vectors closer together.

+4
source share
3 answers

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

 dir_x = player_x - enemy_x dir_y = player_y - enemy_y 

The total speed is s = sqrt (vel_x ^ 2 + vel_y) ^ 2, so we scale the vector dir 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 next to the enemy, the enemy will bend and continue to jump back and forth on the player. Thus, 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) 

Thus, by setting a delay, the adversary will slow down as he approaches the player as soon as he approaches to stop moving at maximum speed (s).

+2
source

I don't have XNA in front of me, so this is just pseudo code ...

The common delta between them should be simple:

 var delta = playerPosition - enemyPosition; 

This gives direction, but will usually be of the wrong size; so we can rescale this to a unit vector via:

 var magnitude = Math.Sqrt(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z); // however many dimensions you have... var unitDelta = delta / magnitude; // assuming non-zero; if zero, don't move var newVelocity = unitDelta * enemySpeed; 

Please note, however, that this challenges the impulse and is very simple (it does not take into account the speed of the player - he will feel that the AI ​​is stupid).

+2
source

If you have an enemy β†’ player vector, you basically have a translation that you can apply to the enemy to make him move to the players position. You need to go back to β€œone” by normalizing the vector, which will set the length of the vectors to β€œ1”, but still point in the direction of the player, then you can multiply this value by the speed that you really want the vector to translate the enemy to move it towards the player

As people have already said - to get a unit you need to sum the products of both components of the vector, and then take the square root. This is due to the Pythagorean theorem, for example:

If you take the components of a vector, say

(2,2)

What was that:

  2 ^ | | --> 2 

And then draw a line between the beginning (0,0) and the point where the vector ends - you get a triangle ...

  2 ^ /| / | --> 2 

This row represents the magnitude of the vector, you take the sum of the squares of both components and then sqrt to get the length of this row:

2 * 2 + 2 * 2 = 8

sqrt(8) = 2.82

Thus, this vector is 2.82, which means that values ​​2 and 2 are 2.82 times the size of one unit vector

To get components whose length is "1" or "one", we need to normalize the vector - we do this by dividing each component by the value

2 / 2.82 = 0.70

Which sounds right - I know that the angle of this vector is 45 degrees (up 2 to 2 should be an ideal diagonal), so you can check it with cosine or sine, since cosine / sine provides a horizontal or vertical unit of length component of the vectors for given angle

sin(45) or cos(45) = 0.70

Spot on

So, now you know that the angle to the player uses a vector of length 1 with the following components

(0.7, 0.7)

To move the enemy 3 units to the player, you simply multiply the components by 3

0.7 * 3 = 2.1

So,

(2.1, 2.1)

Will move the enemy 3 units to the player at this stage of physics.

Does it help at all?

0
source

Source: https://habr.com/ru/post/1439119/


All Articles