playMaker

Author Topic: Snapping/Grid  (Read 2199 times)

Dabora787

  • Playmaker Newbie
  • *
  • Posts: 3
Snapping/Grid
« on: May 22, 2016, 08:06:03 PM »
I am currently making a game (2D) that allows the player to design the interior of a space ship. (Wall placement, doors, machines) I am currently using a grid system with points at intersections. I currently have it to when they select there a piece it spawns in and finds closest point using a gameobject variable and (Find Closest) action. 1st is this the easiest way or does anyone know a better way. 2nd they continue to spawn at the same point. This isnt a huge deal as the player has the ability to move the object around and snap to a new point. Was just curious if someone had any tips they could share. Thanks for any help in advance

Chizzler

  • Junior Playmaker
  • **
  • Posts: 67
Re: Snapping/Grid
« Reply #1 on: May 22, 2016, 09:27:22 PM »
The easiest way to do grid style placement is probably to grab the object position, seperate the Vector X,Y,Z and use "Float Round to nearest" on your chosen floats (i.e. forcing "X" to round to nearest 5.0) followed by "Set Position". You'll notice this method doesn't actually use a physical grid, it just forces placement based on float restrictions.

Now, you might need that physical grid for other things, and your current method works for that, but it's rather expensive. You're looping through every value in the array and testing it's distance from your object to determine the closest point. Instead, you can keep the array, and work out which index in your array your object is at based on it's position (after you've rounded it using the method above).

x = object position X axis
tsx = tilesize X (distance between grid points X axis)
y = Object Position Y axis
tsy = tilesize Y (distance between grid points Y axis)
cols = number of columns in your grid

((y / tsy) * cols)) + (x / tsx) = index

Note: Your array must start at index 0 for this to work.
« Last Edit: May 23, 2016, 09:13:21 AM by Chizzler »