Public Group

# 2D movement based on direction and speed?

This topic is 2780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to move an object in 2D space in pygame. I'm using:

self._pos.x += self._speed * math.cos(self._angle*math.pi/180)
self._pos.y += self._speed * math.sin(self._angle*math.pi/180)

self._bb.center = (self._pos.x, self._pos.y)

But the movement seems to be off by approximately 90 degrees. Any ideas why? I'm stumped. Thanks

##### Share on other sites
Is 0 degrees supposed to point straight up? Or is it supposed to point directly to the right?
I'm not very good at math, but I've found that some APIs have '0 degrees' be about 90 degrees off from what I was expecting/assuming.

Check if this is true for you and your API. Is your API thinking that 0 is to the right, while you are thinking it is pointing straight up?
Are your math functions making any assumptions as well?

##### Share on other sites

Is 0 degrees supposed to point straight up? Or is it supposed to point directly to the right?
I'm not very good at math, but I've found that some APIs have '0 degrees' be about 90 degrees off from what I was expecting/assuming.

Check if this is true for you and your API. Is your API thinking that 0 is to the right, while you are thinking it is pointing straight up?
Are your math functions making any assumptions as well?

That's a good question, but I have no idea how to find that out. What part is the culprit? I mean, I'm only using cos/sin and PI. Otherwise I'm just adding the deltas for X/Y to the position. And then I'm rendering the object in pygame (0,0 is top/left of the screen, with positive X to the right and positive Y down to the bottom of the screen).

Thanks

##### Share on other sites
Using the code you posted, a rotation of 0 degrees/radians should result in movement to the right, along the positive x axis.

Is that what you want? Or are you wanting a rotation of 0 to correspond to some other direction?

##### Share on other sites

Using the code you posted, a rotation of 0 degrees/radians should result in movement to the right, along the positive x axis.

Is that what you want? Or are you wanting a rotation of 0 to correspond to some other direction?

I assumed it would move in a negative Y direction - up the screen.

##### Share on other sites

I assumed it would move in a negative Y direction - up the screen.

What was your assumption based on? (That might help in pointing you in the right direction.)

##### Share on other sites
Everything I've ever worked on with Graphics has always had 0 degree's being along the positive X axis (ie, normally to the right).

If you could explain your assumption that might help us explain to you why this is so.

##### Share on other sites
Indeed, the way you're using the math is based on the unit circle:

Note that at theta = 0, x = 1 and y = 0. That's your base, unrotated case. If you want to have "up" equal 0 degrees, either add 90 to your result, or change the environment context in general (i.e. +x moves up, -y moves left, -x moves down, etc...)

##### Share on other sites
Thanks all - I was so tired last night I couldn't see the wood for the trees. I suddenly realised my mistake as I was lying in bed trying to get to sleep. I made a poor assumption about 0 degrees pointing upwards but then I realised it depended on the coordinate system of pygame, which uses +ve X to the right. So with that knowledge my code and the results are correct.

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013603
×