line intersection with box

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
raymond
Prole
Posts: 3
Joined: Thu Jun 28, 2012 6:27 pm

line intersection with box

Post by raymond »

im trying to find out if a line goes over or is in a box and return false

this is a function i changed from c# not sure whats wrong with it but it returns false when the line originates under or to the left of the box

Code: Select all

function SegmentIntersectRectangle(a_rectangleMinX,a_rectangleMinY,a_rectangleMaxX,a_rectangleMaxY,a_p1x,a_p1y,a_p2x,a_p2y)
    -- Find min and max X for the segment

    local minX = a_p1x
    local maxX = a_p2x

    if a_p1x > a_p2x then
      minX = a_p2x
      maxX = a_p1x
	end

    -- Find the intersection of the segment's and rectangle's x-projections

    if maxX > a_rectangleMaxX then
      maxX = a_rectangleMaxX
    end

    if minX < a_rectangleMinX then
      minX = a_rectangleMinX
    end

    if minX > maxX then-- If their projections do not intersect return false
      return false
    end

    -- Find corresponding min and max Y for min and max X we found before

    local minY = a_p1y
    local maxY = a_p2y

    local dx = a_p2x - a_p1x

    if math.abs(dx) > 0.0000001 then
      a = (a_p2y - a_p1y) / dx
      b = a_p1y - a * a_p1x
      minY = a * minX + b
      maxY = a * maxX + b
    end

    if minY > maxY then
      local tmp = maxY
      maxY = minY
      minY = tmp
    end

    -- Find the intersection of the segment's and rectangle's y-projections

    if maxY > a_rectangleMaxY then
      maxY = a_rectangleMaxY
	end

    if minY < a_rectangleMinY then
      minY = a_rectangleMinY
    end

    if minY > maxY then -- If Y-projections do not intersect return false
      return false
    end

    return true
  end
could someone help me fix this or give me a better way of doing this thanks in advance

found the code here if that helps
http://stackoverflow.com/questions/9935 ... ange-in-2d
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: line intersection with box

Post by kikito »

Take a look at my snippet for Line-segment intersection. It's tested and works very well.
When I write def I mean function.
raymond
Prole
Posts: 3
Joined: Thu Jun 28, 2012 6:27 pm

Re: line intersection with box

Post by raymond »

thank you, lol i couldn't find that when i searched for it.

one thing though I only want it to return a boolean true if it intersects with the box false if it doesn't ive tried to convert it but im still having the same problem :death:

Code: Select all

 function boxSegmentIntersection(l,t,w,h, x1,y1,x2,y2)
  local dx, dy  = x2-x1, y2-y1

  local t0, t1  = 0, 1
  local p, q, r

  for side = 1,4 do
    if     side == 1 then p,q = -dx, x1 - l
    elseif side == 2 then p,q =  dx, l + w - x1
    elseif side == 3 then p,q = -dy, y1 - t
    else                  p,q =  dy, t + h - y1
    end

    if p == 0 then
      if q < 0 then return false end  -- Segment is parallel and outside the bbox
    else
      r = q / p
      if p < 0 then
        if     r > t1 then return false 
        elseif r > t0 then t0 = r
        end
      else -- p > 0
        if     r < t0 then return false 
        elseif r < t1 then t1 = r
        end
      end
    end
  end
  return true
end
pls tell me whats wrong here :) thank you for all the help
Attachments
game from the first level.love
heres the latest update i haven't fixed the problem but you can play the game and give me tool and level suggestions thank you :)
(328.22 KiB) Downloaded 100 times
my game.love
this is the game at the moment your not suppost to be able to draw lines in the purple boxs
the objective is to get the balls in the green box
change tools with 1,2&3
press space to let the balls drop/put the balls at they're origin
press r to reload level
move the camera with the arrowkeys
zoom in and out with the scroll wheel
(4.79 KiB) Downloaded 105 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 4 guests