Legacy GM About Origins

H

Heat4Life

Guest
Hi dude/women/guy/man/girl... watever lol, I believe that Origins are like the positions(or the coordinates) where you place them on the room and stuff like that But are there more Info about Origins? Can they be useful on what situations? 'coz I'm a little 'bit confused to be honest and acting like a total nub on the forum...

Thanks! All answers would be greatly appreciated! :D
 
Last edited by a moderator:

Bingdom

Googledom
At the bottom-left, you can indicate the origin of the sprite, which is the point in the sprite that corresponds to its position within the room, ie: when you create an instance at a particular x/y position, the origin of the sprite is placed there. Default it is the top left corner of the sprite but it is often more convenient to use the center, which can be found easily by clicking the Center button, or some other point on the sprite. You set the origin manually by clicking in the sprite image which will move the cross to the point you clicked, or by inputting different values for x and y in the corresponding boxes. Note that you can even set an origin outside the area of sprite by using negative numbers (for left and up) or positive numbers larger than the sprite width and height (for right and down), which can be very useful when dealing with objects that need to draw composite sprites.
Does this kinda help? :D
 

Stubbjax

Member
It is a bit tricky to understand exactly what you mean by "origin". Do you mean the origin of a sprite or graphical asset? If so, this might be a good place to start.
 

jo-thijs

Member
Gay? That's a great way to great people...
Anyway, sprite origins define when the sprite gets drawn at location (x, y), which pixel is actually drawn at position (x, y).
If you have an origin of (16, 16) and you draw the sprite at location (x, y), then the top left corner of the sprite will be drawn at position (x - 16, y - 16).

They're useful to make mirroring sprites, rotating sprites, grid snapping, certain collision checks and so on easier.
 
H

Heat4Life

Guest
So when I use like the Built-in Variables: x, y, uses the Origin of the sprite?
 

jo-thijs

Member
Try this:
Create a new empty project, create a new sprite and give it a random origin.
Now create a new object with this sprite and put this in its create event:
Code:
X = room_width / 2;
Y = room_height / 2;
and this in its draw event:
Code:
draw_sprite(sprite_index, image_index, X, Y);
draw_set_colour(c_black);
draw_line(0, Y, room_width, Y);
draw_line(X, 0, X, room_height);
draw_set_colour(c_yellow);
draw_point(X, Y);
Put this object in a new room and run the project.
You'll see a yellow dot exactly where the origin of the sprite is, relative to the sprite.

Try changing the origin of the sprite and observe what happens.

Now, put this in the object's keyboard space pressed event:
Code:
image_xscale = -image_xscale;
and test this out.
Do you see what happens?
 

Jezla

Member
So when I use like the Built-in Variables: x, y, uses the Origin of the sprite?
Sort of. x and y refer to the position of the object, whether it has a sprite or not. If it has a sprite, then the sprite's origin will be placed at the x/y coordinate of the object.

I thought we answered this question for you before, and I really don't see what is so difficult about it. You can easily see how it works by creating a simple sprite (a square) and center the origin, then copy it and leave the origin at the top left. Make an object for each sprite, create a room and place them in it. See how they align with the room editor's grid? That's because of how their origin points are defined.
 

Jezla

Member
It depends on how you define them and use your sprites. The origin doesn't DO anything. It's just a point of reference so that GM:S knows where and how to orient the sprite within the room.
 
H

Heat4Life

Guest
It depends on how you define them and use your sprites. The origin doesn't DO anything. It's just a point of reference so that GM:S knows where and how to orient the sprite within the room.
But I can use them like for Shooting on Asteroids right?
 

Jezla

Member
No, because in a game, you are dealing with objects. The origin only refers to the sprite's origin point. In a game, you deal with the object's position, which is x and y.
 
H

Heat4Life

Guest
Well, When I change my origin, the x and y coordinates follows It and the bullet spawns there...
 

Jezla

Member
That's because GM will draw the sprite with the origin at the x,y of the object. If you don't want the bullet to spawn at the sprite origin, you have to specify a point, for example:

Code:
bullet = instance_create(x+4, y+4, obj_bullet);
 
H

Heat4Life

Guest
That's because GM will draw the sprite with the origin at the x,y of the object. If you don't want the bullet to spawn at the sprite origin, you have to specify a point, for example:

Code:
bullet = instance_create(x+4, y+4, obj_bullet);
What is those x and y pointing at?
 
H

Heat4Life

Guest
What If It knows that I am doing that so I can change the Origin of the instance?
 
H

Heat4Life

Guest
Lol This is confusing, I got too many Questions to ask... :l
 

Jezla

