Player-enemy collision (I'm seriously stumped! : update)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- GungnirDev
- Party member
- Posts: 119
- Joined: Thu Jun 28, 2012 10:31 pm
Player-enemy collision (I'm seriously stumped! : update)
I need to make the enemies collide with the player and (for now) destroy the the player. I may add a health system later. I tried using the same script as I used for the bullet-enemy collision for the enemy-player collision but to no avail. The bullet code I used is under ship.lua. Nothing I've tried works or even changes, the enemies just pass right through. Please help.
Also, I'd like to know how I can spawn more enemies once there are no enemies on the screen.
Also, I'd like to know how I can spawn more enemies once there are no enemies on the screen.
- Attachments
-
- shmup.love
- (706.93 KiB) Downloaded 115 times
Last edited by GungnirDev on Sun Aug 05, 2012 9:05 am, edited 1 time in total.
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Bullets are the beauty and I don't know why
Re: Player-enemy collision (I'm seriously stumped!)
why you declare CheckCollision() function within love.update() ?
Move its declaration somether outside love.update(). Better place it before all require comands in main.lua
and watch your code identation! Some times it is hard to tell where your functions ends and where starts next function.
And check your hitboxes. They do not match images you use for player and enemies.
If change all this and put code where it should be, yours CheckCollision() works pretty well and collides player with enemies.
Also delete enemies when wey get out of screen. Then you can check if there is no one to fight (if # enemies = 0 then) and add another foes.
Move its declaration somether outside love.update(). Better place it before all require comands in main.lua
and watch your code identation! Some times it is hard to tell where your functions ends and where starts next function.
And check your hitboxes. They do not match images you use for player and enemies.
If change all this and put code where it should be, yours CheckCollision() works pretty well and collides player with enemies.
Also delete enemies when wey get out of screen. Then you can check if there is no one to fight (if # enemies = 0 then) and add another foes.
- GungnirDev
- Party member
- Posts: 119
- Joined: Thu Jun 28, 2012 10:31 pm
Re: Player-enemy collision (I'm seriously stumped! : update)
OK, I put the BoundingBox in main.lua like you suggested but it's still not working. I've having a hard time wrapping my head around collision, I can't do much of anything with this function cause I don't know why it works (or doesn't, in my case)
I did try playing with the numbers and that at least makes me somewhat understand exactly what the collision function does, but then I tried changing the width and height of the collision functions to 100s to see if that would work (it didn't). Then I tried changing the v.x's to direct player values and taking out the ipairs function to see if a direct approach would work (it didn't) and, since I thought that maybe it would help if the result was a simpler thing that I had already made happen, I changed the desired "result" to love.event.quit() (because I can do that with the escape function) and that didn't work either.
I'm not lazy...just slow... I'm trying though.
I'm sure there's a million things wrong with this code.
I did try playing with the numbers and that at least makes me somewhat understand exactly what the collision function does, but then I tried changing the width and height of the collision functions to 100s to see if that would work (it didn't). Then I tried changing the v.x's to direct player values and taking out the ipairs function to see if a direct approach would work (it didn't) and, since I thought that maybe it would help if the result was a simpler thing that I had already made happen, I changed the desired "result" to love.event.quit() (because I can do that with the escape function) and that didn't work either.
I'm not lazy...just slow... I'm trying though.
Code: Select all
if CheckCollision(player.x,player.y,100,100,enemy.x,enemy.y,100,100) then
love.event.quit()
- Attachments
-
- shmup.love
- (707.12 KiB) Downloaded 77 times
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Bullets are the beauty and I don't know why
Re: Player-enemy collision (I'm seriously stumped! : update)
Again - watch your code indentation!
I love Python for its strict indentation, very nice option for programming language you learn first.
So, take a look at your ship.lua file. You placed collision detection in lines 72-74. Very likely that your problem is what you think, what this is the end of player.update() function. It looks so by indentation of blocks on lines 64-70 and by end at line 76.
But indentation is wrong. You make a trap for yourself.
end on line 76 just closes for opened on line 43. And this for block is used to process bullets. So, no bullets - no collision detection.
I love Python for its strict indentation, very nice option for programming language you learn first.
So, take a look at your ship.lua file. You placed collision detection in lines 72-74. Very likely that your problem is what you think, what this is the end of player.update() function. It looks so by indentation of blocks on lines 64-70 and by end at line 76.
But indentation is wrong. You make a trap for yourself.
end on line 76 just closes for opened on line 43. And this for block is used to process bullets. So, no bullets - no collision detection.
Re: Player-enemy collision (I'm seriously stumped! : update)
One more trick i like:
put comment on end for very big blocks. It helps to track where this end belongs to
or
put comment on end for very big blocks. It helps to track where this end belongs to
Code: Select all
function player.update(dt)
-- ... many many pages of very complex code...
end -- function player.update
Code: Select all
for i,v in ipairs(player.shots) do
-- ... many many pages of very complex code...
end -- bullets processing
Re: Player-enemy collision (I'm seriously stumped! : update)
It's much simpler than you think.thesmeagle wrote:OK, I put the BoundingBox in main.lua like you suggested but it's still not working. I've having a hard time wrapping my head around collision, I can't do much of anything with this function cause I don't know why it works (or doesn't, in my case)
Imagine two rectangles (r1 and r2):
Code: Select all
-----------
----------- | |
| | | (r2) |
| (r1) | | |
| | -----------
-----------
is the RIGHT side of r1 LESS than the LEFT side of r2
OR
is the LEFT side of r1 GREATER than the RIGHT side of r2
If any of these two conditions is TRUE the two boxes are NOT intersecting.
That's all there is to it! ...well almost, we have to check the Y axis as well, but it's the same principle.
To put in pseudo code:
Code: Select all
if r1.RIGHT < r2.LEFT or r1.LEFT > r2.RIGHT then
return false -- no collision
end
if r1.BOTTOM < r2.TOP or r1.TOP > r2.BOTTOM then
return false -- no collision
end
return true -- in any other case they must be intersecting
If I remember correctly BoundingBox.lua accepts an X and Y position for the TOP-LEFT corner of each rectangle along with their WIDTH/HEIGHT.
You could easily write your own version that accept LEFT, TOP, RIGHT, BOTTOM coords for each rectangle.
Re: Player-enemy collision (I'm seriously stumped! : update)
It's much simpler than you think.thesmeagle wrote:OK, I put the BoundingBox in main.lua like you suggested but it's still not working. I've having a hard time wrapping my head around collision, I can't do much of anything with this function cause I don't know why it works (or doesn't, in my case)
Imagine two rectangles (r1 and r2):
Code: Select all
-----------
----------- | |
| | | (r2) |
| (r1) | | |
| | -----------
-----------
is the RIGHT side of r1 LESS than the LEFT side of r2
OR
is the LEFT side of r1 GREATER than the RIGHT side of r2
If any of these two conditions is TRUE the two boxes are NOT intersecting.
That's all there is to it! ...well almost, we have to check the Y axis as well, but it's the same principle.
To put in pseudo code:
Code: Select all
if r1.RIGHT < r2.LEFT or r1.LEFT > r2.RIGHT then
return false -- no collision
end
if r1.BOTTOM < r2.TOP or r1.TOP > r2.BOTTOM then
return false -- no collision
end
return true -- in any other case they must be intersecting
If I remember correctly BoundingBox.lua accepts an X and Y position for the TOP-LEFT corner of each rectangle along with their WIDTH/HEIGHT.
You could easily write your own version that accept LEFT, TOP, RIGHT, BOTTOM coords for each rectangle.
Re: Player-enemy collision (I'm seriously stumped! : update)
You might find this helpful to understand rectangle intersection: http://silentmatt.com/rectangle-intersection/
Try moving the rectangles around and see what happens!
Consider having a debugging mode which, when on, draws rectangles around hitboxes and maybe displays other information. To do this, set up a boolean variable in love.load...
You can then draw helpful things if debugging is true...
ship.lua
foe.lua
To quit the game when the player touches an enemy, try this:
Try moving the rectangles around and see what happens!
Consider having a debugging mode which, when on, draws rectangles around hitboxes and maybe displays other information. To do this, set up a boolean variable in love.load...
Code: Select all
function love.load()
debugging = true
ship.lua
Code: Select all
function player.draw()
love.graphics.setColor(255,230,255)
love.graphics.draw(fighter, player.x, player.y)
if debugging then
love.graphics.rectangle('line', player.x, player.y, player.width, player.height)
end
end
Code: Select all
function foe.draw()
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(enemies) do
love.graphics.draw (wolv, v.x, v.y)
if debugging then
love.graphics.rectangle('line', v.x, v.y, v.width, v.height)
end
end
end
- Loop through all the enemies
- Check for collisions between the player and the current enemy in the loop
Code: Select all
for _, enemy in ipairs(enemies) do
if CheckCollision(player.x, player.y, player.width, player.height, enemy.x, enemy.y, enemy.width, enemy.height) then
love.event.quit()
end
end
- GungnirDev
- Party member
- Posts: 119
- Joined: Thu Jun 28, 2012 10:31 pm
Re: Player-enemy collision (I'm seriously stumped! : update)
Ok Santos, your code worked which is pretty much just how I thought it would. I think the problem wasn't me not getting collision but rather me not looping through the right objects. Good concept, bad coding, I think. But thanks!
IDK about the respawning enemies though. I tried putting the update function in foe.update but the enemies won't spawn.
I figured that the problem might be that not all the enemies are getting removed, but I have the mark and remove function in both foe.update and ship.update so it should remove the enemies, right?
I cleaned up the code a little, and also I made music! In GarageBand. It's kinda fail but it's original and it plays OK. At least sound is easy to do in love...
IDK about the respawning enemies though. I tried putting the update function in foe.update but the enemies won't spawn.
I figured that the problem might be that not all the enemies are getting removed, but I have the mark and remove function in both foe.update and ship.update so it should remove the enemies, right?
I cleaned up the code a little, and also I made music! In GarageBand. It's kinda fail but it's original and it plays OK. At least sound is easy to do in love...
- Attachments
-
- shmup.love
- (2.07 MiB) Downloaded 76 times
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Bullets are the beauty and I don't know why
Re: Player-enemy collision (I'm seriously stumped! : update)
I like the music!
First of all, it would be really useful to know how many enemies there are, that way you can see if they are being removed or not:
Hmm, it seems like they're not. Next step: checking the code which removes enemies!
In LÖVE, a y coordinate of 0 is the top of the screen, so this is actually checking if the enemy is above the screen! Let's fix this:
Now we can see from the number of enemies that they are actually being removed. But no new enemies are being created...
The enemy table is being made... but it's not being inserted into the enemies table.
And it works!
First of all, it would be really useful to know how many enemies there are, that way you can see if they are being removed or not:
Code: Select all
function foe.draw()
love.graphics.print('#enemies: '..#enemies, 10, 10)
Code: Select all
-- mark enemies that are not visible for removal
if v.y < 0 then
table.insert(remEnemy, i)
end
Code: Select all
-- mark enemies that are not visible for removal
if v.y > love.graphics.getHeight() then
table.insert(remEnemy, i)
end
Code: Select all
if # enemies == 0 then
for i=0,4 do
enemy = {}
enemy.width = 40
enemy.height = 20
enemy.x = i * (enemy.width + 100) + 100
enemy.y = i * (enemy.height + 50) + 100
end
end
Code: Select all
if # enemies == 0 then
for i=0,4 do
enemy = {}
enemy.width = 40
enemy.height = 20
enemy.x = i * (enemy.width + 100) + 100
enemy.y = i * (enemy.height + 50) + 100
table.insert(enemies, enemy)
end
end
Last edited by Santos on Tue Aug 07, 2012 6:06 am, edited 1 time in total.
Who is online
Users browsing this forum: No registered users and 6 guests