Good collision and gravity system in platformer game

Started by
1 comment, last by 8Observer8 3 years, 5 months ago

Well, I am creating my platformer game for fun,it will be no that simply one like mario, but I want add a lot of interesting stuff like equipment etc. I add collision and gravity that will works very well, I think there is a many ways to implement it so I dont want any crapy one in my game, I actually created collision in my game in lua.script (I use C++,sfml2 and lua for scripting)

    collision=function(character,tile)
   
        local tBottom = tile:getPositionY() + tile:getHeight();
        local tTop = tile:getPositionY();
        local tRight = tile:getPositionX() + tile:getWidth();
        local tLeft = tile:getPositionX();
       
        local cBottom = character:getPositionY() + character:getHeight();
        local cTop = character:getPositionY();
        local cRight = character:getPositionX() + character:getWidth();
        local cLeft = character:getPositionX();

        if cRight < tLeft then character:setVelocity(-1,0)
        elseif cLeft > tRight then character:setVelocity(1,0)
        elseif cTop < tBottom then character:setVelocity(0,-1)
        elseif cBottom < tTop then character:setVelocity(0,1) end
    end

but it works like crap and I dont like it, can you suggest me or give me any good tutorial how can I implement it well?

I dont want to create pixel perfect collision,I have tilemap, and tiles are only squares and rectangles with different size.

Advertisement

It is SFML. Do not pay attention that it is a Russian language. Just read a code:

This topic is closed to new replies.

Advertisement