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.
Collisions and images
Re: Collisions and images
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
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
Please provide us some of your code. People can help where you're going wrong and can offer the solution.
Re: Collisions and images
Maybe this sample can help
- Attachments
-
- test_aabb_x_aabb.love
- (1.61 KiB) Downloaded 276 times
Re: Collisions and images
Here is my code so far.
- Attachments
-
- ball.rar
- (3.34 KiB) Downloaded 241 times
Re: Collisions and images
While I'm further studying your code. Let me give you some helpful tips:
I did the same principle in your Player() part.
Finally, drawing:
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
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
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
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?sphyrth wrote: ↑Thu Oct 08, 2020 12:58 pm While I'm further studying your code. Let me give you some helpful tips:
I did the same principle in your Player() part.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
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
Finally, drawing: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
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
- Attachments
-
- ball.rar
- (3.29 KiB) Downloaded 228 times
Re: Collisions and images
Set the middle point, radius and check the round collision, it's much easier to check it.
Re: Collisions and images
Just move them every n seconds?
Who is online
Users browsing this forum: No registered users and 1 guest