Page 1 of 1

Grid Placement of Physics Objects?

Posted: Thu Jun 12, 2014 11:05 pm
by Scratchthatguys
I'm making a game that involves bouncing particles around to solve puzzles using physics. Basically, you have five types of blocks; a particle emitter, a particle receiver, blocks that make a particle bounce (change direction) when hit, blocks that absorb particles when hit, and blocks that can't be bounced into but can be passed through. I have the actual game done, I just need to place and remove them on a grid. Is there any easy way to do this besides using tables?

(Note: I don't need help with the actual physics part; I just need some advice on a grid placement and destruction mechanism.)

Re: Grid Placement of Physics Objects?

Posted: Fri Jun 13, 2014 9:34 am
by Plu
If you use a bit of math you can convert any screen-coordinate into a grid-coordinate. If the game already works with objects and you just want to limit the valid values for coordinates, this would probably work:

Code: Select all

function convertPointToGridLockedPoint(x,y)
  local gridSize = 64 -- or whatever
  x = math.floor( x / gridSize ) * gridSize
  y = math.floor( y / gridSize ) * gridSize
  return x,y
end
Now if you feed it, say, ( 120, 230 ) it will return ( 64, 192 ) which is a top-left corner in a 64x64 grid square.

Re: Grid Placement of Physics Objects?

Posted: Fri Jun 13, 2014 4:11 pm
by Scratchthatguys
Thanks! Now I can use that for other games that need grid placement. I hope it works!

EDIT: It does, but I wonder if I'm understanding it correctly. I think it finds what grid number it falls in (the math.floor of the position divided by the grid) and it sets it to the actual grid position (the math.floor of position / grid size, * the grid size). That's how it works, right?

Re: Grid Placement of Physics Objects?

Posted: Fri Jun 13, 2014 5:30 pm
by Scratchthatguys
I have another problem. I need to delete ANY object in the grid, not just one type. I don't want to have to write a line of code for each of the types of blocks, so can I just remove them from the objects table? Would I have to go through the entire objects list looking for whatever is at those coordinates?

Re: Grid Placement of Physics Objects?

Posted: Fri Jun 13, 2014 5:52 pm
by Automatik
You have to do "Body:destroy()" to destroy a physics object, if that's what you're talking about.

Re: Grid Placement of Physics Objects?

Posted: Fri Jun 13, 2014 8:28 pm
by Scratchthatguys
I know how to delete them, but do I really have to iterate over all the objects to find which is at those specific coordinates?

Re: Grid Placement of Physics Objects?

Posted: Sat Jun 14, 2014 8:10 pm
by BetaBlaze
Scratchthatguys wrote:I know how to delete them, but do I really have to iterate over all the objects to find which is at those specific coordinates?
Pretty much. You can store all your objects into a table, with a Position property.

Code: Select all

local block1 = ...
block1.Name = "SpecialBlock"
block1.Position = {64, 128} -- note this line as well
table.insert(objects, block1)
Then, you can create a LocateByPosition function that will iterate through the 'objects' table and look for one with that Position. The rendering code would use the Position property. I find this easier because if I wanted to change the position later, I'd just do

Code: Select all

block1.Position = {128, 256}
There's also the opportunity to add a Remove function to each object.

Code: Select all

block1["Remove"] = function(self)
-- code to destroy itself
end

block1:Remove()