Collision not working

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Geecko
Prole
Posts: 3
Joined: Mon Jan 20, 2020 11:09 am

Collision not working

Post by Geecko »

I don't know why, but colliders works only in one place(upper middle)
Attachments
Froguelike.love
This file should work
(6.61 KiB) Downloaded 169 times
Last edited by Geecko on Tue Jan 21, 2020 6:40 am, edited 1 time in total.
Sorry for bad english! :nyu:
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Collision not working

Post by pgimeno »

It was a bit difficult to get the program to work for me. I had to find my copy of dkjson.lua and copy it as Json.lua, and enable it at the top, otherwise I got a nil indexing error on the Json global.

Then I had another problem with the case of the directories. Once in the .love file, they are case sensitive. They are also case sensitive in most platforms where Löve is supported, including mine, just not in yours apparently.

Finally I ran into another problem where you were using the io functions instead of love.filesystem.read, meaning that the files can not work once the program is packaged into a .love file.

Once I could go past these hurdles, I could actually check your code :)

The immediate problem is that you're setting the XxxxCollision variables in a loop, but if it happens to be true in one iteration of the loop, you're overwriting it in the next iteration.

An easy solution is to change this:

Code: Select all

    for k, object in ipairs(Objects.list) do
        if object.name ~= "p_right_leg" and object.name ~= "p_left_leg" and object.name ~= "p_head" and object.name ~= "p_body"
        and object.name ~= "p_right_arm" and object.name ~= "p_left_arm" then
            if object.solid then
                print(object.name .. ": " .. "x: " .. object.x .. ", y: " .. object.y .. "; width: " .. object.width .. ", height: " .. object.height .. ";")
                LeftCollision = checkCollision(LeftCollider.x, LeftCollider.y, LeftCollider.width, LeftCollider.height, object.x, object.y, object.width, object.height)
                RightCollision = checkCollision(RightCollider.x, RightCollider.y, RightCollider.width, RightCollider.height, object.x, object.y, object.width, object.height)
                UpCollision = checkCollision(UpCollider.x, UpCollider.y, UpCollider.width, UpCollider.height, object.x, object.y, object.width, object.height)
                DownCollision = checkCollision(DownCollider.x, DownCollider.y, DownCollider.width, DownCollider.height, object.x, object.y, object.width, object.height)
            end
            object.x = object.x - Movement.x
            object.y = object.y - Movement.y
        end
    end
to this:

Code: Select all

    -- First, initialize all to false.
    LeftCollision = false
    RightCollision = false
    UpCollision = false
    DownCollision = false

    for k, object in ipairs(Objects.list) do
        if object.name ~= "p_right_leg" and object.name ~= "p_left_leg" and object.name ~= "p_head" and object.name ~= "p_body"
        and object.name ~= "p_right_arm" and object.name ~= "p_left_arm" then
            if object.solid then
                print(object.name .. ": " .. "x: " .. object.x .. ", y: " .. object.y .. "; width: " .. object.width .. ", height: " .. object.height .. ";")

                -- Now, we do: variable = variable or checkCollision... for each of the variables
                LeftCollision = LeftCollision or checkCollision(LeftCollider.x, LeftCollider.y, LeftCollider.width, LeftCollider.height, object.x, object.y, object.width, object.height)
                RightCollision = RightCollision or checkCollision(RightCollider.x, RightCollider.y, RightCollider.width, RightCollider.height, object.x, object.y, object.width, object.height)
                UpCollision = UpCollision or checkCollision(UpCollider.x, UpCollider.y, UpCollider.width, UpCollider.height, object.x, object.y, object.width, object.height)
                DownCollision = DownCollision or checkCollision(DownCollider.x, DownCollider.y, DownCollider.width, DownCollider.height, object.x, object.y, object.width, object.height)

            end
            object.x = object.x - Movement.x
            object.y = object.y - Movement.y
        end
    end
That solves the immediate problem. However, I can't help notice that you're checking an enormous amount of rectangles. Collision handling is a complex subject. I recommend you leave it in hands of a dedicated library. bump.lua is one such library. One clear advantage over your approach is that it only checks collisions with rectangles in the vicinity of the one being moved (your player in this case), instead of checking each and every rectangle. If your map keeps growing, that is going to end up being a bottleneck in your program.
Geecko
Prole
Posts: 3
Joined: Mon Jan 20, 2020 11:09 am

Re: Collision not working

Post by Geecko »

It's not works :cry:.
Also sorry, i forgot that this folder have to be outside .love file. I did it intentionally so that everyone can create mods for the game.
I will attach archive with properly located folders and files later, because now i fix bugs with love.filesystem.
Sorry for bad english! :nyu:
Geecko
Prole
Posts: 3
Joined: Mon Jan 20, 2020 11:09 am

Re: Collision not working

Post by Geecko »

I don't wanna change something in code and add libraries, because it will it will only complicate everything.
Sorry for bad english! :nyu:
Post Reply

Who is online

Users browsing this forum: No registered users and 132 guests