Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the position of the origin in PyGame coordinate system

I am doing some operations with vectors and physics in PyGame and the default coordinate system is inconvenient for me. Normally the (0, 0) point is at the top-left corner, but I would rather that the origin were at the bottom-left corner. I would rather change the coordinate system than converting every single thing I have to draw.

Is it possible to change the coordinate system in PyGame to make it work like this?

like image 414
Andrés Avatar asked Apr 16 '12 00:04

Andrés


People also ask

How do you change origin on pygame?

The simplest way to do it would be to have a function to convert coordinates, and use it just before drawing any object. This will take your coordinates and convert them into pygame's coordinates for drawing, given height , the height of the window, and coords , the top left corner of an object. Save this answer.

Where is the origin in pygame?

The origin of the display, where x = 0 and y = 0, is the top left of the screen. Both axes increase positively towards the bottom right of the screen. The pygame display can actually be initialized in one of several modes.


1 Answers

Unfortunately, pygame does not provide any such functionality. The simplest way to do it would be to have a function to convert coordinates, and use it just before drawing any object.

def to_pygame(coords, height):
    """Convert coordinates into pygame coordinates (lower-left => top left)."""
    return (coords[0], height - coords[1])

This will take your coordinates and convert them into pygame's coordinates for drawing, given height, the height of the window, and coords, the top left corner of an object.

To instead use the bottom left corner of the object, you can take the above formula, and subtract the object's height:

def to_pygame(coords, height, obj_height):
    """Convert an object's coords into pygame coordinates (lower-left of object => top left in pygame coords)."""
    return (coords[0], height - coords[1] - obj_height)
like image 165
Casey Kuball Avatar answered Sep 22 '22 02:09

Casey Kuball