[Solved] Origin point for a draw event

Discussion in 'Programming' started by InkSho, Oct 13, 2019 at 4:07 AM.

Tags:
  1. InkSho

    InkSho Member

    Joined:
    Sunday
    Posts:
    3
    Im trying to find out how to change the y origin point of an object that doesn't have a sprite.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    What? The y origin is y. It doesn't matter if it has a sprite asssigned.
     
    ParodyKnaveBob likes this.
  3. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    If you're trying to imply you want to change the origin for the next sprite you draw, origin is a sprite property (not object property) and is changed with sprite_set_offset. This will change the origin for all uses of the sprite of course. If you want to modify draw position for one instance only, you adjust your sprite draw position with variables set in the instance. But if you want the collision mask move too, you'll have to change the instance's x and y position.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    An object is located at x, y. This is always true (by definition). If it has a sprite, its origin pixel will occupy that position. If it doesn't, the object still is there, but it won't be able to have collisions or be visible. (If it has a collision mask separate from its sprite, that also has its origin placed in x,y)
     
    Bentley and ParodyKnaveBob like this.
  5. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    176
    If you're just looking for the drawing part, then modifying matrix_world using matrix_set is the way to go.
    That basically allows you to choose a custom origin/pivot point (or whatever you want to call it).

    As an example, let's take draw_rectangle:
    Code:
    var size = 50;
    
    draw_rectangle(x, y, x+size, y+size, false);
    The above code draws a square of size 50 with the top-left corner located at (x,y).
    That corresponds to a sprite origin of (0, 0).

    If you want to do the same thing while having control over the pivot/origin/... point, you need to move the rectangle to the actual origin (the top-left corner of the room/world), and move it to (x, y) afterwards using a transformation that you create using matrix_build:
    Code:
    var size = 50;
    
    var mat_transform = matrix_build(x, y, 0, 0, 0, 0, 1, 1, 1);
    matrix_set(matrix_world, mat_transform);
    
    draw_rectangle(0, 0, size, size, false);
    
    matrix_set(matrix_world, matrix_build_identity());
    The above code isn't that useful yet, since the origin is still in the top-left corner.
    That can be improved by adding variables for the actual origin:
    Code:
    var size = 50;
    var origin_x = 25, origin_y = 25;
    
    var mat_transform = matrix_build(x, y, 0, 0, 0, 0, 1, 1, 1);
    matrix_set(matrix_world, mat_transform);
    
    draw_rectangle(0-origin_x, 0-origin_y, size-origin_x, size-origin_y, false);
    
    matrix_set(matrix_world, matrix_build_identity());
    Here's a quick example that shows how it works. You can use Left and Right arrow keys to rotate about the pivot point.
    Rotating and scaling are quick ways to visually check that the origin is where you want it to be.

    Hope this helps a bit :)
     
  6. InkSho

    InkSho Member

    Joined:
    Sunday
    Posts:
    3
    to go into more detail, yes the origin is defaulted to 0,0 regardless if it had a sprite or not. Specifically im trying to draw text and strictly text through the draw event without the sprite stuff at all. If i were to change the yscale which affects how the text is drawn, then the text would become taller or shorter. The origin determines where this increase or decrease starts.

    even though stuff like xscale or yscale work with drawn text, offset doesnt seem to work when creating a new yorigin. initially i tried something like offsetting the y origin with text_height but that didnt work.

    in the example the text grows downwards because there's a yscale increase while the origin for y is 0.
    i'd like for it to grow up instead but i would have to change the y origin in such a way that it not only is always the bottom, but even if more lines were to add on
     

    Attached Files:

    Last edited: Oct 14, 2019 at 5:47 AM
  7. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    176
    The code that I posted also works for text (use draw_text instead of draw_rectangle).
    But since your question is about drawing text, you may not need all that. Did you have a look at draw_set_valign yet?
     
  8. InkSho

    InkSho Member

    Joined:
    Sunday
    Posts:
    3
    ah i havent, this may be how i do it

    mhm, this basically fixes everything, thank you
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice