Screen Collision [BEGINNER]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Semeon
Prole
Posts: 36
Joined: Tue Jul 12, 2016 1:35 pm

Screen Collision [BEGINNER]

Post by Semeon »

Hey,
I am kinda new in the world of LOVE2D and I was looking for some "around the screen collision" but it seems like when I use my player to try to see if he is colliding, part of the player moves off the screen and when his speed is really high he goes off the screen. Bellow I have the code with the player's speed high so you can see the effect mentioned above. By the way the collision for the right part of the screen hasn't been done because I stopped during the process of writing the code cause I got mad :o :o !!!

Code: Select all

function love.load()
    player = {}
    
    player.x = 100
    player.y = 100
    player.w = 100
    player.h = 100

    player.speed = 100
end

function love.update(dt)
    if player.x < 0 then
        player.x = 0
    end
    if player.y < 0 then
        player.y = 0
    end
    
    if player.y + player.h >= 600 then
        player.y = 600 - player.h
    end    

    if love.keyboard.isDown("left") then
        player.x = player.x - player.speed
    end
    if love.keyboard.isDown("right") then
        player.x = player.x + player.speed
    end
    if love.keyboard.isDown("up") then
        player.y = player.y - player.speed
    end
    if love.keyboard.isDown("down") then
        player.y = player.y + player.speed
    end
end

function love.draw()
    love.graphics.rectangle("fill", player.x, player.y, player.w, player.h)
end
palmettos
Prole
Posts: 14
Joined: Sun May 29, 2016 7:00 pm

Re: Screen Collision [BEGINNER]

Post by palmettos »

Your player is going off-screen because you're updating its positions according to its speed after the collision check. Here's what's happening:

love calls update -> you check the player's position and set the position inside the playable area -> you update the player's position according to its speed (even if player.x == 0 and player.y == 0 before this step, player.x and player.y could now be negative or greater than screen width/height) -> love calls draw -> your rectangle is drawn at a position that could be either negative (off-screen) or greater than the screen width/height (off-screen).

You can fix this by performing your collision checks after updating positions.

You also probably want to multiply player.speed with dt in your movement expressions so that it's moving by that amount per second, not per frame.
User avatar
Semeon
Prole
Posts: 36
Joined: Tue Jul 12, 2016 1:35 pm

Re: Screen Collision [BEGINNER]

Post by Semeon »

Hey,
I sort of understand my mistake but how can I get the code working. But the thing I don't understand is your refrence "You can fix this by performing your collision checks after updating positions" , what do you really mean?
So this is and edit to the original message so what you are saying is to perform player control checks before I check for collision?

Thank you!
palmettos
Prole
Posts: 14
Joined: Sun May 29, 2016 7:00 pm

Re: Screen Collision [BEGINNER]

Post by palmettos »

Semeon wrote:so what you are saying is to perform player control checks before I check for collision?
Thank you!
Yes. This will give you the results you would expect:

Code: Select all

function love.load()
    player = {}
    
    player.x = 100
    player.y = 100
    player.w = 100
    player.h = 100

    player.speed = 100
end

function love.update(dt)

    if love.keyboard.isDown("left") then
        -- multiplying player.speed by dt makes the object move player.speed units per second
        -- otherwise it adds +/- 100 every frame, which will result in very fast movement
        player.x = player.x - player.speed * dt 
    end
    if love.keyboard.isDown("right") then
        player.x = player.x + player.speed * dt
    end
    if love.keyboard.isDown("up") then
        player.y = player.y - player.speed * dt
    end
    if love.keyboard.isDown("down") then
        player.y = player.y + player.speed * dt
    end
    
    -- check collisions after calculating movement in case the player moved out of bounds this iteration
    if player.x < 0 then
        player.x = 0
    end
    if player.x + player.w >= 800 then
    	player.x = 800 - player.w
    end
    if player.y < 0 then
        player.y = 0
    end
    if player.y + player.h >= 600 then
        player.y = 600 - player.h
    end
    
end

function love.draw()
    love.graphics.rectangle("fill", player.x, player.y, player.w, player.h)
end
Hopefully this makes sense.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 26 guests