Member
Instances don't have origins, they have coordinates. Origins only have to do with sprites, and as I said, they are a point of reference so GM:S will draw the sprite at the proper position. After you've set the origin in the sprite editor, forget about it. When dealing with objects, you'll use the x,y coordinates.
 
W

Wraithious

Guest
Instances don't have origins, they have coordinates. Origins only have to do with sprites,
This I didn't realize untill you said it, alllll this time i have been struggling with things like screen_save_part in relation to an object's position and wondering why i had to use a bunch of math and position related functions to get it right, and most of the time to be honest i would adjust, try, adjust, try untill it worked then sit here and wonder why it looks different on a mobile device, thank you for pointing out that fact
 
H

Heat4Life

Guest
So when you do like:
bullet = instance_create(x+4, y+4, obj_bullet);
where It will spawn? :l
 

Jezla

Member
I didn't mean to imply that you're dumb, but your incessant use of lol and lmao makes it hard to tell if you're genuinely struggling or just being obtuse. I'll try and explain it again:

The origin of a sprite is a reference point that GM:S uses to draw the sprite within the room. The origin coordinates that you see in the sprite editor are in relation to the sprite image. Thus, an origin of 16,16 means that the origin of the sprite is 16 pixels right and 16 pixels down from the top left corner of the sprite.

Now, when you tell GM to draw a sprite in the room, say at 100, 100, GM will place the sprite in the room, aligning the defined origin point of the sprite with the 100,100 coordinate.

In an object with an assigned sprite, GM will automatically draw the object's sprite at the object's x,y coordinate, again, aligning the defined sprite origin with the x,y coordinate.

Give me a few minutes and I can post a graphical example.
 

Micah_DS

Member
X and Y are instance variables. They refer to an xy point within the room space.
Origin X and Y are sprite/mask variables. An origin of 0, 0 means the top-left of the sprite is equal to the instance's x y point.

Sprite/mask origin does not affect the instance X Y in any way.

But here's an example of how sprites/masks offset when changing the origin (not real code; just demonstrating the workings):
Code:
SpriteX = InstanceX + SpriteOriginX;
SpriteY = InstanceY + SpriteOriginY;
Does this make it clearer?

Another example, showing how xy positioning would work (showing the same thing, three different ways):
x + sprite origin x = sprite position x
y + sprite origin y = sprite position y

64 + -16 = 48
64 + -16 = 48

x (64) + sprite origin x (-16) = sprite position x (48)
y (64) + sprite origin y (-16) = sprite position y (48)
The above example would mean your instance's xy room position is 64, but your sprite's top-left corner is at position 48 within the room.
(EDIT: Oops, I tried to simplify this by just showing an example of x, but realized it'd be better to show both x and y, so I changed the above example)

-

Your brain seems to be overcomplicating it. Truthfully, it's not complicated at all. Don't worry. You'll understand it eventually. That is, if you don't already get it.
 
Last edited:

Jezla

Member


Okay, check this out. This is from the room editor, but it should do for an example.

The grid is 32x32 pixels. The three objects are all on the same y position, but appear different because each of their respective sprites has a different origin.

The lander has a 32x32 sprite with the origin centered (16,16) with an x/y position of 400, 416.
The landing pad has a 40x32 sprite with the origin at the center-top (32,0) with an x/y position of 464, 416.
The collision box (the pink object) has a 32x32 sprite with the origin at the top left (0,0) with an x/y position of 496, 416.

See how they all have a y of 416, yet because of how their sprite origins are defined, they are aligned differently at their coordinates? That's all that setting the origin does.

Any reference or manipulation of object positions is done with the object's x,y coordinates.
 

TheouAegis

Member
Yes, you can do more with an origin comma butt only to some extent. If you position the origin correctly, you have less math to do with certain operations. For example if you want to mirror a sprite using a negative xscale, you put the horizonal origin in the center of the sprite so that the sprite's position in the room doesn't change. If you rotate an object using image_angle, the position of the sprite will change depending on where the origin is.

Typically, for top down perspective or rotating objects, the origin is centered horizontally and vertically. For 2# platformers, the origin is centered horizontally but at the very bottom of the sprite.

Also be aware that there is no rule that says what origin has to actually be in the sprite. You could make a 32x32 sprite with its origin at (16, 128) if you wanted to.
 
H

Heat4Life

Guest
I know what Origin really does, It places your instance on the room based on the origin but I'm just saying IF there are a more useful stuff there to do with Origins.
 

Bingdom

Googledom
Yes, if you put the origin 100% (so 0,16) to the left of the sprite. You can use image_xscale to make the bar expand to the right side.

e.g.
upload_2016-8-31_16-8-9.png
As you can see, the sprite does not expand on the left side, only the right side.

The origin can determine what parts of the sprite should expand, or partially expand. If it's centered, then all sides will expand equally ;)
 
Top