Help! Need help with bullet and enemy collision

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
ThatChillHomie
Prole
Posts: 5
Joined: Sun Jun 10, 2018 1:52 am

Help! Need help with bullet and enemy collision

Post by ThatChillHomie »

Hello , I am trying to make a working collision detection and response for my players bullets hitting the enemy ships. I would prefer to do this the simple way without using physics since this is a very simple game. I have looked at all the other threads talking about this and trust me its all over complicated non sense this is my first real project so thank you to anyone that can help. :awesome:

Heres my code:

paused = 'true'

time = 0
timeinbetweenbullets = 0.4
spawntimer = 1
enemies = {}

function love.load()
player = {}
player.x = 20
player.y = 20
player.width = 200
player.height = 100
player.speed = 250





bullet = 0
player.bullets = {}
function player.fire()
bullet = {}
bullet.x = player.x + 48
bullet.y = player.y
bullet.hitbox = 10
table.insert(player.bullets,bullet)
end
player_sprite = love.graphics.newImage('gfx/SpaceShip.png')
background = love.graphics.newImage('gfx/Space.png')
enemy_sprite = love.graphics.newImage('gfx/alien.png')

end




function spawnEnemies()
local enemy = {}
enemy.x = math.random(800)
enemy.y = math.random(50,100)
hitbox = {}
hitbox.x = 50
hitbox.y = 60
table.insert(enemies,enemy)
end

function love.update(dt)
time = time + dt
if love.keyboard.isDown('d') and player.x + player.width < 900 then
player.x = player.x + player.speed*dt
end
if love.keyboard.isDown('a') and player.x > 0 then
player.x = player.x - player.speed*dt
end
if love.keyboard.isDown('w') and player.y > 0 then
player.y = player.y - player.speed*dt
end
if love.keyboard.isDown('s') and player.y + player.height < 600 then
player.y = player.y + player.speed*dt
end
if love.keyboard.isDown('escape') then
love.event.quit()
end
if love.keyboard.isDown('enter') then
paused = 'false'

end
if love.keyboard.isDown('space') and
time > timeinbetweenbullets then
time = 0
player.fire()

end


for _,b in pairs(player.bullets) do
b.y = b.y - 5

end

spawntimer = spawntimer - dt
if spawntimer <= 0 then
spawnEnemies()
local leftover = math.abs(spawntimer)
spawntimer = 1 + leftover
end





function love.draw()


for i = 0, love.graphics.getWidth() / background:getWidth() do
for j = 0, love.graphics.getHeight() / background:getHeight() do
love.graphics.draw(background, i * background:getWidth(), j * background:getHeight())

love.graphics.draw(player_sprite,player.x,player.y)

for _,e in pairs(enemies) do
love.graphics.draw(enemy_sprite,e.x,e.y)
end
for _,h in pairs(enemies) do
love.graphics.rectangle('line',h.x+26,h.y+32,50,38)
end
for _,s in pairs(player.bullets) do
love.graphics.rectangle('line',s.x,s.y,bullet.hitbox,bullet.hitbox)



for _,b in pairs(player.bullets) do
love.graphics.rectangle('fill',b.x,b.y,5,12)
end

end
end
end
end
end
Last edited by ThatChillHomie on Tue Oct 09, 2018 11:44 pm, edited 1 time in total.
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: HELP! Need to center(offset) hitboxes so i can center them

Post by NotARaptor »

This can be done in lots of different ways.

The easiest, based on the code you've shown, would be to always assume that the x,y pair represents the centre of the enemy, both graphically and for hitbox purposes.

(Note than in your code excerpt, the `hitbox` variable in your `spawnEnemies()` isn't used for anything)

Basically something like this:

Code: Select all

enemies = {}

function drawEnemies()
 local _, enemy
 for _,enemy in pairs(enemies) do 
  -- This assumes a fixed visual size for the enemies - adjust as required
  love.graphics.rectangle('line',enemy.x-25,h.enemy-25,50,50) 
 end
end

function spawnEnemy()
 -- local so we don't pollute the global namespace - this can lead to odd bugs
 local enemy = {}
 enemy.x = math.random(800) 
 enemy.y = math.random(50, 100) 
 enemy.hitbox_half_width = 25
 enemy.hitbox_half_height = 60
 table.insert(enemies, enemy)
end

function checkEnemiesCollide(enemy1, enemy2)
 return math.abs(enemy1.x-enemy2.x)<=(enemy1.hitbox_half_width+enemy2.hitbox_half_width) and math.abs(enemy1.y-enemy2.y)<=(enemy1.hitbox_half_height+enemy2.hitbox_half_height)
end
Obviously, you might want to store the hitbox info in a separate table, especially if the hitbox IS offset from the centre. You'll probably also want to store more graphical info in the enemy struct - but if you always remember what the x,y coords represent (in this case the centre of the enemy), it should be relatively easy.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: HELP! Need to center(offset) hitboxes so i can center them

Post by zorg »

Just one extra information, you don't need to declare "_" and "enemy" as local variables if you declare them in a for loop that way, since they'll be local to that already.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest