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
xiphirx
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:
Post
by xiphirx » Tue Apr 20, 2010 8:18 pm
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?
GroundUpEngine
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
Post
by GroundUpEngine » Wed Apr 21, 2010 12:26 am
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 ->
xiphirx
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:
Post
by xiphirx » Wed Apr 21, 2010 12:46 am
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 ->
I'll try it tomorrow and report back, thanks.
LeonBlade
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
Post
by LeonBlade » Wed Apr 21, 2010 1:28 am
Shouldn't you use modulus instead?
There's no place like ~/
GroundUpEngine
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
Post
by GroundUpEngine » Wed Apr 21, 2010 9:29 am
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
}
LeonBlade
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
Post
by LeonBlade » Wed Apr 21, 2010 12:10 pm
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 ~/