Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Snapping Mouse to a Grid

Author
Message
Superdeath360
13
Years of Service
User Offline
Joined: 23rd Nov 2010
Location:
Posted: 21st Jan 2011 06:23
So I've seen all types of cool videos in which people have created their own map editors that saved the map tiles in the form of text.

What I need to be able to do is make the mouse snap to a grid. How can I do this? thanks

herp derp
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Jan 2011 15:58
I may have an article to suit you in the next newsletter, just waiting for some additional information. Watch out for it on 1st Feb

WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 21st Jan 2011 17:56
Something like this?

int mx;
int snap=4;

mx=( dbMouseX() / snap ) * snap;

Because its using ints the decimal point is ignored.

Or something like the old days of optimising (don't need it these days though because the compiler does all that for you).

mx=( dbMouseX() >> 2 ) << 2; // also snaps by 4

Warning! May contain Nuts!
haliop
User Banned
Posted: 22nd Jan 2011 17:57
ive done this a long time ago and i kinda faked the process . instead of using math i just used if dbmousemovex or rbmousemovey move a fake cursor which is an object or a sprite and moving it around the grid size or snap. however i would go for the math thing since it will probably faster to calculate. however if you decide to go my way then keep in mind to add if dbmousemove bigger then for say 50 then move the fake cursor more snap points so you will get faster to disant places on the grid. if you are using a 3d grid then use dbmousemovez which is the mouse wheel. also dont forget you can implent several ways to achiv the same goal which will also give you the ability to edit your grid in several ways therefor you get a much powerfull editor. hope this helps. nadav.
Superdeath360
13
Years of Service
User Offline
Joined: 23rd Nov 2010
Location:
Posted: 23rd Jan 2011 05:58 Edited at: 23rd Jan 2011 06:17
Thank you! every post was useful! I'll try both your methods, haliop, WLFfx, and see which one i like the best. I'll also look out for that newsletter, batVink. I appreciate all of your help, everyone.

EDIT:omg yay i'm no longer on the noob delay

herp derp
haliop
User Banned
Posted: 23rd Jan 2011 06:30
hehe u never was.
Superdeath360
13
Years of Service
User Offline
Joined: 23rd Nov 2010
Location:
Posted: 23rd Jan 2011 06:42 Edited at: 23rd Jan 2011 22:08
haliop -> I has a problem.



so the first two ifs work. The little sprite moves to the right or down only when the mouse is in the appropriate spot...but the last two ifs dont do anything as far as i can tell. This is probably something blindingly obvious or stupid. I'm going to sleep cus its like 1 in the morning

EDIT: WLGfx, Your method works great !

herp derp
haliop
User Banned
Posted: 26th Jan 2011 09:43
first of all gj.
second the last two should be dbmousemove ==-1 cause u move to the other direction. try to find a way also not to compare it to the sprite.
haliop
User Banned
Posted: 26th Jan 2011 18:33
ive thought of another one for you. instead of moving the sprite or the cursor, move the actuall grid like shown in fpsc where you always work on the tile that is centered at screen. you can do that with only checking your mouse position in relation to the center of the screen . this approach will enable you to scroll really fast if your grid is huge. and is very easy to implent.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 26th Jan 2011 21:49
Quote: "so the first two ifs work. The little sprite moves to the right or down only when the mouse is in the appropriate spot...but the last two ifs dont do anything as far as i can tell. This is probably something blindingly obvious or stupid."


Well, I can calm you down, it's not stupid, just something you might not know. The dbMouseMove-commands check the mouse movement since the last time you called these commands!
So if you call them twice in your loop they will allways return 0 the second time, because nothing has changed in between the calls.

Try something like this:



See the difference? You only call them once like that. Hope this helps a bit.

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 27th Jan 2011 21:52 Edited at: 27th Jan 2011 21:55
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 27th Jan 2011 22:32 Edited at: 27th Jan 2011 22:48
That code snipped you linked to is nice, TechLord!
Makes me want to work on an editor again

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Superdeath360
13
Years of Service
User Offline
Joined: 23rd Nov 2010
Location:
Posted: 30th Jan 2011 22:06
Quote: "try to find a way also not to compare it to the sprite."

Well I was comparing it to the mouse position, idk why i changed that.

wow this thread has gotten so many posts i'm gonna try some new stuff now with the whole grid snap thing.

Oh, and thanks for explaining that, pauli

herp derp
haliop
User Banned
Posted: 31st Jan 2011 12:35
Super soon you will be our snapping king! go for it
and dont forget to share what you learned , you might even come with a new solution!

Login to post a reply

Server time is: 2024-06-23 03:24:52
Your offset time is: 2024-06-23 03:24:52