[Solved] - Beginner - Collision detection with projectiles

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
Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

[Solved] - Beginner - Collision detection with projectiles

Post 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.
Attachments
testGame.love
(3.56 KiB) Downloaded 182 times
Last edited by Ximici on Sat Jun 29, 2013 11:45 am, edited 1 time in total.
IndieRetro
Citizen
Posts: 51
Joined: Mon Apr 15, 2013 8:21 pm
Contact:

Re: - Beginner - Collision detection with projectiles

Post 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
irc.freenode.org ##oodnet
http://exez.in/
Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

Re: - Beginner - Collision detection with projectiles

Post 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
IndieRetro
Citizen
Posts: 51
Joined: Mon Apr 15, 2013 8:21 pm
Contact:

Re: - Beginner - Collision detection with projectiles

Post 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.
irc.freenode.org ##oodnet
http://exez.in/
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: - Beginner - Collision detection with projectiles

Post 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.
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: - Beginner - Collision detection with projectiles

Post 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].
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests