Snap to grid?

Non-platform specific questions.

Snap to grid?

Postby Novice » Thu Mar 23, 2006 12:13 am

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
Why do i always get stuck?
User avatar
Novice
 
Posts: 399
Joined: Mon Aug 29, 2005 10:54 am
Location: Relative
Score: 5 Give a positive score

Postby DilloDude » Tue Apr 04, 2006 3:52 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby DilloDude » Thu May 25, 2006 5:49 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Novice » Fri May 26, 2006 1:15 am

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!
Why do i always get stuck?
User avatar
Novice
 
Posts: 399
Joined: Mon Aug 29, 2005 10:54 am
Location: Relative
Score: 5 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest