Its obviously post geometrical tests night for yours truly tonight.
Here is my function which calculates the closest point on a sphere to another point in 3d space. The example I've posted shows this working in 2d.
Remstart
PJY - code to calculate the closest point on a sphere from another point which
I have called "ray"
Remend
sync on
sync rate 40
autocam off
type vec
x AS float
y AS float
z AS float
endtype
ray AS vec
circle_centre AS vec
result AS vec
repeat
ray.x = rnd(900)
ray.y = rnd(700)
ray.z = 0
circle_centre.x = 0
circle_centre.y = 0
circle_centre.z = 0
circle_radius = rnd(300) + 200
cls
temp = closest_point_on_sphere(ray, circle_centre, circle_radius)
if temp = 1
for i = 1 to 40
text 0, 0, "Result: " + str$(result.x) + " " + str$(result.y) + " " + str$(result.z)
text 0, 30, "Outside sphere? " + str$(temp)
rem scale object 2, circle_radius * 100, circle_radius * 100, circle_radius * 100
circle 0, 0, circle_radius
text ray.x, ray.y, "Ray"
text result.x, result.y, "Closest point"
sync
next i
endif
until returnkey() > 0
flush video memory
end
Rem PJY - function to calculate the closest point on a sphere to another point
Rem PJY - called ray. If the second point is within the radius of the sphere
Rem PJY - then the function returns 0, otherwise 1. The coordinates of the
Rem PJY - closest point on the sphere are saved in "result"
function closest_point_on_sphere(ray AS vec, circle_centre AS vec, circle_radius AS integer)
null = make vector3(1)
set vector3 1, circle_centre.x - ray.x, circle_centre.y - ray.y, circle_centre.z - ray.z
d_length# = length vector3(1)
Rem PJY - check to see if the ray is outside the sphere
if d_length# > circle_radius
success = 1
else
success = 0
endif
normalize vector3 1, 1
b_length# = d_length# - circle_radius
multiply vector3 1, b_length#
result.x = x vector3(1) + ray.x
result.y = y vector3(1) + ray.y
result.z = z vector3(1) + ray.z
null = delete vector3(1)
endfunction success
Cheer if you like bears! Cheer if you like jam sandwiches!
"I highly recommend Philip" (Philip)