GML Calculating 9 points on a rotating grid

Dorsal

Member
I am having trouble wrapping my head around this problem. I have a square that is 120x120 that I have divided into a 3x3 grid. I don't have a problem getting the mid points of each of the nine squares and placing objects at those points. The issue is when I rotate the grid I can't seem to figure out how to calculate those same nine mid points in each block.

I am trying to rotate the grid from the top left. I am trying to expand this out to any sized grid that might be angled.

grid.gif

Thanks
 

Dorsal

Member
After tripping over myself I found a solution that works. Just in case someone else was looking for something similar.

I have a spot_array that holds the (x,y) values of each mid point.

Code:
var arrayL = array_length_1d(spot_array);

for ( var i = 0; i < arrayL; i++)
{
    _ref = spot_array[@ i];
    xx = _ref[@0];
    yy = _ref[@1];
   
    var pd = point_direction(x,y,xx,yy );
    var len = point_distance(x,y,xx,yy);
   
    xC = x + lengthdir_x(len, pd + image_angle);
    yC = y + lengthdir_y(len, pd + image_angle);
   
    draw_circle(xC,yC,10,false);
}
 

Attachments

2Dcube

Member
Wouldn't it be more useful to rotate grids from the center? I guess it depends on what you plan to do with it.
 
C

CedSharp

Guest
Another thing you could do is that if there is no real good reason to having those "points" separated from the grid, you could simply create a surface, draw the grid + points, and then draw that surface rotated. This way you can easily draw the points as if the grid wasn't rotated.
 
This should be a lot faster because there are fewer function calls, and none inside of the loop, but in gml who knows:
Code:
var _c = dcos(angle);
var _s = dsin(angle);
for(var _x = 20; _x < 120; _x += 40){
    for(var _y = 20; _y < 120; _y += 40){
        _x1 = x0 + _x *  _c + _y * _s;
        _y1 = y0 + _x * -_s + _y * _c;
    }
}
_x1,_y1 is the location of each circle. x0,y0 is the location of the grid's origin. Adjust for loop contstraints accordign to the layout of the grid.
 

GMWolf

aka fel666
Another thing you could do is that if there is no real good reason to having those "points" separated from the grid, you could simply create a surface, draw the grid + points, and then draw that surface rotated. This way you can easily draw the points as if the grid wasn't rotated.
off screen rendering is not the most elegant solution. rotating the primitives is far nicer.

If your project requires a lot of these sorts of transformations, i would encourage you to learn how to deal with matrix tranformations.
They allow you to work in whatever space you need. So if you know how to draw one thing, you can also draw it rotated, scaled, translated, etc.
Whats great is that you can even get the GPU to do all the heavy lifting.
you just set your transform spaces, draw whatever (axis aligned grids and all) and the GPU will use the matrix to apply whatver tranformation you need, all in one go.

matrices can also be multiplied together to combine their effecs, so if you need to rotate, translate, roate again... you can just work out each of those matrices independantly and then multiply them all together and give it to the GPU to transform your geometry.

There are plenty of resources out there to learn matrix transformations and how they apply to compute graphics, googling should do.
as for GML, i would look at https://docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/matrices/index.html

It IS may seem advanced (but actually look, we are so smart its super simple for us!) but learning this is an important step in learning computer graphics, and works out ot be a very powerful tool (especilally if you want to play with 3d)
 
Last edited:

FrostyCat

Redemption Seeker
It IS advanced! but learning this is an important step in learning computer graphics, and works out ot be a very powerful tool (especilally if you want to play with 3d)
This is not "advanced" at all. If you are in a computer graphics course, this stuff comes within the first few weeks of lectures. I was in one and I know it.

The GMC is the only place where I see people struggle this much with radial bullet patterns and relative attachments, because everywhere else hierarchical transformation is a standard-issue technique. GML-only rookies have the misfortune of being brainwashed into believing this is difficult, and encouraged to manually apply lengthdir_x() and lengthdir_y() in ways that become unmanageable for more than one joint. They're being herded into gun fights with knives, and YoYo is left wondering why GM games have a "distinctive" look.

One thing I'm starting to dislike about the GMC is how liberally the "advanced" label gets slapped on essential, frequently used skills. Things like arrays, data structures, JSON, buffers and matrix/vector operations get this same kind of unfair treatment all the time, and only in here. It synthetically limits the growth of novices and is borderline slanderous. I'd request that you and other responders band together and help put a stop to this. If something shows up a lot in general development and is essential for independence, please CEASE AND DESIST from calling it "advanced" or "hard", and start calling it "basic".
 

GMWolf

aka fel666
This is not "advanced" at all. If you are in a computer graphics course, this stuff comes within the first few weeks of lectures. I was in one and I know it.

The GMC is the only place where I see people struggle this much with radial bullet patterns and relative attachments, because everywhere else hierarchical transformation is a standard-issue technique. GML-only rookies have the misfortune of being brainwashed into believing this is difficult, and encouraged to manually apply lengthdir_x() and lengthdir_y() in ways that become unmanageable for more than one joint. They're being herded into gun fights with knives, and YoYo is left wondering why GM games have a "distinctive" look.

One thing I'm starting to dislike about the GMC is how liberally the "advanced" label gets slapped on essential, frequently used skills. Things like arrays, data structures, JSON, buffers and matrix/vector operations get this same kind of unfair treatment all the time, and only in here. It synthetically limits the growth of novices and is borderline slanderous. I'd request that you and other responders band together and help put a stop to this. If something shows up a lot in general development and is essential for independence, please CEASE AND DESIST from calling it "advanced" or "hard", and start calling it "basic".
\

It is the basics! 100% agree. To you and I, to any experienced users yes, linear algebra is the fundamental mathematics that underpins a lot of computer graphics.
However to someone who has issues using trigonometry in game code, matrices will 100% seem like an advanced topic when they look at it. I know hey seemed advance to me for the longest time.

In the context of GameMaker graphics programming, matrices are definetly an advanced topic. Its one of the last things that GM has to teach you. For me it was the last thing I learnt in GM before 'graduating' from gamemaker (or was it shaders, idk).
Even more recently I still learn new things about matrices. I think I only got an intuition for how rotation matrices work two/three years ago.


When I use the words 'Advanced' I don't mean to discourage, or otherwise infer that this is something that needs to be learnt once you are more experienced. What I mean is that it will require some ammount of learning. You need to learn some basic linear algebra, how model matrices work, what the different parts are, you can have inverse matrices, projections matrices, etc.
Then you also need to understand how they can be applied: you can apply them to individual points in code, or get the GPU to transform all the points, and so on.
Its a lot to take in!

All the way along your programming carreers you will be learning advanced topics. What's great is when those advanced topics become basics again. The trick is recognizing those advanced topics, because they are the onse worth aiming for!
I spent so long avoiding what I considered to be advanced, had I known those were precisely the things I had to learn next I would be a much better programmer today.

Don't be affraid to learn something that is too advanced. If it was too advanced, you probably wouldn't know it existed, yet.




[EDIT]

But, I see how now that people are probably in the same mindset as I was, avoiding learning anything advanced.

So, ill change my phrasing to this:
"It may seem advanced, but once you learn it it will be what you base your programming on. This is the next step"
It pretty much applies to everything you mentioned (array, data structures, json, etc.)
 
Last edited:
There is no such thing as advanced or basic concepts. It's all relative to personal experience. And, while I believe there's no reason gamemaker or its users should shy away from any concept, there are a lot of things yoyogames could do to make gamemaker appealing not only to complete novices, but to keep more experienced users interested in it also.
 
Top