Snapping to grid help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Snapping to grid help

Post 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?
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Snapping to grid help

Post 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
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Snapping to grid help

Post 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.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Snapping to grid help

Post by LeonBlade »

Shouldn't you use modulus instead?
There's no place like ~/
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Snapping to grid help

Post 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
}
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Snapping to grid help

Post 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.
There's no place like ~/
Post Reply