♥ FREE Snap To Grid tutorial/template - some community love ♥

LumpAppsLumpApps Member Posts: 2,881
For the love of this community! An easy snap to grid tutorial and attached template

Snap to grid: THE MATH
- Take the X position of the actor and divide it by the grid size. The answer is the place on the X axis in the grid.
For example: the grid = 64px wide. Then an actor on self.position.X = 128 will be on place 2.
- The outcome needs to be floored (rounded down)
For example: the grid = 64px wide and the actors self.position.X = 133 the place on the grid will be 133 / 64 = 2.078125 floor(2.078125) = 2!
- To determine the position on the grid the place we found needs to be multiplied by the width of the grid.
Example: 2 x 64px =128px
- But since the actors X is in the centre of the actor and not on the (bottom-)left we need to add half of the grid width to the outcome
Example: 2 x 64px = 128px + 32px = 160px
- The actors size can be bigger or smaller then the grid size.
- Its the same for the Y position but off course the width of the grid and actor should be exchanged by height.

Put these two change attributes in the beginning of your actors and you will have a snap to grid behaviour.
self.Position.X = (floor( self.Position.X /64)*64)+32
self.Position.Y=(floor( self.Position.Y /64)*64)+32

Projectfile is attached to play with.

With love from Lump ;)

Comments

  • SolarPepperStudiosSolarPepperStudios Member Posts: 754
    Cool! Too bad I already found another way but it is basically this here. Good job! ;)
  • LumpAppsLumpApps Member Posts: 2,881
    Good for you! Care to share? :D
  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    You little lumpy thing, you. ;) Very nice project file. Thanks for sharing!
  • LumpAppsLumpApps Member Posts: 2,881
    Thanks, nice and simple, the way I like it ;)
  • GrapeSodaGamesGrapeSodaGames Member Posts: 93
    Thanks for sharing! Works great!
  • LumpAppsLumpApps Member Posts: 2,881
    My pleasure!
  • ChakkuChakku Member Posts: 1,513
    edited May 2013
    @LudwigHeijden

    Highly generous for a single community member :), but it doesn't make up for no community love week :-S

    Chakku
  • LumpAppsLumpApps Member Posts: 2,881
    How can you say that? I have so much love to share =(( :P
  • -Timo--Timo- Member Posts: 2,313
    really awesome @LumpApps ;) thanks for sharing (don't know how I came here... :D)
  • vuifahvuifah Member Posts: 62
    Very useful. Thank you very much.
  • LumpAppsLumpApps Member Posts: 2,881
    Your welcome guys. (Took me a while to notice you guys posted here)
  • DarkpaxDarkpax Member Posts: 22
    very thank you
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Good information. The only thing I would change is to use prec() instead of floor() to be able to round to the nearest whole number rather than always rounding down. If my actor is at grid square 2.99, I don't want it to go to grid square 2. I want it to go to 3. I used to use a mod() function and then a rule based on the result (higher or lower than 5) to round up with ceil() or down with floor() but prec() should do this a lot more efficiently without a rule.
  • LumpAppsLumpApps Member Posts: 2,881
    Thanks @tatiang You are absolutely right!
  • jlrobertson1993jlrobertson1993 Member, PRO Posts: 7

    This is great! Thank you! Not sure if you are still on this thread or not... but I am working with a game that has a similar function to it, but the execution is being odd. I want to be able to tap, move to a place on the grid and then place the item with one click/tap. On the computer previewer (PC) it works fine. But on the Android GameSalad previewer app, I have to double tap to place the item on the grid. This slows down the play of the game and my husband (playtesting) says it is annoying enough that he would delete the game, even though the rest is fun. Any ideas on what could be causing this? Is there a post somewhere that explains it? I jumped on here because the touch/mouse/press/etc is all that is missing from the original post. Thanks!

  • LumpAppsLumpApps Member Posts: 2,881

    Hi @jlrobertson1993 Thanks for your reply. It has been a while since I used GameSalad so don't think I'm of much help. Maybe some of the other veterans can jump in ;)

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited August 2019

    @jlrobertson1993 You might want to start a new thread for this conversation but I'm wondering... are you wanting the user to tap and drag and then release? If so, I have a few demos of this I can post.

  • jlrobertson1993jlrobertson1993 Member, PRO Posts: 7

    @tatiang I have been wanting to post a new thread, but I am worried that I won't be able to explain the background of how the game is built currently enough to get an answer, which is why I jumped on here, as others seemed to already know the basic foundation without me muddling an explanation. But, yes, tap/drag/release is what I am after! :-) Thank you!!!

  • jlrobertson1993jlrobertson1993 Member, PRO Posts: 7

    @LumpApps Thank you!! :-)

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited August 2019

    @jlrobertson1993 Are any of these demos helpful?

  • jlrobertson1993jlrobertson1993 Member, PRO Posts: 7

    @tatiang I will check them out! Thank you!!!!

Sign In or Register to comment.