Grid Placement of Physics Objects?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Scratchthatguys
Prole
Posts: 8
Joined: Thu Jun 12, 2014 10:49 pm

Grid Placement of Physics Objects?

Post 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.)
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Grid Placement of Physics Objects?

Post 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.
Scratchthatguys
Prole
Posts: 8
Joined: Thu Jun 12, 2014 10:49 pm

Re: Grid Placement of Physics Objects?

Post 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?
Scratchthatguys
Prole
Posts: 8
Joined: Thu Jun 12, 2014 10:49 pm

Re: Grid Placement of Physics Objects?

Post 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?
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: Grid Placement of Physics Objects?

Post by Automatik »

You have to do "Body:destroy()" to destroy a physics object, if that's what you're talking about.
Scratchthatguys
Prole
Posts: 8
Joined: Thu Jun 12, 2014 10:49 pm

Re: Grid Placement of Physics Objects?

Post 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?
BetaBlaze
Prole
Posts: 5
Joined: Fri Jun 13, 2014 1:34 am

Re: Grid Placement of Physics Objects?

Post 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()
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests