Search code examples
delphimathangle

Calculate a position based on an angle a speed and a starting position


Althougth i have read many articles on internet regarding it, i m unable to solve my issue.

There is a 2D plan where x, y is the top left point of the screen. I have a point from which i know the position x, y, the direction (0 to 360 degrees) and a speed (in pixel per step ).

According to what i have read, if i want to calculate the next position of the point after one step i use the following code:

  self.px.X := round(self.px.X + self.speed * cos(direction));
  self.px.Y := round(self.px.Y + Self.speed * sin(direction));

For testing purpose i use a speed value of 10 and a direction of 90. Normally, the point should move horizontally (or even vertically would be a progress), but it is moving in a diagonal movement, not even something like 45°.

Anyone knows what i'm doing wrong ?


Solution

  • As said by Andreas Rejbrand (but i don't know how to turn a comment into an answer, sorry Andreas), the solution was to use radians number by simply calling degToRad function:

    self.px.X := round(self.px.X + self.speed * cos(DegToRad(direction)));
    self.px.Y := round(self.px.Y + Self.speed * sin(DegToRad(direction)));