Page 1 of 1

Collisions and images

Posted: Wed Oct 07, 2020 6:03 pm
by ProfB
Hello guys.

I am currently recreating "Space Invaders" but I find myself in trouble with collisions. My aliens are Quads with Images and my bullets are rectangles. I tried using their x and y coordinates along with their width and height to measure if my bullets were inside the alien, but that's not working very well. I am running out of ideas on how to do that and I'd appreciate if you could help me.

Thank you a lot.

Re: Collisions and images

Posted: Wed Oct 07, 2020 7:32 pm
by Xugro
It sounds like you are using AABB Collision Detection. That should work. What is the problem with your collision detection? My first guess would be that your bullets are too fast, but there can be others problems.

Here is a short article about AABB Collision Detection:
https://tutorialedge.net/gamedev/aabb-c ... -tutorial/

And here is a longer one (with a few more advanced techniques):
https://hopefultoad.blogspot.com/2017/0 ... ponse.html

Re: Collisions and images

Posted: Thu Oct 08, 2020 12:36 am
by sphyrth
Please provide us some of your code. People can help where you're going wrong and can offer the solution.

Re: Collisions and images

Posted: Thu Oct 08, 2020 5:37 am
by siberian
Maybe this sample can help

Re: Collisions and images

Posted: Thu Oct 08, 2020 12:00 pm
by ProfB
sphyrth wrote: Thu Oct 08, 2020 12:36 am Please provide us some of your code. People can help where you're going wrong and can offer the solution.
Here is my code so far.

Re: Collisions and images

Posted: Thu Oct 08, 2020 12:58 pm
by sphyrth
While I'm further studying your code. Let me give you some helpful tips:

Code: Select all

--- In your Bullet(x, y)

function init:draw()
    ....
    --- You shouldn't do this. The adjustment you made "init.x + 25" is only VISUAL. You won't see where the bullet REALLY is.
    love.graphics.rectangle("fill", init.x + 25, init.y, init.width, init.height)

    --- Just draw them as is:
    love.graphics.rectangle("fill", init.x, init.y, init.width, init.height)
    ....
end
I did the same principle in your Player() part.

Code: Select all

    function init:Shoot(x, y)
        local bullet = Bullet(x, y) -- I changed (x - 7, y) to just (x, y). Bear with me on this.
        table.insert(projectile_table, bullet)
    end

Code: Select all

    if love.keyboard.isDown("space") then
        function love.keypressed(key)
            if key == "space" then
                player:Shoot(player.x + 18, player.y) -- This is where I made all of that calculation. (-7 and + 25)
                -- This solution is STILL crude, but it at least shoots at the center of your screen.
            end
        end
    end
Finally, drawing:

Code: Select all

    function init:draw()
    	-- As good programming practice... put local image BEFORE the draw function. Don't put it inside it.
        local image = love.graphics.newImage("enemy.png")
        
        -- This is an interesting take on how you draw quads. But I recommend replacing these two lines below:
        init.quad = love.graphics.newQuad(init.x, init.y, init.width, init.height, image:getWidth()/4, image:getHeight()/4)
        love.graphics.draw(image, init.quad, init.x_table, init.y_table)
        
        -- With these:
        love.graphics.draw(image, init.x_table, init.y_table, 0, 1/4, 1/4)
        -- The zero is your rotation.
        -- The two 1/4 is your attempt to scale the image down to the quarter of its size.
    end

Re: Collisions and images

Posted: Thu Oct 08, 2020 4:26 pm
by ProfB
sphyrth wrote: Thu Oct 08, 2020 12:58 pm While I'm further studying your code. Let me give you some helpful tips:

Code: Select all

--- In your Bullet(x, y)

function init:draw()
    ....
    --- You shouldn't do this. The adjustment you made "init.x + 25" is only VISUAL. You won't see where the bullet REALLY is.
    love.graphics.rectangle("fill", init.x + 25, init.y, init.width, init.height)

    --- Just draw them as is:
    love.graphics.rectangle("fill", init.x, init.y, init.width, init.height)
    ....
end
I did the same principle in your Player() part.

Code: Select all

    function init:Shoot(x, y)
        local bullet = Bullet(x, y) -- I changed (x - 7, y) to just (x, y). Bear with me on this.
        table.insert(projectile_table, bullet)
    end

Code: Select all

    if love.keyboard.isDown("space") then
        function love.keypressed(key)
            if key == "space" then
                player:Shoot(player.x + 18, player.y) -- This is where I made all of that calculation. (-7 and + 25)
                -- This solution is STILL crude, but it at least shoots at the center of your screen.
            end
        end
    end
Finally, drawing:

Code: Select all

    function init:draw()
    	-- As good programming practice... put local image BEFORE the draw function. Don't put it inside it.
        local image = love.graphics.newImage("enemy.png")
        
        -- This is an interesting take on how you draw quads. But I recommend replacing these two lines below:
        init.quad = love.graphics.newQuad(init.x, init.y, init.width, init.height, image:getWidth()/4, image:getHeight()/4)
        love.graphics.draw(image, init.quad, init.x_table, init.y_table)
        
        -- With these:
        love.graphics.draw(image, init.x_table, init.y_table, 0, 1/4, 1/4)
        -- The zero is your rotation.
        -- The two 1/4 is your attempt to scale the image down to the quarter of its size.
    end
I managed to make it right, but sometimes the bullet will hit two enemies when colliding almost on their side, but it's good now. Now, a new doubt: can I make the aliens move like if they were making a step and stopping, stepping and stopping... like the original game?

Re: Collisions and images

Posted: Thu Oct 29, 2020 11:46 am
by darkfrei
Set the middle point, radius and check the round collision, it's much easier to check it.

Re: Collisions and images

Posted: Thu Oct 29, 2020 1:52 pm
by darkfrei
ProfB wrote: Thu Oct 08, 2020 4:26 pm Now, a new doubt: can I make the aliens move like if they were making a step and stopping, stepping and stopping... like the original game?
Just move them every n seconds?