trouble understanding chipmunk's coordinate system

Official forum for the Chipmunk2D Physics Library.
Post Reply
msbr
Posts: 2
Joined: Sun Mar 15, 2015 8:13 pm
Contact:

trouble understanding chipmunk's coordinate system

Post 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.
Attachments
demo.c
(3.6 KiB) Downloaded 1631 times
ewing
Posts: 4
Joined: Tue Sep 03, 2013 4:30 am
Contact:

Re: trouble understanding chipmunk's coordinate system

Post 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
msbr
Posts: 2
Joined: Sun Mar 15, 2015 8:13 pm
Contact:

Re: trouble understanding chipmunk's coordinate system

Post 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,
ewing
Posts: 4
Joined: Tue Sep 03, 2013 4:30 am
Contact:

Re: trouble understanding chipmunk's coordinate system

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests