Ray->Sphere intersection?

Blitz3D Forums/Blitz3D Programming/Ray->Sphere intersection?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hi Devils Child,
I've written a function to reflect a ray below. To use it you need to perform the following steps:-

1 - Create a Unit Vector called Ray from Line start x,y,z to sphere intersection point ix,iy,iz:-

    global Ray.vector=new vector
    Ray\x=ix-x : Ray\y=iy-y : Ray\z=iz-z
    UnitVector(Ray)


2 - Create a Normal Unit Vector from the sphere center sx,sy,sz to the intersection point ix,iy,iz:-

   global Normal.vector=new vector
   Normal\x=ix-sx : Normal\y=iy-sy : Normal\z=iz-sz
   UnitVector(Normal)


3 - Now you can present these to the function ReflectRay to get the new ray direction:-

  newRay.vector=ReflectRay(Normal,Ray)


Hope that helps!

Cheers,

Roy

Type vector
    Field x#,y#,z#
End Type

Function ReflectRay.vector(normal.vector,ray.vector)
	Local newray.vector=New vector
	Local normpiv%=CreatePivot()
	AlignToVector normpiv,normal\x,normal\y,normal\z,3
	TFormVector ray\x,ray\y,ray\z,0,normpiv
	ray\x=TFormedX()
	ray\y=TFormedY()
	ray\z=TFormedZ()
	TurnEntity normpiv,0,0,180
	TFormVector ray\x,ray\y,ray\z,normpiv,0
	newray\x=-TFormedX()
	newray\y=-TFormedY()
	newray\z=-TFormedZ()
	FreeEntity normpiv
	UnitVector(newray)
	Return newray
End Function

Function UnitVector(v.vector)
	Local l#
	l=Sqr(v\x*v\x+v\y*v\y+v\z*v\z)
	If l=0 Then l=1
	v\x=v\x/l
	v\y=v\y/l
	v\z=v\z/l
	If Abs(v\x)<0.000001 Then v\x=0
	If Abs(v\y)<0.000001 Then v\y=0
	If Abs(v\z)<0.000001 Then v\z=0
End Function