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.)
Grid Placement of Physics Objects?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 8
- Joined: Thu Jun 12, 2014 10:49 pm
Re: Grid Placement of Physics Objects?
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:
Now if you feed it, say, ( 120, 230 ) it will return ( 64, 192 ) which is a top-left corner in a 64x64 grid square.
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
-
- Prole
- Posts: 8
- Joined: Thu Jun 12, 2014 10:49 pm
Re: Grid Placement of Physics Objects?
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?
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?
-
- Prole
- Posts: 8
- Joined: Thu Jun 12, 2014 10:49 pm
Re: Grid Placement of Physics Objects?
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?
You have to do "Body:destroy()" to destroy a physics object, if that's what you're talking about.
-
- Prole
- Posts: 8
- Joined: Thu Jun 12, 2014 10:49 pm
Re: Grid Placement of Physics Objects?
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?
Pretty much. You can store all your objects into a table, with a Position property.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?
Code: Select all
local block1 = ...
block1.Name = "SpecialBlock"
block1.Position = {64, 128} -- note this line as well
table.insert(objects, block1)
Code: Select all
block1.Position = {128, 256}
Code: Select all
block1["Remove"] = function(self)
-- code to destroy itself
end
block1:Remove()
Who is online
Users browsing this forum: Google [Bot] and 3 guests