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.
[Solved] - Beginner - Collision detection with projectiles
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] - Beginner - Collision detection with projectiles
- Attachments
-
- testGame.love
- (3.56 KiB) Downloaded 189 times
Last edited by Ximici on Sat Jun 29, 2013 11:45 am, edited 1 time in total.
-
- Citizen
- Posts: 51
- Joined: Mon Apr 15, 2013 8:21 pm
- Contact:
Re: - Beginner - Collision detection with projectiles
Just use aabb collision, for instance inside of your entities.lua
That is probably a horrible bounding box but, you get the idea :p
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
irc.freenode.org ##oodnet
http://exez.in/
http://exez.in/
Re: - Beginner - Collision detection with projectiles
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
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
-
- Citizen
- Posts: 51
- Joined: Mon Apr 15, 2013 8:21 pm
- Contact:
Re: - Beginner - Collision detection with projectiles
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
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.
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
irc.freenode.org ##oodnet
http://exez.in/
http://exez.in/
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Re: - Beginner - Collision detection with projectiles
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.
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: - Beginner - Collision detection with projectiles
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.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.
If you use love.physics / box2d, you can use [wiki]World:rayCast[/wiki].
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 19 guests