Snap to grid?

0 favourites
  • 13 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Is there a easy way to snap to grid? With a drag and drop mechanics?

    Or should i use this example? and place it on the nearest grid to the mouse? But the how do i get the nearest to the mouse, Distance(Mouse.X,Mouse.y,?,?)

  • "Pick nearest" is a common condition.

    You select the object type ("Grid" here I guess) and input the position you want to test (here mouse.x and mouse.y).

    This will pick the nearest instance of the Grid object to your mouse cursor.

    For drag/drop mechanics I suggest you to check the how do I FAQ, I think there are a few examples of drag/drop listed in there.

    The "snap to grid" effect is achieved when you drop an object by spawning it in the middle of the current cell instead of the mouse position where it was droped.

    It depends a great deal on how you setup your grid.

  • Kyatric

    I looked at the manual entry, But didnt really understand how to use the expression, I made the grid part, Can you help me place the objects onto the grid please?

    Here is my capx,

  • nah it's far simpler than that.

    Snapping is a kind of value rounding. For instance if your round off float value, you obtain a snapping to exactly 1 px.

    To achieve the same kind of effect with wider cells you just have to scale down values, round them up and scale them back up.

    That's the idea.

    Mathematically it looks like that :

    System: Every Tick
       -> Sprite: set X to round(mouse.X/cellsize)*cellsize
       -> Sprite: set Y to round(mouse.Y/cellsize)*cellsize[/code:1sjnyoii]
    now you might end up with some offset depending on either:
    1. the position of the pivot point of your sprite
    2. the starting position or origin of your grid
    for the first one you just have to add a position offset to the result
    [code:1sjnyoii]System: Every Tick
       -> Sprite: set X to round(mouse.X/cellsize)*cellsize+Sprite.pivotOffsetX
       -> Sprite: set Y to round(mouse.Y/cellsize)*cellsize+Sprite.pivotOffsetY[/code:1sjnyoii]
    for the second you have to offset the position before calculation and then offset it back (its like a change of origin)
    [code:1sjnyoii]System: Every Tick
       -> Sprite: set X to round((mouse.X-gridOffsetX)/cellsize)*cellsize+gridOffsetX
       -> Sprite: set Y to round((mouse.Y-gridOffsetY)/cellsize)*cellsize+gridOffsetY[/code:1sjnyoii]
    I hope I'm not too wrong
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Great post, Yann! Glad I found it.

  • Yann, Thank you. This is exactly what I needed. Very elegant solution.

  • Yuup Yann nailed it as usual. Couldnt have said it better mself

  • Good stuff <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Hi guys,

    I just started using construct and I wanted to give a try at making a game operate similar to PvZ. So far I am trying to get the snap to grid to work but I'm having some slight issues.

    Here is my project file so that you can see what I have so far:

  • goldboyj

    First of all, welcome to the community!

    It's great you've added your .capx to the post, but it would be even better if you could also point out exactly what the problem is. It all depends on what you want to happen, so it's really just a guessing game for me when looking at your .capx at this point.

    PS: You could also have made a new thread instead of resurrecting this old one. That way you get notifications when others post in the thread, instead of the guy who started this thread a year ago. <img src="smileys/smiley2.gif" border="0" align="middle" />

  • Nimtrix

    Thanks for the reply. I assumed that because I was using similar code to what Yann displayed it would have been suitable to post in this thread.

    Anyway, I have reposted my query into a separate thread here

  • goldboyj:

    Yeah, I see what you mean.

    Thing is, it's often easier to get a reply if you make your own thread since people might think your reply to this thread is an answer and not a question, and therefore don't bother to have a look.

    Whereas if you see a new thread with no replies, you know the problem hasn't been solved yet.

    Also, there's the notification thing I mentioned earlier. Anyways, I'll have a look at your .capx and post something in the other thread.

  • Thanks Yann that was very simple and useful for me!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)