Collisions and images

Show off your games, demos and other (playable) creations.
Post Reply
ProfB
Prole
Posts: 4
Joined: Wed Oct 07, 2020 5:20 pm

Collisions and images

Post 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.
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Collisions and images

Post 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
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Collisions and images

Post by sphyrth »

Please provide us some of your code. People can help where you're going wrong and can offer the solution.
siberian
Prole
Posts: 21
Joined: Sat Apr 06, 2019 1:42 pm

Re: Collisions and images

Post by siberian »

Maybe this sample can help
Attachments
test_aabb_x_aabb.love
(1.61 KiB) Downloaded 250 times
ProfB
Prole
Posts: 4
Joined: Wed Oct 07, 2020 5:20 pm

Re: Collisions and images

Post 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.
Attachments
ball.rar
(3.34 KiB) Downloaded 221 times
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Collisions and images

Post 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
ProfB
Prole
Posts: 4
Joined: Wed Oct 07, 2020 5:20 pm

Re: Collisions and images

Post 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?
Attachments
ball.rar
(3.29 KiB) Downloaded 210 times
User avatar
darkfrei
Party member
Posts: 1174
Joined: Sat Feb 08, 2020 11:09 pm

Re: Collisions and images

Post by darkfrei »

Set the middle point, radius and check the round collision, it's much easier to check it.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1174
Joined: Sat Feb 08, 2020 11:09 pm

Re: Collisions and images

Post 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?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests