Moving a point along a vector

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-12-22T01:43:33Z Indexed on 2010/12/22 1:54 UTC
Read the original article Hit count: 650

Filed under:
|
|
|
|

Hello-
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 2p (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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c