Page 2 of 2
Re: Bullet collision??
Posted: Mon Mar 24, 2014 9:31 pm
by micha
If you just want to detect, if there is a hit, or not, you can use this code inside the box.lua.
Code: Select all
function update( dt )
local p1 = line.point1
local p2 = line.point2
hit = true
if p1.x < x and p2.x < x then
hit = false
return
end
if p1.x > x + w and p2.x > x + w then
hit = false
return
end
if p1.y < y and p2.y < y then
hit = false
return
end
if p1.y > y+h and p2.y > y+h then
hit = false
return
end
-- last case: first build normal vector
local nx,ny = p2.y-p1.y, -(p2.x-p1.x)
local vx,vy = x-p1.x, y-p1.y
local min = nx*vx+ny*vy
local max = min
vx,vy = x+w-p1.x, y-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
vx,vy = x+w-p1.x, y+h-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
vx,vy = x-p1.x, y+h-p1.y
min = math.min(min,nx*vx+ny*vy)
max = math.max(max,nx*vx+ny*vy)
if min*max>0 then
hit = false
end
end
It is based on the separating axis theorem. The code is not commented and it does not allow you to find the intersection point. It will only determine if it is a hit or not. If you want to understand this code, it is probably best to look into vector analysis. But if you only want to use it, feel free to use this code.
Re: Bullet collision??
Posted: Mon Mar 24, 2014 9:44 pm
by jag_e_nummer_ett
micha wrote:--snip--
Your code works as expected and will probably be used. I will though want to know how to effectively getting the intersection point for maybe a more precise project. But I think I will leave that to another day.
Re: Bullet collision??
Posted: Tue Mar 25, 2014 10:17 pm
by davisdude
I would show you the actual code, but it's really quite long and lengthy, so instead, I'll show you
how I've done it. Feel free to steal the code if you'd like (but credit wouldn't hurt). Like I said, lengthy and complicated. Lots of math stuff. Enjoy!
Re: Bullet collision??
Posted: Wed Mar 26, 2014 11:28 pm
by kikito
I must point out that this particular case is neatly solved without vectors with an algorithm called the
Liang-Barsky intersection.
Here's one implementation in Lua (warning, untested):
Code: Select all
local function getSegmentIntersectionIndices(l,t,w,h, x1,y1,x2,y2)
local t1, t2 = 0, 1
local dx, dy = x2-x1, y2-y1
local p, q, r
for side = 1,4 do
if side == 1 then p,q = -dx, x1 - l -- left
elseif side == 2 then p,q = dx, l + w - x1 -- right
elseif side == 3 then p,q = -dy, y1 - t -- top
else p,q = dy, t + h - y1 -- bottom
end
if p == 0 then
if q <= 0 then return nil end
else
r = q / p
if p < 0 then
if r > t2 then return nil
elseif r > t1 then t1 = r
end
else -- p > 0
if r < t1 then return nil
elseif r < t2 then t2 = r
end
end
end
end
local xi1, yi1 = x1 + dx*t1, y1 + dy*t1
local xi2, yi2 = x1 + dx*t2, y1 + dx*t2
return xi1, yi1, xi2, yi2
end
It will return nil if the segment does not intersect with the box, or the coordinates of the two points of intersection if the segment intersects with the box.
Re: Bullet collision??
Posted: Thu Mar 27, 2014 12:17 am
by davisdude
I took a look at the Wikipedia you linked, but I couldn't figure out what I was calling. Mind explaining?
Re: Bullet collision??
Posted: Thu Mar 27, 2014 9:28 am
by kikito
davisdude wrote:I took a look at the Wikipedia you linked, but I couldn't figure out what I was calling. Mind explaining?
Sure. The wikipedia article is a bit hard if you are not big on math.
- kinda-elvish.jpg (39 KiB) Viewed 2985 times
I actually got the initial code from
this other place, and then adapted it to Lua.
The parameters are:
- l,t : "left" and "top" corners of the box
- w,h: "width" and "height" of the box
- x1,y1: The start of the segment (the point "from which the bullet is shot")
- x2,y2: The end of the segment (the "bullet target")
Re: Bullet collision??
Posted: Thu Mar 27, 2014 10:04 am
by Azhukar
kikito wrote:The parameters are:
- l,t : "left" and "top" corners of the box
- w,h: "width" and "height" of the box
- x1,y1: The start of the segment (the point "from which the bullet is shot")
- x2,y2: The end of the segment (the "bullet target")
I suspect that doesn't work for rotated rectangles.
Re: Bullet collision??
Posted: Thu Mar 27, 2014 10:23 am
by jag_e_nummer_ett
Azhukar wrote:
I suspect that doesn't work for rotated rectangles.
Well I bet not concidering it doesn't got an angle as parameter
Re: Bullet collision??
Posted: Thu Mar 27, 2014 10:42 am
by kikito
Azhukar wrote:I suspect that doesn't work for rotated rectangles.
Well of course it doesn't. That's what AABB means - Axis-Aligned Bounding Box. If it's rotated, then it's not axis-aligned any more