Hello, I have a small problem with my platformer collision system. I use SFML, C++ and Lua. Here is my code in lua to check collision between character and entity:
collision=function(character,tile)
local cLeft = character:getPositionX()
local cRight = character:getPositionX() + character:getBoundingRectWidth()
local cTop = character:getPositionY()
local cBottom = character:getPositionY() + character:getBoundingRectHeight()
local tLeft = tile:getPositionX()
local tRight = tile:getPositionX() + tile:getBoundingRectWidth()
local tTop = tile:getPositionY()
local tBottom = tile:getPositionY() + tile:getBoundingRectHeight()
--bottom collision
if cTop < tTop and cBottom < tBottom and cLeft < tRight and cRight > tLeft
then
character:setEntityPosition(character:getPositionX(),tTop - character:getBoundingRectHeight())
character:setVelocityY(0)
character:setOnGround(true)
--top collision
elseif cTop > tTop and cBottom > tBottom and cLeft < tRight and cRight > tLeft
then
character:setEntityPosition(character:getPositionX(),tBottom)
character:setVelocityY(0)
end
--right collision
if cLeft < tLeft and cRight < tRight and cTop < tBottom and cBottom > tTop
then
character:setEntityPosition(tLeft - character:getBoundingRectWidth(),character:getPositionY())
character:setVelocityX(0)
--left collision
elseif cLeft > tLeft and cRight > tRight and cTop < tBottom and cBottom > tTop
then
character:setEntityPosition(tRight,character:getPositionY())
character:setVelocityX(0)
end
end,
The problem is when character touch a corner of the rectangle, then he intersects for example left and top side in the same time, and character is pushed to the wrong direction, it very confuse me, idk how to fix that, I was thinking about to add a vector variable that let collision know that character noves on x or y axis but in my platformer character can move in both in the same time, for example when player push W and D in the same time, do u know any solution?