# Snapping to a Rotating Grid

6 replies to this topic

### #1 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 08 April 2012 - 06:00 AM

Um, I am in a dilemma. Does anyone know how to snap to a 'grid' that is rotating? I know that you can rotate objects using lengthdir_x and stuff, but what if I want it to snap to a 16x16 grid that is rotating with it?

Like if it rotates 45 degrees, and I create a random object in the grid, it would snap to the nearest 16x16 spot, relative to 45 degrees. Does this make sense?

I tried to divide by 16, round, then multiply, but that makes it rotate in a snap-grid fashion. I wanted it to orbit smoothly, while still be 16 distance away from another object rotating around the same thing. I hope its not too complicated...

I hope to hear from the community soon. Thanks for reading and extra thanks for whoever can solve my problem.

EDIT:
Ok, I've tried this way, but it doesn't work.

CREATE EVENT:
```parent = obj_planet;
par_dist = distance_to_point(parent.x,parent.y)+(32);
rev_pos = point_direction(x,y,parent.x,parent.y)+180;

x = floor((parent.x+cos(rev_pos*pi/180)*(par_dist))/16)*16;
y = floor((parent.y-sin(rev_pos*pi/180)*(par_dist))/16)*16;

par_dist = distance_to_point(parent.x,parent.y)+(32);
rev_pos = point_direction(x,y,parent.x,parent.y)+180;```

I believed it would work since it snaps to a 16 when it is created, but when I tested it, it seems to ignore what I state here, even though I used show_message to tell me that the coords did change when I tried to snap it to grid.....Just putting it out there for further info.

Hope you guys can help me.

Edited by andykuo1, 08 April 2012 - 06:27 AM.

• 0

### #2 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 08 April 2012 - 10:42 AM

Well, you can first rotate your object with the angle of your grid, but in the other sense. You can do so with this code:
// xbegin and ybegin form the origin of your grid, in your example they would both be 0
// grid_angle is the angle of the grid
var dist,dirc;
dist=point_distance(xbegin,ybegin,x,y);
dirc=point_direction(xbegin,ybegin,x,y);
x=xbegin+lengthdir_x(dist,dirc-grid_angle);
y=ybegin+lengthdir_y(dist,dirc-grid_angle);

Then you snap to the grid as if it would have had the angle 0, like this code does:
// I guess it is obvious what grid_width and grid_height are
x=floor((x-xbegin)/grid_width)*grid_width+xbegin;
y=floor((y-ybegin)/grid_height)*grid_height+ybegin;

And now we just rotate the object back with this code:
dist=point_distance(xbegin,ybegin,x,y);
dirc=point_direction(xbegin,ybegin,x,y);
x=xbegin+lengthdir_x(dist,dirc+grid_angle);
y=ybegin+lengthdir_y(dist,dirc+grid_angle);
• 0

### #3 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 10 April 2012 - 02:39 AM

ummm....I didn't quite catch your answer. But instead I found a "cheat", in doing this. It does not snap, but however, rotates the same speed, and is snapped to the nearest object who is already in grid. This adds a lot of complications, so I really hope to figure out what you are trying to say. Can you explain it more? Do all this happen in the beginning, where no rotation has taken place? Or does some parts require other events? Thanks for answering so quickly.
• 0

### #4 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 10 April 2012 - 08:22 AM

Ok, I'll explain the logic behind my code and afterwards give a script that does it at once:
We consider it as impossible to snap the object to a rotated grid, but it is possible to snap it to a normal grid, so somehow we must be able to convert the rotated grid to a normal grid to do this job. So how dop we do that, we just rotate the rotated grid until it is a normal grid and we rotate the object with it, so now we work in fact in the same area, only a bit rotated, but we do know how to snap the object here, so we do that. Now he is snapped to the rotated grid, but we may not forget to rotated everything to the angle everything had before, so we do that.
Now the script:

// argument0 is the x component of the origin of the grid (you should use 0)
// argument1 is the y component of the origin of the grid (you should use 0)
// argument2 is the width of the grid
// argument3 is the height of the grid
// argument4 is the angle of the grid compared to the x axis
var dist,dirc;
dist=point_distance(argument0,argument1,x,y);
dirc=point_direction(argument0,argument1,x,y)-argument4;
x=argument0+lengthdir_x(dist,dirc);
y=argument1+lengthdir_y(dist,dirc);
x=floor((x-argument0)/argument2)*argument2+argument0;
y=floor((y-argument1)/argument3)*argument3+argument1;
dist=point_distance(argument0,argument1,x,y);
dirc=point_direction(argument0,argument1,x,y)+argument4;
x=argument0+lengthdir_x(dist,dirc);
y=argument1+lengthdir_y(dist,dirc);
• 0

### #5 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 02 June 2012 - 05:13 PM

Ok, I'll explain the logic behind my code and afterwards give a script that does it at once:
We consider it as impossible to snap the object to a rotated grid, but it is possible to snap it to a normal grid, so somehow we must be able to convert the rotated grid to a normal grid to do this job. So how dop we do that, we just rotate the rotated grid until it is a normal grid and we rotate the object with it, so now we work in fact in the same area, only a bit rotated, but we do know how to snap the object here, so we do that. Now he is snapped to the rotated grid, but we may not forget to rotated everything to the angle everything had before, so we do that.
Now the script:

// argument0 is the x component of the origin of the grid (you should use 0)
// argument1 is the y component of the origin of the grid (you should use 0)
// argument2 is the width of the grid
// argument3 is the height of the grid
// argument4 is the angle of the grid compared to the x axis
var dist,dirc;
dist=point_distance(argument0,argument1,x,y);
dirc=point_direction(argument0,argument1,x,y)-argument4;
x=argument0+lengthdir_x(dist,dirc);
y=argument1+lengthdir_y(dist,dirc);
x=floor((x-argument0)/argument2)*argument2+argument0;
y=floor((y-argument1)/argument3)*argument3+argument1;
dist=point_distance(argument0,argument1,x,y);
dirc=point_direction(argument0,argument1,x,y)+argument4;
x=argument0+lengthdir_x(dist,dirc);
y=argument1+lengthdir_y(dist,dirc);

Sorry, for the long delay. I had Finals I had to study for.
Thank YOU jo-thijs! It worked. Thank you so much. I was stuck on that for a long time.....But now I'm stuck on another one.

It is similar to this topic so I am debating whether to start another one or continue on this one.

The problem is, I am trying to place something on a grid that is rotating. Now the part where it rotates along with the grid is solved, but before you place it, when it is following the mouse.
I am trying to make the "Mouse" object to snap to the rotating grid yet stay relative to the mouse position. Is that possible? I've been working on this since 3 days ago. I hope to get some answers. Thanks in advanced.
• 0

### #6 jo-thijs

jo-thijs

GMC Member

• GMC Member
• 1464 posts
• Version:GM8.1

Posted 02 June 2012 - 06:34 PM

Funny, my finals almost start.
Do you want an object follow the mouse and snap to the nearest intersection of the grid?
• 0

### #7 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 05 June 2012 - 03:28 PM

Funny, my finals almost start.