Page 1 of 1

Snap to grid?

PostPosted: Thu Mar 23, 2006 12:13 am
by Novice
Has anyone suceeded in making actors snap to an imaginary grid without using an array of actors or regions? I looked at the checkers demo but i can't find the part of code that makes the peaces snap. If anyone has a solution i would be very grateful. TIA

PostPosted: Tue Apr 04, 2006 3:52 am
by DilloDude
I don't know how to do it with something like dragging with the mouse, but you can use a cursor actor with codes
Code: Select all
y+=34;
x+=34;
x-=34;
y-=34;

in keydown up, left, right and down, respectively.

PostPosted: Thu May 25, 2006 5:49 am
by DilloDude
Ok, to do it with the mouse, add a mousebutton down event, with drag enabled, and put
Code: Select all
//
in script editor (or any other code you want).
In global code, add the function:
Code: Select all
void SnapToGrid(int xsize, int ysize)
{
    const int halfx = xsize / 2;
    const int halfy = ysize / 2;
    x = (round((x - halfx) / xsize) * xsize) + halfx;
    y = (round((y - halfy) / ysize) * ysize) + halfy;
}

On mousebutton up, put
Code: Select all
SnapToGrid(width, height);
or
Code: Select all
SnapToGrid(xamount, yamount);
. Now it will move to a position with a gridn of size xamount by yamount, with it aligned inside the grid (So if it is close to zero, its corner will be on 0,0). If you want it aligned on top of the grid (so its center will be at 0,0) take out the xsize and ysize bits.

PostPosted: Fri May 26, 2006 1:15 am
by Novice
Thaks, for the effort and the code DilloDude.
I found a way around the problem so it's a bit late, but thaks anyway. I'm sure that I will use it later and that everyone here will find this very usefull at some point.
BTW I tried it out and it works prefectly!