#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

Posts: 2,867Member
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

• Posts: 754Member
Cool! Too bad I already found another way but it is basically this here. Good job!
• Posts: 2,867Member
Good for you! Care to share?
• Posts: 9,119Member, Sous Chef, PRO, Bowlboy Sidekick
You little lumpy thing, you. Very nice project file. Thanks for sharing!
• Posts: 2,867Member
Thanks, nice and simple, the way I like it
• Posts: 93Member
Thanks for sharing! Works great!

• Posts: 2,867Member
My pleasure!
• Posts: 1,513Member
edited May 2013
@LudwigHeijden

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

Chakku
• Posts: 2,867Member
How can you say that? I have so much love to share =(( :P
• Posts: 2,313Member
really awesome @LumpApps thanks for sharing (don't know how I came here... )

# ✮ Available for hire! [email protected] ✮

• Posts: 62Member, PRO
Very useful. Thank you very much.
• Posts: 2,867Member
Your welcome guys. (Took me a while to notice you guys posted here)
• Posts: 22Member
very thank you
• Posts: 11,717Member, Sous Chef, PRO, Senior Sous-Chef
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.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Posts: 2,867Member
Thanks @tatiang You are absolutely right!