GungnirDev wrote:I am not quite sure where you are meaning for me to plug these in. I've rearranged the code a few times but I keep receiving 'syntax' or 'nil' errors. I've not done many shortcuts in LOVE before.
Code: Select all
platform = {}
function newplatform(x, y, w, h)
return {x = x, y = y, width = w, height = h}
end
function drawplatform(platform)
local x, y, w, h = unpack(platform)
love.graphics.rectangle("fill", x, y, w, h)
end
function love.load()
end
function love.update(dt)
table.insert(platforms, newplatform(x, y, w, h))
end
function love.draw()
drawplatform(platforms[i])
end
To better help you understand my predicament, I am already able to create rectangular platforms. However, I have platform-images with irregular shapes, and I want to use these as platforms. So, I want to create a platform for each horizontal surface of this platform, and also post the image, and create this as a single platform (let's say, called, natplat2).
Then, by typing natplat2(x, y) I should be able to place this platform anywhere I choose. Does that make more sense?
Oh, okay then.
So, if you want to use images to make objects, you have a few ways of doing this. Some are harder, some are not:
• Convert pixels to platforms (slower, more work for computer, more chances to fail, much more precision for collisions *precision is variable, depending on the computer speed);
• Convert lines of pixels to platform (slow, but not as much as pixels, more work for computer, smaller chances to fail, much more precision for collision);
• Convert blocks of pixels to platforms by using an image mask (fast, less work for computer, little chances to fail, good precision, but not as much as pixel maps);
I already used the first option in a mod I made for Mari0, called
Mari0 +Portal. There's a tile property I made named "
Pixel collision" that converts each pixel of a tile to a collidable point. It is very slow and fails many times (because of slowing down PC speed), but if I were/am going to remake that, I'd use the second option because of it's precision. For your case, I'd use the last.
Make it so when you're loading the image, it groups similar color of pixels and "draws" a rectangle based on these points. Something like this:
Code: Select all
local imgdata = love.image.newImageData(platform mask image)
local platsToDo = {}
for x = 0, imgdata:getWidth()-1 do
for y = 0, imgdata:getHeight()-1 do
local r, g, b, a = imgdata:getPixel(x, y)
if a == 255 then --If opaque, and not transparent
if platsToDo[r.."-"..g.."-"..b] then
platsToDo[r.."-"..g.."-"..b][2] = {x+1, y+1}
else
platsToDo[r.."-"..g.."-"..b] = {}
platsToDo[r.."-"..g.."-"..b][1] = {x+1, y+1}
end
end
end
end
for i, v in pairs(platsToDo) do
if v[1] and v[2] then --make a rectangle
table.insert(platforms, platform:new(v[1][1], v[1][2], v[2][1]-v[1][1], v[2][2]-v[1][2])
end
end
But in the example above (which should work, so feel free to use if you want), I'm directly converting them into the screen. If your games dimensions don't apply (tiles, scale or something else) or your platform should not be made in the same spots as the image mask (multiple platforms in different places, for example), just convert to your game's dimension on the "table.insert" part, where it creates the platforms. Also, if instead of the system "x, y, width, height" you're using a system like "x1, y1, x2, y2", just remove the values being subtracted in the last 2 arguments on the platform creator.
I hope I helped. If not, just let me know
EDIT: Here's an example of it based on your image. I had to make it on Windows XP paint, because I'm not in my computer, so sorry in advance for the quality. Just make like in the image below, except for the background (use a transparent background too). You have to make each rectangle of a different color, but, by using some program other than Paint I'm sure you can do a lot better
![Wink ;)](./images/smilies/ms-wink.png)