Skip to main content
fixed a typo in a formula per comments
Source Link
SpartanDonut
  • 2.9k
  • 1
  • 23
  • 37

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. These numbers already have a bit of a bias on speed though and could improperly influence how fast you end up moving. As a result, we want to normalize this into a single unit.

To normalize this into a unit vector you first must find the length (aka magnitude) of the vector using the equation sqrt((x * axx) + (y * y))

Once you've found the length you can convert it to a unit vector by dividing the x and y values by the length.

Multiply this unit vector by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;
var length = sqrt((rise*rise) + (run*run)); //pseudocode
var unitX = run / length;
var unitY = rise / length;

this.x += unitX * this.speed;
this.y += unitY * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. These numbers already have a bit of a bias on speed though and could improperly influence how fast you end up moving. As a result, we want to normalize this into a single unit.

To normalize this into a unit vector you first must find the length of the vector using the equation sqrt((x * ax) + (y * y))

Once you've found the length you can convert it to a unit vector by dividing the x and y values by the length.

Multiply this unit vector by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;
var length = sqrt((rise*rise) + (run*run)); //pseudocode
var unitX = run / length;
var unitY = rise / length;

this.x += unitX * this.speed;
this.y += unitY * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. These numbers already have a bit of a bias on speed though and could improperly influence how fast you end up moving. As a result, we want to normalize this into a single unit.

To normalize this into a unit vector you first must find the length (aka magnitude) of the vector using the equation sqrt((x * x) + (y * y))

Once you've found the length you can convert it to a unit vector by dividing the x and y values by the length.

Multiply this unit vector by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;
var length = sqrt((rise*rise) + (run*run)); //pseudocode
var unitX = run / length;
var unitY = rise / length;

this.x += unitX * this.speed;
this.y += unitY * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

added 565 characters in body
Source Link
SpartanDonut
  • 2.9k
  • 1
  • 23
  • 37

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. These numbers already have a bit of a bias on speed though and could improperly influence how fast you end up moving. As a result, we want to normalize this into a single unit.

To normalize this into a unit vector you first must find the length of the vector using the equation sqrt((x * ax) + (y * y))

Once you've found the length you can convert it to a unit vector by dividing the x and y values by the length.

Multiply this unit vector by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;
var length = sqrt((rise*rise) + (run*run)); //pseudocode
var unitX = run / length;
var unitY = rise / length;

this.x += rununitX * this.speed;
this.y += riseunitY * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. Multiply this by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;

this.x += run * this.speed;
this.y += rise * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. These numbers already have a bit of a bias on speed though and could improperly influence how fast you end up moving. As a result, we want to normalize this into a single unit.

To normalize this into a unit vector you first must find the length of the vector using the equation sqrt((x * ax) + (y * y))

Once you've found the length you can convert it to a unit vector by dividing the x and y values by the length.

Multiply this unit vector by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;
var length = sqrt((rise*rise) + (run*run)); //pseudocode
var unitX = run / length;
var unitY = rise / length;

this.x += unitX * this.speed;
this.y += unitY * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.

Source Link
SpartanDonut
  • 2.9k
  • 1
  • 23
  • 37

I would solve this using simple algebra.

Given your sprite's position and the target location you should be able to find the slope of the line that would connect the two coordinates. This can be done using the following equation:

enter image description here

Essentially, the numerator (top number) is going to be velocity you apply on the Y axis and the denominator (bottom number) is going to be the velocity you apply to the X axis. Multiply this by your speed and you should be properly moving towards your target.

In code this would look something like:

var run = playerX - this.x;
var rise = playerY - this.y;

this.x += run * this.speed;
this.y += rise * this.speed;

You might need to do some fiddling with the sign or the order of the numbers depending on what direction is up, etc. but the concept should apply.