I accidentally switched t1 and t2.
Here's the unoptimised code (don't use this, this is only so you understand the math):
function intersect2D(x1#, y1#, x2#, y2#, angle1#, angle2#)
rem build matrix from directional vectors for simultaneous linear equation solving
D as Mat2x2
d2 as Vec2
d2.x# = cos(angle2#)
d2.y# = sin(angle2#)
D.m11# = d2.x#
D.m12# = -cos(angle1#)
D.m21# = d2.y#
D.m22# = -sin(angle1#)
rem if matrix is singular (i.e. determinant is zero), no solution exists
det# = D.m11#*D.m22# - D.m12#*D.m21#
if det# = 0.0 then exitfunction 0
rem compute inverse of matrix
temp# = D.m11#
D.m11# = D.m22#/det#
D.m22# = temp#/det#
D.m12# = -D.m12#/det#
D.m21# = -D.m21#/det#
rem Multiply inverse of matrix with difference of the input position vectors
rem The result is t1 and t2.
P as Vec2
P.x# = x1#-x2#
P.y# = y1#-y2#
t2# = D.m11#*P.x# + D.m12#*P.y#
t1# = D.m21#*P.x# + D.m22#*P.y#
rem plug t2 back into original equation to find intersection position
ret.x# = x2# + t2#*d2.x#
ret.y# = y2# + t2#*d2.y#
endfunction 1
Since we only need t2, there's some stuff we can omit to make it faster:
function intersect2D(x1#, y1#, x2#, y2#, angle1#, angle2#)
rem build matrix from directional vectors for simultaneous linear equation solving
D as Mat2x2
d2 as Vec2
d2.x# = cos(angle2#)
d2.y# = sin(angle2#)
D.m11# = d2.x#
D.m12# = -cos(angle1#)
D.m21# = d2.y#
D.m22# = -sin(angle1#)
rem if matrix is singular (i.e. determinant is zero), no solution exists
det# = D.m11#*D.m22# - D.m12#*D.m21#
if det# = 0.0 then exitfunction 0
rem compute inverse of matrix (only necessary fields are computed for speed)
D.m11# = D.m22#/det#
D.m12# = -D.m12#/det#
rem Multiply inverse of matrix with difference of the input position vectors
rem The result is t1 and t2. Since we only need one of the t's, computing t1
rem is omitted.
P as Vec2
P.x# = x1#-x2#
P.y# = y1#-y2#
t2# = D.m11#*P.x# + D.m12#*P.y#
rem plug t2 back into original equation to find intersection position
ret.x# = x2# + t2#*d2.x#
ret.y# = y2# + t2#*d2.y#
endfunction 1
Example:
`%Project Title%
`2d_intersection.dba
`======================
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
show mouse
rem required for 2D return values
global ret as Vec2
rem position and direction of two entities
x1 = 200
y1 = 200
a1 = 76
x2 = 400
y2 = 500
a2 = 170
rem main loop
do
rem user info
set cursor 0, 0
rem draw entities
circle x1, y1, 10
line x1, y1, cos(a1)*20+x1, sin(a1)*20+y1
circle x2, y2, 10
line x2, y2, cos(a2)*20+x2, sin(a2)*20+y2
if intersect2D(x1, y1, x2, y2, a1, a2)
center text ret.x#, ret.y#+20, "intersection"
circle ret.x#, ret.y#, 10
else
print "no intersection"
endif
rem refresh screen
sync
rem end of main loop
loop
type Vec2
x# as float
y# as float
endtype
type Mat2x2
m11# as float
m12# as float
m21# as float
m22# as float
endtype
function intersect2D(x1#, y1#, x2#, y2#, angle1#, angle2#)
rem build matrix from directional vectors for simultaneous linear equation solving
D as Mat2x2
d2 as Vec2
d2.x# = cos(angle2#)
d2.y# = sin(angle2#)
D.m11# = d2.x#
D.m12# = -cos(angle1#)
D.m21# = d2.y#
D.m22# = -sin(angle1#)
rem if matrix is singular (i.e. determinant is zero), no solution exists
det# = D.m11#*D.m22# - D.m12#*D.m21#
if det# = 0.0 then exitfunction 0
rem compute inverse of matrix (only necessary fields are computed for speed)
D.m11# = D.m22#/det#
D.m12# = -D.m12#/det#
rem Multiply inverse of matrix with difference of the input position vectors
rem The result is t1 and t2. Since we only need one of the t's, computing t1
rem is omitted.
P as Vec2
P.x# = x1#-x2#
P.y# = y1#-y2#
t2# = D.m11#*P.x# + D.m12#*P.y#
rem plug t2 back into original equation to find intersection position
ret.x# = x2# + t2#*d2.x#
ret.y# = y2# + t2#*d2.y#
endfunction 1