Center of an Image

Hi, I’am new to GD and was studying the Beginner’s tutorial and one problem I found is that the images don’t get well placed (for example the explosion image doesn’t was exactly over the enemy’s tank). The problem was that when GD draws an object it uses coordinates 0,0 (upper left corner) to place the image. The solution was to Add/Change the point Origin to the center of the image. My question is if this is the standart way to draw objects centered in a X,Y coordinates or there is another way?

Hi TransDiv! Welcome to the forums!

GD posses a relative coordinates system for every object. So you can add new vectors (pair of X,Y coordinates) to an object and refer to them later.

There are two pre-defined vectors in every object: “Origin” and “Centre” (yeah, “Centre”, in french, even in my spanish-translated version of GD); being “Origin” the pair of X,Y coordinates from where the sprite will be placed and yes, by default it’s referencing the top-left corner (0,0) but you can change it.

These custom points can be created from the object editor and the way to reference the coordinates of a specific point in an object is:

objectName.PointX(pointName), objectName.PointY(pointName)

An useful example of this feature is (for example :smiley: ) when you need to display multiple explosions over an object, so you can create a series of coordinates over each spot in the object where an explossion will have place, then equalize the “Origin” point values to the “Centre” point values of your explosion object and draw the explosion over the coordinates previously created over the “exploded” object. That will give you perfect centered explosions over specific spots of an object.

An important thing to have in mind when using custom pairs of coordinates is that those points belongs to the current animation from where it were created. For example if you created the custom point “bulletOrigin” for the “Animation 1” of the object “ship” then ship.PointX(bulletOrigin) and ship.PointY(bulletOrigin) are only valid references when “ship” is in “animation 1” (at least you had created the same point for other animations in the same object).

Hope that helps.

1 Like