Bullet collision??

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
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Bullet collision??

Post 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.
User avatar
jag_e_nummer_ett
Citizen
Posts: 52
Joined: Thu May 16, 2013 6:31 pm
Location: Stockholm, Sweden

Re: Bullet collision??

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullet collision??

Post 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!
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
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Bullet collision??

Post 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.
Last edited by kikito on Thu Mar 27, 2014 2:22 pm, edited 1 time in total.
When I write def I mean function.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullet collision??

Post by davisdude »

I took a look at the Wikipedia you linked, but I couldn't figure out what I was calling. Mind explaining?
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
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Bullet collision??

Post 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
kinda-elvish.jpg (39 KiB) Viewed 2749 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")
When I write def I mean function.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Bullet collision??

Post 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.
User avatar
jag_e_nummer_ett
Citizen
Posts: 52
Joined: Thu May 16, 2013 6:31 pm
Location: Stockholm, Sweden

Re: Bullet collision??

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Bullet collision??

Post 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 :)
When I write def I mean function.
Post Reply

Who is online

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