slopes and collisions

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
User avatar
azulfato
Prole
Posts: 6
Joined: Sun Nov 13, 2016 4:01 pm

slopes and collisions

Post by azulfato »

trying slopes for the 1st time and came up with this for point vs line collision
Image

Code: Select all

local function get_center(ax,ay, bx,by)
  local cx = ax + (bx - ax)/2
  local cy = ay>by and (by + (ay - by)/2) or (ay + (by - ay)/2)
  return floor(cx), floor(cy)
end
local function do_stuff( ox, oy, ax, ay, bx, by) -- 'o' = point / 'a' and 'b' = line ends

  local cx,cy = get_center( ax,ay, bx,by)
  if cx == ox and cy <= oy then
    return vec(cx,cy)
  end

  local up = ay>by --did you know that IRL the number of slopes going down is = the number of slopes going up?
  local u,s
  local sx,sy,ex,ey

  if up then
    if oy>=cy and ox>=cx then return vec(cx,cy) end
    if (oy<cy and ox<=cx) or (oy==cy and ox<cx) then return false end
    u = oy<cy and ox>cx --ac/d
    s = oy>cy and ox<cx --ab/e
    if u then
      sx, sy = cx, cy
      ex, ey = bx, by
    elseif s then
      sx, sy = ax, ay
      ex, ey = cx, cy
    end
  else
    if oy>=cy and ox<=cx then return vec(cx,cy) end
    if (oy<cy and ox>=cx) or (oy==cy and ox>cx) then return false end
    u = oy<cy and ox<cx --ac/e
    s = oy>cy and ox>cx --ab/d
    if u then
      sx, sy = ax, ay
      ex, ey = cx, cy
    elseif s then
      sx, sy = cx, cy
      ex, ey = bx, by
    end
  end
  return do_stuff( ox, oy, sx, sy, ex, ey) --keep doing it until its on or off
end
Image
is it ok if it works?
do you know a better way?
inb4 google it
i just want to know how you guys handle this.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: slopes and collisions

Post by ivan »

You can't reliably do point vs line collisions because in mathematics: points are infinitely small and lines are infinitely thin. So you just can't do that kind of math using floating point precision. You can check if a point is on a particular "side" of the line but that's about it. There are also established algorithms for finding the closest point on a line/distance from a line. Alternatively you can treat your lines as capsules with a specific thickness.
ghurk
Prole
Posts: 15
Joined: Tue Apr 25, 2017 11:05 pm

Re: slopes and collisions

Post by ghurk »

Im usually using this collision check based on some math article which link i dont have anymore.

Code: Select all

px, py = x, y --point coordinates
rec = { {x,y}; {x,y}; {x,y}; {x,y} }; --rectangle

function checkA ( x0, y0, x1, y1, x2, y2 ) --check point against line
	if ( x1*y2 - y1*x2 - x0*y2 + y0*x2 + x0*y1 - y0*x1 ) < 0 then return false end
end

function checkB( px, py, rec ) --check point against rectangle.
	if checkA( px, py, rec[1][1], rec[1][2], rec[2][1], rec[2][2] ) == false then return end
	if checkA( px, py, rec[2][1], rec[2][2], rec[3][1], rec[3][2] ) == false then return end
	if checkA( px, py, rec[3][1], rec[3][2], rec[4][1], rec[4][2] ) == false then return end
	if checkA( px, py, rec[4][1], rec[4][2], rec[1][1], rec[1][2] ) == false then return end
	return true
end

if checkB == true then
	--collision is true, perform action or anything else
end
function checkA looks if point is "on the right side of line". currently for clockwise points, for counter-clockwise set points of shape it is (if i remember well, didnt use that order in a long time):

Code: Select all

if ( x1*y2 - y1*x2 - x0*y2 + y0*x2 + x0*y1 - y0*x1 ) > 0 then return false end -- ">" is the difference
function checkB looks if the point is on the "right side" of all lines wrom which shape consists. Can be used on any closed shape with all points set in same order, if no line of the shape intersect other line of the shape.

sorry, now noticed the "point vs line collision". been looking on the video for too long i guess. Looking at your code, checking if point x is in range of one slope segment visible in video, sombined with one check if point is under the slope would result in the slope being marked like it does when u touch it in the video. 4 checks for any rectangle anywhere on screen.

for point_vs_line collision im using code from article posted above, which finds nearest point on line (usually check all lines in range), then check if result is in set range from point. thats how it usually works in graphic applications. to evade marking multiple points in area, usually something like the shape_based check binded to second mouse button/shift+mouse button, which enables you to mark single shape and then further check point_vs_line collisions only on that shape/object.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], DOMINOSRULZ and 76 guests