Detect Collision and React moon game

Asked

Viewed 99 times

0

I wanted help with 2d square collision and how to prevent the character from passing through the block Follow the player code:

   local player = nil
Player = {}
player_skins = {
    human  = love.graphics.newImage("skin.png")
}

function Player:create()
player = {
    x = 1000,
    y = 1500,
    angle  = 0,
    speed = 1,
    xveloc = 0,
    yveloc = 0,
    maxveloc = 4
    }
end

function Player:movement(dt)
    if player.yveloc > 0 and player.yveloc > 0.1 then
        player.yveloc = player.yveloc - 10*dt
    elseif player.yveloc < 0 and player.yveloc < -0.1 then
        player.yveloc = player.yveloc + 10*dt
    elseif player.yveloc < 0.1 and  player.yveloc > 0 then
        player.yveloc = 0
    elseif player.yveloc < 0 and player.yveloc > -0.1 then
        player.yveloc = 0
    end
    if player.xveloc > 0 and player.xveloc > 0.1 then
        player.xveloc = player.xveloc - 10*dt
    elseif player.xveloc < 0 and player.xveloc < -0.1 then
        player.xveloc = player.xveloc + 10*dt
    elseif player.yveloc < 0.1 and  player.xveloc > 0 then
        player.xveloc = 0
    elseif player.yveloc < 0 and player.xveloc > -0.1 then
        player.xveloc = 0
    end
    if player ~= nil and not pause then
        player.x = player.x + player.xveloc
        player.y = player.y + player.yveloc 
    end
    if love.keyboard.isDown("w") then
        if player.yveloc  <= player.maxveloc and player.yveloc > (player.maxveloc - (player.maxveloc*2)) then
            player.yveloc = player.yveloc - (player.speed*dt*20)
        end
    end
    if love.keyboard.isDown("s") then
        if player.yveloc  < player.maxveloc and player.yveloc > (player.maxveloc - (player.maxveloc*2)) then
            player.yveloc = player.yveloc + (player.speed*dt*20)
        end
    end
    if love.keyboard.isDown("d") then
        if player.xveloc  < player.maxveloc and player.xveloc > (player.maxveloc - (player.maxveloc*2)) then
            player.xveloc = player.xveloc + (player.speed*dt*20)
        end
    end
    if love.keyboard.isDown("a") then
        if player.xveloc  < player.maxveloc and player.xveloc >         (player.maxveloc - (player.maxveloc*2)) then
            player.xveloc = player.xveloc - (player.speed*dt*50)
        end
    end
end

function Player:camera(dt)
    camera.x  = player.x - love.graphics.getWidth()/2
    camera.y = player.y- love.graphics.getHeight()/2
end

function Player:AngleRelatedToMouse()
    player.angle = math.atan2(love.mouse.getY() -     love.graphics.getHeight()/2,love.mouse.getX() - love.graphics.getWidth()/2) -         1.5
end

function Player:draw()
    love.graphics.draw(player_skins.human,player.x,player.y,player.angle,0.2,    0.2,player_skins.human:getWidth()/2,player_skins.human:getHeight()/2)
end

function Player:checkcollid()
    a = require ""

end

function Player:get()

return player
end

Also . the map I made in Tiled . all layers have the property Solid with 1 or 0 (I’ll put 2 in the future for doors and others) he puts everyone who has Solid 1 in a table called Wall

the code I made took all the elements present in the Wall table and checked the 4 sides to see if the player was touching the specific block I can’t release this part of the code because I deleted it last night and.e ... .

the problem is that when I check if the player is touching the block he Zera the speed x and y not letting the player go back . but continues to enter the block

I looked for all the guys I just found the aabb methods and the raycast that I could not do to prevent the player from entering the block inserir a descrição da imagem aqui

I am good in English and have functions that have no real meaning

  • @bigown The duplicate question has been removed?

  • @Phanpy was yes, you want to answer that?

  • @bigown I was wondering if I would... hm, I guess I’ll wait for a next question, you’re in need of an Edit yet :/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.