Page 1 of 1

trouble understanding chipmunk's coordinate system

Posted: Sun Mar 15, 2015 8:51 pm
by msbr
Hi,

I'm learning to use chipmunk and SDL together and I'm having some trouble to understand what is going on. I've been reading some tutorials online and wrote a little program to give it a try. I know that SDL and chipmunk coordinates are different and I'm struggling a bit with this. I want to create an object that will fall to the ground and then stop after bouncing a little. However, the object seems to be always offset, thus falling out of the screen bottom. Besides, when the object falls on the ground, it seems to slide a bit from side to side before coming to a rest.

I attached the code for reference (warning, it's ugly code :oops:).

Best regards.

Re: trouble understanding chipmunk's coordinate system

Posted: Mon Mar 16, 2015 7:56 pm
by ewing
Chipmunk uses Cartesian coordinates, the same you find in any math book:
- X increases to the right
- Y increases upward
- Rotations go counter-clockwise (right-hand-rule) and are in radians.
- Additionally, x and y positions represent the center of an object.

SDL on the other hand:
- X increases to the right (same)
- Y increases downward (inverted)
- Rotations go clockwise and are in degrees
- x and positions represent the top-left corner of an object

So every time you want to draw your Chipmunk object in SDL, take your Chipmunk values and:
- Invert the Y (e.g. invert_pos_y = SCREEN_HEIGHT - chipmunk_pos.y)
- Shift the x and y by 1/2 the width and height of your object
- Convert radians to degrees (chipmunk_angle * (360.0 / (2 * PI))
- flip the sign of the angle

Re: trouble understanding chipmunk's coordinate system

Posted: Mon Mar 16, 2015 11:26 pm
by msbr
Thanks for your reply. I'll change my code to follow what you said.

Is the origin point the same for SDL and chipmunk (top-left corner)? Or this does not matter at all?

Best regards,

Re: trouble understanding chipmunk's coordinate system

Posted: Tue Mar 17, 2015 1:35 pm
by ewing
Chipmunk origin is effectively <0,0>, but remember that Y increases upwards.
Since Chipmunk doesn't draw graphics, it is up to SDL or your drawing library where to put stuff.

But to start with, maybe it will be easier for you to conceptually think of Chipmunk's <0,0> in the bottom-left corner.