ObjectLibrary
is a simple library for the LÖVE2D game engine that allows users to create objects with collision detection and optional textures. If a texture is not provided, the object defaults to a simple square shape. This library can help developers quickly add basic object collision and rendering to their LÖVE2D projects.
- Easy Object Creation: Define objects with positions, sizes, and optional textures.
- Collision Detection: Automatically detects and handles collisions between objects.
- Collision Response: Objects cannot overlap; they stop moving into each other when colliding.
- Simple Rendering: Renders objects with textures, or as a default square if no texture is specified.
- Download
ObjectLibrary.lua
and place it in your LÖVE2D project folder. - Include it in your main file (
main.lua
or equivalent) with:local ObjectLibrary = require("ObjectLibrary")
You can create an object by calling ObjectLibrary:new(x, y, width, height, imagePath)
, where:
x
andy
specify the object's position.width
andheight
specify its dimensions.imagePath
is an optional parameter for adding a texture image.
local ObjectLibrary = require("ObjectLibrary")
local player = ObjectLibrary:new(100, 100, 50, 50, "player.png")
local wall = ObjectLibrary:new(200, 100, 50, 50) -- default to white square
In your love.update(dt)
function, you can move objects and check for collisions:
function love.update(dt)
-- Store previous position
local prevX, prevY = player.x, player.y
-- Move player using arrow keys
if love.keyboard.isDown("right") then player.x = player.x + 100 * dt end
if love.keyboard.isDown("left") then player.x = player.x - 100 * dt end
if love.keyboard.isDown("up") then player.y = player.y - 100 * dt end
if love.keyboard.isDown("down") then player.y = player.y + 100 * dt end
-- Resolve collision
player:resolveCollision(wall)
-- If collision occurs, restore previous position
if player.isColliding then
player.x, player.y = prevX, prevY
end
end
To render your objects, call draw()
on each object in your love.draw()
function:
function love.draw()
player:draw()
wall:draw()
-- Draw a red border if collision is detected
if player.isColliding then
love.graphics.setColor(1, 0, 0)
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
love.graphics.setColor(1, 1, 1) -- reset color
end
end
your_project/
├── main.lua
├── ObjectLibrary.lua
└── assets/
└── player.png
In this example, main.lua
initializes the library and uses it to create objects. The assets/
folder holds any textures (like player.png
) used by objects.
This library is free to use under the MIT License. See LICENSE for more details.
Feel free to open issues or submit pull requests with improvements or additional features. All contributions are welcome!
Developed by Virus.