Rpg solide collision
For my game I need solid colisson for building trees please have you a recommandations
For my game I need solid colisson for building trees please have you a recommandations
You could use quick engine and set the gravity to zero
Here I made an example for you:
// --- Helper Function: AABB Collision Check ---
// Checks if two rectangles (rect1 and rect2) are overlapping.
// Each rect should have properties: x, y, width, height
checkRectCollision = function(rect1, rect2) // Very useful! I love using this!
return rect1.x < rect2.x + rect2.width and
rect1.x + rect1.width > rect2.x and
rect1.y < rect2.y + rect2.height and
rect1.y + rect1.height > rect2.y
end
// --- Tree Class Definition ---
Tree = class
constructor = function(spriteName, x, y, collisionWidth, collisionHeight, collisionOffsetY)
this.spriteName = spriteName // e.g., "tree01"
this.x = x // Drawing X coordinate (center bottom of sprite usually)
this.y = y // Drawing Y coordinate
this.collisionWidth = collisionWidth // Width of the collision box
this.collisionHeight = collisionHeight // Height of the collision box
this.collisionOffsetY = collisionOffsetY || 0 // Y offset from this.y to the top of the collision box
end
// Returns the actual collision rectangle based on the tree's current position and offsets
getCollisionRect = function()
return object
x = this.x - this.collisionWidth / 2 // Center the collision box horizontally
y = this.y + this.collisionOffsetY
width = this.collisionWidth
height = this.collisionHeight
end
end
draw = function()
// Assuming your sprite's (x,y) is its center-bottom for drawing
// Adjust as needed for your specific sprite origins
screen.drawSprite(this.spriteName, this.x, this.y)
// --- DEBUGGING ONLY: Draw the collision box ---
// local colRect = this.getCollisionRect()
// screen.setColor("#FF000080") // Semi-transparent red
// screen.drawRect(colRect.x, colRect.y, colRect.width, colRect.height)
// screen.setColor("white") // Reset color
end
end
// --- Player Class Definition (Simplified for collision example) ---
Player = class
constructor = function(x, y)
this.x = x
this.y = y
this.width = 16 // Player sprite width
this.height = 24 // Player sprite height
this.collisionWidth = 12 // Player's collision box width (e.g., for feet)
this.collisionHeight = 8 // Player's collision box height
this.collisionOffsetY = 16 // Offset from player.y to the top of their collision box
this.speed = 2
end
getCollisionRect = function()
return object
x = this.x - this.collisionWidth / 2
y = this.y + this.collisionOffsetY
width = this.collisionWidth
height = this.collisionHeight
end
end
// Checks if the player's proposed movement would collide with any tree
collidesWithTrees = function(trees, newPlayerX, newPlayerY)
// Create a temporary collision rect for the proposed new position
local proposedPlayerRect = object
x = newPlayerX - this.collisionWidth / 2
y = newPlayerY + this.collisionOffsetY
width = this.collisionWidth
height = this.collisionHeight
end
for tree in trees
local treeRect = tree.getCollisionRect()
if checkRectCollision(proposedPlayerRect, treeRect) then
return true // Collision detected!
end
end
return false // No collision
end
update = function(trees)
local moveX = 0
local moveY = 0
if keyboard.LEFT then moveX = moveX - this.speed end
if keyboard.RIGHT then moveX = moveX + this.speed end
if keyboard.UP then moveY = moveY - this.speed end
if keyboard.DOWN then moveY = moveY + this.speed end
// --- Collision Resolution (Slide against walls) ---
// Try moving horizontally first
local newX = this.x + moveX
if not this.collidesWithTrees(trees, newX, this.y) then
this.x = newX
end
// Then try moving vertically
local newY = this.y + moveY
if not this.collidesWithTrees(trees, this.x, newY) then
this.y = newY
end
end
draw = function()
// Draw player sprite
screen.drawSprite("player", this.x, this.y) // Assuming "player" is your sprite name
// --- DEBUGGING ONLY: Draw player's collision box ---
// local colRect = this.getCollisionRect()
// screen.setColor("#0000FF80") // Semi-transparent blue
// screen.drawRect(colRect.x, colRect.y, colRect.width, colRect.height)
// screen.setColor("white") // Reset color
end
end
// --- Game Global Variables ---
local player
local allTrees = []
// --- Game Init Function ---
init = function()
// Instantiate your player
player = new Player(screen.width / 2, screen.height / 2)
// Create some trees with their sprite name, x, y, collisionWidth, collisionHeight, collisionOffsetY
// Example: tree at (100, 150), collision box 20x10 pixels, offset 30 pixels down from its Y
allTrees.push(new Tree("tree_pine", 100, 150, 20, 10, 30))
allTrees.push(new Tree("tree_oak", 250, 100, 20, 10, 30))
allTrees.push(new Tree("tree_pine", 50, 250, 20, 10, 30))
allTrees.push(new Tree("tree_oak", 300, 200, 20, 10, 30))
end
// --- Game Update Function ---
update = function()
player.update(allTrees)
end
// --- Game Draw Function ---
draw = function()
screen.clear("deepskyblue") // Clear background
screen.fillRect(0, screen.height / 2, screen.width, screen.height / 2, "forestgreen") // Simple ground
// Draw trees and player in a specific order to handle "depth"
// This is a simple sorting by Y for visual depth
local drawables = []
drawables.push(player)
for tree in allTrees
drawables.push(tree)
end
// Sort by Y coordinate to make things appear in front or behind correctly
drawables.sortList(function(a, b)
if a.y < b.y then return -1
elsif a.y > b.y then return 1
else return 0
end
end)
for item in drawables
item.draw()
end
end