Oh and here is another example of a basic intersection test. In this case we calculate the XYZ worldspace coordinates on a plane at which a parametric ray strikes it.
Remstart
PJY - code to calculate the point where a parametric ray intersects a plane
start_ray are the starting coords of the ray. Ray is then the vector describing the ray itself
result are the coords where the parametric ray will strike the plane
Remend
sync on
sync rate 40
autocam off
make object plain 1, 100, 100
set object light 1, 1
set object ambient 1, 1
d3d_init
type vec
x AS float
y AS float
z AS float
endtype
start_ray AS vec
ray AS vec
plane_normal AS vec
result AS vec
reset = 1
repeat
if reset = 1
repeat
start_ray.x = rnd(50)
start_ray.y = rnd(50)
start_ray.z = rnd(50)
ray.x = rnd(6) - 3
ray.y = -10
ray.z = rnd(6) - 3
plane_normal.x = rnd(10) - 5
plane_normal.y = rnd(10) - 5
plane_normal.z = rnd(10) - 5
null = make vector3(1)
set vector3 1, plane_normal.x, plane_normal.y, plane_normal.z
normalize vector3 1, 1
plane_normal.x = x vector3(1)
plane_normal.y = y vector3(1)
plane_normal.z = z vector3(1)
null = delete vector3(1)
temp = ray_plane_intersection(start_ray, ray, plane_normal)
position camera 0, result.x, result.y + 20, result.z - 100
point camera 0, result.x, result.y, result.z
reset = 0
until temp > 0
endif
if temp = 1 OR temp = 2
text 0, 0, "Result: " + str$(result.x) + " " + str$(result.y) + " " + str$(result.z)
text 0, 30, "Intersection? " + str$(temp)
d3d_dot3d start_ray.x, start_ray.y, start_ray.z, 5, rgb(255, 255, 255), 1
d3d_line3d start_ray.x, start_ray.y, start_ray.z, start_ray.x + ray.x, start_ray.y + ray.y, start_ray.z + ray.z, rgb(255, 0, 0), 1
position object 1, 0, 0, 0
point object 1, plane_normal.x, plane_normal.y, plane_normal.z
d3d_line3d 0, 0, 0, plane_normal.x * 5, plane_normal.y * 5, plane_normal.z * 5, rgb(0, 255, 0), 1
d3d_dot3d result.x, result.y, result.z, 5, rgb(0, 0, 255), 1
Rem PJY - if you uncomment the following line, the line will prove the accuracy of the calculation of the result
rem d3d_line3d result.x, result.y, result.z, start_ray.x, start_ray.y, start_ray.z, rgb(255, 0, 255), 1
control camera using arrowkeys 0, 2, 2
sync
endif
if spacekey() > 0
reset = 1
endif
until returnkey() > 0
flush video memory
end
Rem PJY - function to calculate the point on a plane at which a ray intersects
Rem PJY - it. This function assumes the plane_normal has indeed been normalized
Rem PJY - before it is passed into the function. Start_ray is the coords of
Rem PJY - the beginning of the ray. Ray is the vector describing the ray itself
function ray_plane_intersection(start_ray AS vec, ray AS vec, plane_normal AS vec)
null = make vector3(1)
null = make vector3(2)
set vector3 1, ray.x, ray.y, ray.z
set vector3 2, plane_normal.x, plane_normal.y, plane_normal.z
value# = dot product vector3(1, 2)
Rem PJY - if value# = 0 then ray is parallel to the plane and there is no
Rem PJY - intersection
if value# <> 0
Rem PJY - if value# > 0 then ray intersects with reverse of plane
if value# > 0
success = 1
endif
Rem PJY - if value# < 0 then ray intersects with front of plane
if value# < 0
success = 2
endif
set vector3 1, start_ray.x, start_ray.y, start_ray.z
angle# = dot product vector3(1, 2)
dist# = 0 - angle# / value#
set vector3 1, ray.x, ray.y, ray.z
scale vector3 1, 1, dist#
result.x = x vector3(1) + start_ray.x
result.y = y vector3(1) + start_ray.y
result.z = z vector3(1) + start_ray.z
else
success = 0
result.x = 0
result.y = 0
result.z = 0
endif
null = delete vector3(1)
null = delete vector3(2)
endfunction success
Cheer if you like bears! Cheer if you like jam sandwiches!
"I highly recommend Philip" (Philip)