Setting Boundary/Border/Frame for In Game Snapping to Grid

Mack_DundeeMack_Dundee Member Posts: 31
edited November -1 in Working with GS (Mac)
Hey guys,

I'm trying to snap an actor to particular coordinantes during gameplay. There are several great templates that show how to control movement according to the width/height of the grid cells. The problem I am having is that each of these templates uses the entire screen as a grid and I want to have a boundary (or border) around my grid. I'm not sure how to offset the x (or the y) by, for example, 10 pixels to the left before the actor snaps to the grid.

So, imagine a grid that snaps the actor every 40 pixels starting to snap at 50 pixels from the left (allowing for the 10 pixel boundary) instead of at 40 pixels from the left (not compensating for the boundary). Then it would snap again at 90 pixels instead of 80 pixels and so on. Each snap would add 10 pixels to the left to set the boundary, but would then maintain the 40 pixel separation between grid spots.

Using just the x coordinante, I've found two ways to effectively snap an actor to a nearby grid cell. Either way, I can't quite figure out the final offset math.

1) (ceil((floor(( self.Position.X / game.cellWidth )*2))/2))* game.cellWidth
2) ( game.cellWidth /2)+ game.cellWidth *floor( self.Position.X / game.cellWidth )

Thanks.

Comments

  • Mack_DundeeMack_Dundee Member Posts: 31
    Just to clarify, what I'm asking for is how to mathematically generate a grid not for the entire screen, but only a portion of the screen. The above two formulas generate the grid over the entire 420x320 when I want to limit it to an area of something like 390x300.

    Any help or suggestions would be much appreciated. Thanks for your time.
  • Mack_DundeeMack_Dundee Member Posts: 31
    One more thing, the "cellWidth" attribute mentioned above sets the size of each grid spot in the x dimension. I also have a "cellHeight" attribute for the y dimension. Since the cellWidth and cellHeight are both the same, I'll probably just end up using a single attribute for both values. I'll try something like, "slotSize." Just wanted to try and clear that up in case there was any confusion.
  • Mack_DundeeMack_Dundee Member Posts: 31
    For anyone who has this problem in the future and finds this forum post using search, I recommend the template titled "Snap to Grid." This template is new (at least as I post this message) and features an image that reads "Advanced Snap to Grid DEMO." I hope the template helps you figure things out.

    I'm not sure who did the template, but I really appreciate the help. Thanks to...whomever.
Sign In or Register to comment.