Re: Simple Tiled Implementation - STI v0.14.1.10
Posted: Fri Jan 01, 2016 8:49 pm
Yep, figured it out, thanks! Here's what I'm doing (powObject is the map object, powFixture is the associated fixture I created):
Next question: I want to have multiple maps that smoothly connect to each other -- each map is a separate "room", and I want to be able to assemble different arrangements of rooms on the fly, so I don't even know what their positions will be relative to each other until runtime. This requires me to offset the map and its collision data. I added this method to the Map class, which causes it to draw the map at a different location:
and offset the collision detection with this:
But bizarrely, I have to apply 2x the translation to the body that I do to the map. Like, if I move the collision body by (50, 50) then I have to call map:offset(25, 25). Any idea why that is?
More importantly, the map refuses to draw if it moves too far. I'm guessing there's some kind of automatic culling going on. What's the recommended way to deal with this? Am I barking up the wrong tree entirely?
Code: Select all
-- Delete the object from the map's object list.
map.objects[powObject.id] = nil
-- Delete the object from the layer in the map it is in.
local newObjects = {}
for i, obj in ipairs(powObject.layer.objects) do
if obj ~= powObject then
table.insert(newObjects, obj)
end
end
powObject.layer.objects = newObjects
-- Stop drawing the object.
map:setObjectSpriteBatches(powObject.layer)
Code: Select all
function Map:offset(dx, dy)
self.offsetx = self.offsetx + dx
self.offsety = self.offsety + dy
for _, layer in pairs(self.layers) do
layer.x = layer.x + dx
layer.y = layer.y + dy
end
end
Code: Select all
local body = map.box2d_collision.body
body.setPosition(body.getX() + dx, body.getY() + dy)
More importantly, the map refuses to draw if it moves too far. I'm guessing there's some kind of automatic culling going on. What's the recommended way to deal with this? Am I barking up the wrong tree entirely?