Skip to main content
added 147 characters in body
Source Link
MAnd
  • 4.9k
  • 3
  • 25
  • 43

A vector is given by a point in space (in your case, 2D space). When we think of or use a vector as a direction, it means that it represents the direction going from the origin (0,0) to that given point in space. Not a specific angle. So, in your example, the vector (4,5) that you calculated is the direction from point B to point A. And actually, as you will se, that is all we need.

In what regards speed, that's a totally different thing. By you mixing that together with length and magnitude, it is apparent that you are mixingconfounding speed and velocity, which are two different concepts. Speed is just a scalar number that represents the amount of distance walked in a given time, i.e. length divided by elapsed time. Velocity is a vector given by the direction of the movement divided by elapsed time. SeeHence, the current speed of a movement is the length of the velocity of that movement. For more details, see: http://www.physicsclassroom.com/class/1DKin/Lesson-1/Speed-and-Velocity.

It means that, at each iteration of the loop while movement is in place, you take the current location of the moving char and sum to it the result of the multiplication between the normalized direction of the movement, the speed of the movement and the frame delta.Time.

A vector is given by a point in space (in your case, 2D space). When we think of or use a vector as a direction, it means that it represents the direction going from the origin (0,0) to that given point in space. Not a specific angle. So, in your example, the vector (4,5) is the direction from point B to point A. And actually that is all we need.

In what regards speed, that's a totally different thing. By you mixing that together with length and magnitude, it is apparent that you are mixing speed and velocity, which are two different concepts. Speed is just a scalar number that represents the amount of distance walked in a given time, i.e. length divided by elapsed time. Velocity is a vector given by the direction of the movement divided by elapsed time. See: http://www.physicsclassroom.com/class/1DKin/Lesson-1/Speed-and-Velocity.

A vector is given by a point in space (in your case, 2D space). When we think of or use a vector as a direction, it means that it represents the direction going from the origin (0,0) to that given point in space. Not a specific angle. So, in your example, the vector (4,5) that you calculated is the direction from point B to point A. And actually, as you will se, that is all we need.

In what regards speed, that's a totally different thing. By you mixing that together with length and magnitude, it is apparent that you are confounding speed and velocity, which are two different concepts. Speed is just a scalar number that represents the amount of distance walked in a given time, i.e. length divided by elapsed time. Velocity is a vector given by the direction of the movement divided by elapsed time. Hence, the current speed of a movement is the length of the velocity of that movement. For more details, see: http://www.physicsclassroom.com/class/1DKin/Lesson-1/Speed-and-Velocity.

It means that, at each iteration of the loop while movement is in place, you take the current location of the moving char and sum to it the result of the multiplication between the normalized direction of the movement, the speed of the movement and the frame delta.Time.

added 14 characters in body
Source Link
MAnd
  • 4.9k
  • 3
  • 25
  • 43

Then, we normalize that vector. What does normalize mean? It only means that we take the vector "movement_direction" and makeforce its length/magnitude to become equalsequal 1. It is prettyThat's pretty simple to achieve: you just divide the vector by its length. See more here: http://www.fundza.com/vectors/normalize/. For that, we will need to get the distancedirection lenght, just as you did:

float direction_length = Math.sqrt (Math.Pow(movement_direction.x,2) + Math.Pow(movement_direction.y,2) );

Then, let's normalize the direction of the movement:

Vector2 movement_direction_normalized = movement_direction  / direction_length;

Now, you just choose the speed you want for your movement and you retrieve from your game engine the so called frame delta.Time, which is the time elapsed between the last frame and the current frame of the main game loop.

The final formula for the movement becomes, at each frame of the main loop while the movement is still going on:

AndNote that you should execute the formula (i.e. update the movement) in a loop that keeps working until the updated distance between the moving character is smaller than the initial distance, i.e. smaller than the distance between points B and A.

