[Not Solved] Bullets Collide With Entities?

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.
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

[Not Solved] Bullets Collide With Entities?

Post by Deltise »

Currently I'm using randomly generate entities (that are made using an object system) and the player being able to fire bullets using WASD and move with the arrow keys. Within the zombie's AI code, I have it searching collisions by comparing it's own position, with the position of a bullet in the bullet table.

I want the entities to basically take damage from the bullets fired at them by the player but...

(This is in my Entities.lua under each of the enemies' update/AI sections)

Code: Select all

brgx = self.x
brgy = self.y

for i, v in ipairs(bullet) do
bvx = v.x 
bvy = v.y
end

if brgx == bvx and brgy == bvy and self.hitready == true then
self.health = self.health - player.damage
end


if self.health >= 0 then
	for i, v in ipairs(bullet) do
	table.remove(enemies, i)
	end
end
...what happens when I test the game out is that as soon as I fire, all of the entities on the field die without even touching the bullet.
Everything else in the game works fine such as player collision with entity and so on.

Thanks for your help in advance :awesome:
Attachments
Game.zip
(210.97 KiB) Downloaded 184 times
Last edited by Deltise on Thu Jun 13, 2013 11:48 pm, edited 1 time in total.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Bullets Collide With Entities?

Post by Plu »

Code: Select all

if self.health >= 0 then
   for i, v in ipairs(bullet) do
   table.remove(enemies, i)
   end
end
This reads "if my health is more than 0, roll through each entry in bullets and remove the corresponding enemy". That can't be right, can it?
User avatar
NightKawata
Party member
Posts: 294
Joined: Tue Jan 01, 2013 9:18 pm
Location: Cyberspace, Room 6502
Contact:

Re: Bullets Collide With Entities?

Post by NightKawata »

This should work: (I forgot to respond earlier)

Code: Select all

if self.health <= 0 then
   for i, v in pairs(enemies) do
     if v == self then
       table.remove(enemies, i)
     end
   end
end
Now let's analyze this:

Code: Select all

if self.health >= 0 then
   for i, v in ipairs(bullet) do
   table.remove(enemies, i)
   end
end
First issue, it checks to see if the health is greater than or equals zero. I'm sure you don't want that.
Second issue, I'm assuming you're killing the enemy here, so why are you iterating through the bullet table?
Next up, that iterator will delete every enemy for as many indexes exist in the bullets, which'll pretty much kill everything. (I'm not 100% sure on that though.)
"I view Python for game usage about the same as going fishing with a stick of dynamite. It will do the job but it's big, noisy, you'll probably get soaking wet and you've still got to get the damn fish out of the water." -taylor
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Bullets Collide With Entities?

Post by MadByte »

There seems to be much more problems then the "if self.health >= 0 then" loop.
Sadly I'm not really familiar with this kind of object creation. :(
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

Re: Bullets Collide With Entities?

Post by Deltise »

NightKawata wrote:This should work: (I forgot to respond earlier)

Code: Select all

if self.health <= 0 then
   for i, v in pairs(enemies) do
     if v == self then
       table.remove(enemies, i)
     end
   end
end
Now let's analyze this:

Code: Select all

if self.health >= 0 then
   for i, v in ipairs(bullet) do
   table.remove(enemies, i)
   end
end
First issue, it checks to see if the health is greater than or equals zero. I'm sure you don't want that.
Second issue, I'm assuming you're killing the enemy here, so why are you iterating through the bullet table?
Next up, that iterator will delete every enemy for as many indexes exist in the bullets, which'll pretty much kill everything. (I'm not 100% sure on that though.)
Ah, my mistake, thank you,
How would you approach the bullet collision thing though, where an entity will die even though it hasn't hit the bullet?

EDIT: With this bit of code, the entities don't die at all, probably because my collide condition only registers the top left corner of the bullet, but how would I go about calculating any form of collision between the entity and the bullet?
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

Re: Bullets Collide With Entities?

Post by Deltise »

Plu wrote:

Code: Select all

if self.health >= 0 then
   for i, v in ipairs(bullet) do
   table.remove(enemies, i)
   end
end
This reads "if my health is more than 0, roll through each entry in bullets and remove the corresponding enemy". That can't be right, can it?
Yeah, caught that after looking at my code, I was being silly and not paying attention. But I still don't know how to go about creating an if statement that checks if a bullet collided with an entity
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets Collide With Entities?

Post by davisdude »

You can do 1 of 3 things (from easiest to hardest):
1) Make the bullet have a rectangular hit-box.
2) Make the enemies have circular hit-boxes (not recommended).
3) Look at the post I had a couple months ago. It has nice code for circle-rectangle collision (which is what you want).
http://www.love2d.org/forums/viewtopic.php?f=4&t=33493
This is a link to the thread.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

Re: Bullets Collide With Entities?

Post by Deltise »

davisdude wrote:You can do 1 of 3 things (from easiest to hardest):
1) Make the bullet have a rectangular hit-box.
2) Make the enemies have circular hit-boxes (not recommended).
3) Look at the post I had a couple months ago. It has nice code for circle-rectangle collision (which is what you want).
http://www.love2d.org/forums/viewtopic.php?f=4&t=33493
This is a link to the thread.
What if their both rectangles :T
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets Collide With Entities?

Post by davisdude »

You can do something like this:

Code: Select all

for i = #bullets, 1, -1 do --w and h are width and height, respectivey
  for i = for b = #bullet, 1, -1 do
		for e = #enemy, 1, -1 do	
			if b.x + b.w > e.x and
                        b.x < e.x + e.w and
                        b.y + b.h > e.y and
                        b.y < e.y + e.h then
				print("hit!")
				table.remove(bullet, b)
				table.remove(enemy, e)
			end
		end
	end
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

Re: Bullets Collide With Entities?

Post by Deltise »

davisdude wrote:You can do something like this:

Code: Select all

for i = #bullets, 1, -1 do --w and h are width and height, respectivey
  for i = for b = #bullet, 1, -1 do
		for e = #enemy, 1, -1 do	
			if b.x + b.w > e.x and
                        b.x < e.x + e.w and
                        b.y + b.h > e.y and
                        b.y < e.y + e.h then
				print("hit!")
				table.remove(bullet, b)
				table.remove(enemy, e)
			end
		end
	end
end
Where exactly would I throw this into? My bulletUpdate?
If so, I get an error at the start saying that it cannot find the height of the bullet, because no bullets are spawned yet at the start of the game
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests