Question


I have a point defined by x,y and a vector defined by heading, speed. I am attempting to move the point x,y along this vector, at a distance of 'speed'. Below is the code I am currently using:

self.x += self.speed * cos(self.heading);
self.y += self.speed * sin(self.heading);

Heading can be any angle in a full circle - 0 to 2π (0-360 degrees). The problem is the above code:

  • Only moves along the x or y axis when angle is 0->270
    • for example, when the avatar is facing the top-right corner (45 degrees relative), it moves straight up.
  • Does not move at all when angle is 270->360

heading, speed, X and Y are all doubles, and heading is reported by the user touching a direction-pad in the lower corner. I know the heading is correct because the avatar rotates to the correct direction, it is just the actual movement I am having problems with.

Thanks for any help
- Chris

Was it helpful?

Solution

Moving straight up would be sin(90), not sin(45). So you have some kind of problem there.

(sin(90) == 1 which would just keep adding your speed to y and nothing to x because cos(90) == 0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top