NoteAlso note that including delta.Time is crucial, because that is the way you guarantee that you movement will have the same speed no matter how fast is the computer running the game. Otherwise, the speed would vary because of the difference in how fast computers calculate each frame of your game.

Then, we normalize that vector. What does normalize mean? It only means that we take the vector "movement_direction" and make its length/magnitude become equals 1. It is pretty simple: you just divide the vector by its length. See more here: http://www.fundza.com/vectors/normalize/. For that, we will need to get the distance lenght, just as you did:

float direction_length = Math.sqrt (Math.Pow(movement_direction.x,2) + Math.Pow(movement_direction.y,2) );
Vector2 movement_direction_normalized = movement_direction  / direction_length;

Now, you just choose the speed you want for your movement and you retrieve from your game the delta.Time, which is the time elapsed between the last frame and the current frame of the main game loop.

The final formula for the movement becomes:

And you should execute that until the updated distance between the moving character is smaller than the initial distance, i.e. the distance between points B and A.

Note that including delta.Time is crucial, because that is the way you guarantee that you movement will have the same speed no matter how fast is the computer running the game. Otherwise, the speed would vary because of the difference in how fast computers calculate each frame of your game.

Then, we normalize that vector. What does normalize mean? It only means that we take the vector "movement_direction" and force its length/magnitude to become equal 1. That's pretty simple to achieve: you just divide the vector by its length. See more here: http://www.fundza.com/vectors/normalize/. For that, we will need to get the direction lenght, just as you did:

float direction_length = Math.sqrt (Math.Pow(movement_direction.x,2) + Math.Pow(movement_direction.y,2) );

Then, let's normalize the direction of the movement:

Vector2 movement_direction_normalized = movement_direction  / direction_length;

Now, you just choose the speed you want for your movement and you retrieve from your game engine the so called frame delta.Time, which is the time elapsed between the last frame and the current frame of the main game loop.

The final formula for the movement becomes, at each frame of the main loop while the movement is still going on:

Note that you should execute the formula (i.e. update the movement) in a loop that keeps working until the updated distance between the moving character is smaller than the initial distance, i.e. smaller than the distance between points B and A.

Also note that including delta.Time is crucial, because that is the way you guarantee that you movement will have the same speed no matter how fast is the computer running the game. Otherwise, the speed would vary because of the difference in how fast computers calculate each frame of your game.

added 14 characters in body
Source Link
MAnd
  • 4.9k
  • 3
  • 25
  • 43

In detail: you have to calculate the vector direction (not the angle) and normalize it, have to use the scalar speed that you desire, and have to applyapply a frame-to-frame delta time for consistency across different computers. You put all that in a loop that loops while the distance between moving char and destination point (in your case, point A) is lower than the total distance between points A and B. And voilá!

Let's do it. If the movement goes from point B to point A, then the vector direction of the movement is equal the location (position) of point A in the space minus the location (position) of the point B in space. I will use C# for the following examples, but you will get the idea:

In detail: you have to calculate the vector direction (not the angle) and normalize it, have to use the scalar speed that you desire, and have to apply a delta time for consistency across different computers. You put all that in a loop that loops while the distance between moving char and destination point (in your case, point A) is lower than the total distance between points A and B. And voilá!

Let's do it. If the movement goes from point B to point A, then the vector direction of the movement is equal the location of point A in the space minus the location of the point B in space. I will use C# for the following examples, but you will get the idea:

In detail: you have to calculate the vector direction (not the angle) and normalize it, have to use the scalar speed that you desire, and have to apply a frame-to-frame delta time for consistency across different computers. You put all that in a loop that loops while the distance between moving char and destination point (in your case, point A) is lower than the total distance between points A and B. And voilá!

Let's do it. If the movement goes from point B to point A, then the vector direction of the movement is equal the location (position) of point A in the space minus the location (position) of the point B in space. I will use C# for the following examples, but you will get the idea:

Source Link
MAnd
  • 4.9k
  • 3
  • 25
  • 43
Loading