Page 1 of 1

Snapping to grid help

Posted: Tue Apr 20, 2010 8:18 pm
by xiphirx

Code: Select all

						if(	abs(
							floor( 
								(double) 
								(  (mousex/(32/grid))*(32/grid)+scrLeft  )
							)
							- (  (mousex/(32/grid))*(32/grid)+scrLeft  )
						)
						< 
						abs(
							ceil(
								(double) 
								(  (mousex/(32/grid))*(32/grid)+scrLeft  )
							)
							- (  (mousex/(32/grid))*(32/grid)+scrLeft  )
						)
					   )
					{
						mousex = (mousex/(32/grid))*(32/grid);
					}
					else
					{
						mousex = (int)ceil((double)(mousex/(32/grid))*(32/grid));
					}

					if(	abs(
							floor( 
								(double) 
								(  (mousey/(32/grid))*(32/grid)+scrTop  )
							)
							- (  (mousey/(32/grid))*(32/grid)+scrTop  )
						)
						< 
						abs(
							ceil(
								(double) 
								(  (mousey/(32/grid))*(32/grid)+scrTop  )
							)
							- (  (mousey/(32/grid))*(32/grid)+scrTop  )
						)
					   )
					{
						mousey = (mousey/(32/grid))*(32/grid);
					}
					else
					{
						mousey = (int)ceil((double)(mousey/(32/grid))*(32/grid));
					}
I am trying to replicate this effect
http://www.awestyproductions.com/images ... ystem.html
(notice how if you get closer to one side, it snaps there)

Mine totally fails... it just acts as if I floored the values...

What am I doing wrong?

Re: Snapping to grid help

Posted: Wed Apr 21, 2010 12:26 am
by GroundUpEngine
The code on the site works fine imo, I modified a bit though ;)

Code: Select all

void Snap_To(int mousex, int mousey, int grid = 32)
{
    if(abs((int)floor(mousex/grid)-(mousex/grid)) < abs((int)ceil(mousex/grid)-(mousex/grid))) {
        mousex = floor((mousex/grid))*grid;
    } else {
        mousex = ceil((mousex/grid))*grid;
    }

    if(abs((int)floor(mousey/grid)-(mousey/grid)) < abs((int)ceil(mousey/grid)-(mousey/grid))) {
        mousey = floor((mousey/grid))*grid;
    } else {
        mousey = ceil((mousey/grid))*grid;
    }


    App.MainWindow->SetCursorPosition(mousex, mousey); // sfml CursorPos
}
screenshot ->
Image

Re: Snapping to grid help

Posted: Wed Apr 21, 2010 12:46 am
by xiphirx
GroundUpEngine wrote:The code on the site works fine imo, I modified a bit though ;)

Code: Select all

void Snap_To(int mousex, int mousey, int grid = 32)
{
    if(abs((int)floor(mousex/grid)-(mousex/grid)) < abs((int)ceil(mousex/grid)-(mousex/grid))) {
        mousex = floor((mousex/grid))*grid;
    } else {
        mousex = ceil((mousex/grid))*grid;
    }

    if(abs((int)floor(mousey/grid)-(mousey/grid)) < abs((int)ceil(mousey/grid)-(mousey/grid))) {
        mousey = floor((mousey/grid))*grid;
    } else {
        mousey = ceil((mousey/grid))*grid;
    }


    App.MainWindow->SetCursorPosition(mousex, mousey); // sfml CursorPos
}
screenshot ->
Image

I'll try it tomorrow and report back, thanks.

Re: Snapping to grid help

Posted: Wed Apr 21, 2010 1:28 am
by LeonBlade
Shouldn't you use modulus instead?

Re: Snapping to grid help

Posted: Wed Apr 21, 2010 9:29 am
by GroundUpEngine
LeonBlade wrote:Shouldn't you use modulus instead?
Ye, I was just basing my code of the code from the site but something like that I guess would do same ->

Code: Select all

void Snap_To(int mousex, int mousey, int grid = 32)
{
    mousex -= mousex % grid;
    mousey -= mousey % grid;

    App.MainWindow->SetCursorPosition(mousex, mousey); // sfml CursorPos
}

Re: Snapping to grid help

Posted: Wed Apr 21, 2010 12:10 pm
by LeonBlade
GroundUpEngine wrote:
LeonBlade wrote:Shouldn't you use modulus instead?
Ye, I was just basing my code of the code from the site but something like that I guess would do same ->

Code: Select all

void Snap_To(int mousex, int mousey, int grid = 32)
{
    mousex -= mousex % grid;
    mousey -= mousey % grid;

    App.MainWindow->SetCursorPosition(mousex, mousey); // sfml CursorPos
}
The only thing with that, it doesn't snap in the up and left directions.
You have to check to see if you are half the distance or more to a certain side to determine which side to snap to.