Page 1 of 1

[Solved] - Beginner - Collision detection with projectiles

Posted: Fri Jun 28, 2013 1:05 pm
by Ximici
Hello,

I 'm learning the basics of making a game and I have a question: how can I detect the collision between a projectile (bullet) and a entity?
I 've written a piece of experimental code in "the bullet.lua" in the function "bulletIsColliding()" but it isn't working (to be honnest I think it's total nonsense what I wrote there).
Can someone please help me with finding a methode to detect collision between a projectile and a entity? I would really appreciate that.
I also want to know a good system for spawing entities because what I wrote was actually just the same as my "bullet.lua".

Thanks in advance,

Ximici

PS.: I don't like using libraries so I avoid them as much as I can.

Re: - Beginner - Collision detection with projectiles

Posted: Fri Jun 28, 2013 1:26 pm
by IndieRetro
Just use aabb collision, for instance inside of your entities.lua

Code: Select all

function mob:update(dt)

	for i,v in ipairs(mob) do
		v.x = v.x - mob.speed * dt
		for a,b in ipairs(bullet) do
			if bullet.x > v.x and bullet.y > v.y and 
				bullet.x < v.x+4 and bullet.y < v.y+4 then
				table.remove(mob, i);
			end
		end
	end

end
That is probably a horrible bounding box but, you get the idea :p

Re: - Beginner - Collision detection with projectiles

Posted: Fri Jun 28, 2013 2:01 pm
by Ximici
Hey,

Thanks for replying! But if I try the code, it gives an error "Trying to compare number with nil" when I shoot. At first I thought it was because you wrote:
"bullet.x > v.x" but if I change bullet.x to b.x (because of the ipairs) it just does nothing, why is that?

Ximici

Re: - Beginner - Collision detection with projectiles

Posted: Fri Jun 28, 2013 2:05 pm
by IndieRetro
Haha yeah it should be b.x, my bad, also the bounding box I made was pretty bade, you will need to tweak the positions to make it work better, at the moment it is only checking a small 4x4 pixel box.

EDIT:
Something more like this

Code: Select all

function mob:update(dt)

   for i,v in ipairs(mob) do
      v.x = v.x - mob.speed * dt
      for a,b in ipairs(bullet) do
         if b.x < v.x+32 and b.y < v.y+32 and
            b.x > v.x-8 and b.y > v.y-8 then
            table.remove(mob, i);
         end
      end
   end

end
So we check b.x(the top left corner of the bullet) if it is smaller than the entities v.x+32(so, the right corner of the entity) etc, its somewhat hard to explain through text though, also I am tired haha.

Re: - Beginner - Collision detection with projectiles

Posted: Sat Jun 29, 2013 3:08 am
by severedskullz
Not trying to threadjack or anything but it seems on topic... Are there any resources available for hitscan projectiles, or how to implement them? Ive been looking but none of the articles I found were based in 2D or they relied on some engine feature to do the work for them.

Re: - Beginner - Collision detection with projectiles

Posted: Sat Jun 29, 2013 3:24 am
by slime
severedskullz wrote:Not trying to threadjack or anything but it seems on topic... Are there any resources available for hitscan projectiles, or how to implement them? Ive been looking but none of the articles I found were based in 2D or they relied on some engine feature to do the work for them.
You'd probably want to implement some form of raycasting for your particular collision detection implementation and use that - I don't know about how that would be done though, it probably depends highly on the sort of collision detection you're using.

If you use love.physics / box2d, you can use [wiki]World:rayCast[/wiki].