Is this useful for anything?

Blitz3D Forums/Blitz3D Programming/Is this useful for anything?

Came across this old demo on my HD. It basically finds where a line intersects with a box. The box can have any rotation/size and doesn't need to be axis-aligned. It doesn't use LinePick and the distance between object and target doesn't affect speed - I don't think so, anyway. :P

	Graphics3D 800,600,32
	SetBuffer BackBuffer()

	WireFrame 0
	AntiAlias 0

	SeedRnd MilliSecs()

	Global frame_count%
	Global fps%
	Global slowest_fps%
	Global fps_timeout%
	Global frame_time%
	Global slowest_frame%
	Global frame_start%
	fps_timer = CreateTimer(60)
	slowmo% = False
	wiref% = False
	
	piv = CreatePivot()
	cam = CreateCamera(piv)
	PositionEntity cam,0,0,-8
	TurnEntity piv,0,45,0
	light = CreateLight()
	
	cube = CreateCube()
	EntityAlpha cube,.9
	;PositionEntity cube,-.5,-2,3
	xsize# = .5
	ysize# = 1.23
	zsize# = 1.5
	ScaleMesh cube,xsize,ysize,zsize
		
	piv2 = CreatePivot()
	enemy = CreateCone(32,1,piv2)
	RotateMesh enemy,90,0,0
	UpdateNormals enemy
	ScaleEntity enemy,.3,.3,.3
	PositionEntity enemy,1,2,-3
	EntityColor enemy,255,255,0
	
	hit = CreateSphere()
	ScaleMesh hit,.12,.12,.12
	EntityColor hit,0,255,0
	PositionEntity hit,0,0,0

	p#=-.1
	y#=-.5
	r#=1
	p2#=0
	y2#=-.5
	r2#=-1
	timer = MilliSecs()

	num_spots=100
	s=CreateSphere(3,cube)
	ScaleMesh s,.1,.1,.1
	EntityColor s,255,0,255
	HideEntity s
	Dim spots(num_spots)
	For n=1 To num_spots
		spots(n)=CopyEntity(s,cube)
		PositionEntity spots(n),0,10000,0
	Next
	free_spot=1
	
	; Main loop
	While Not KeyHit(1)
		frame_start = MilliSecs()
	
		If KeyHit(28) Then slowmo = Not slowmo
		If KeyHit(14)
			wiref = Not wiref
			WireFrame wiref
		EndIf

		PointEntity enemy,cube
		TurnEntity piv2,p2,y2,r2
		If MilliSecs()-timer>=3000
			p=Rnd(-1,1)
			y=Rnd(-1,1)
			r=Rnd(-1,1)
			p2=Rnd(-1,1)
			y2=Rnd(-1,1)
			r2=Rnd(-1,1)
			timer=MilliSecs()
		EndIf
		TurnEntity cube,p,y,r

		l3d = create_3D_line(0,EntityX(cube,1),EntityY(cube,1),EntityZ(cube,1),EntityX(enemy,1),EntityY(enemy,1),EntityZ(enemy,1),255,0,0)
		
		TFormPoint EntityX(enemy,1),EntityY(enemy,1),EntityZ(enemy,1),0,cube
		dx# = TFormedX()
		dy# = TFormedY()
		dz# = TFormedZ()
		rat1# = xsize/Abs(dx)
		rat2# = ysize/Abs(dy)
		rat3# = zsize/Abs(dz)
		rat# = lowest_val(rat1,rat2,rat3)
		nx# = rat * dx
		ny# = rat * dy
		nz# = rat * dz
		TFormPoint nx,ny,nz,cube,0
		PositionEntity hit,TFormedX(),TFormedY(),TFormedZ(),1
		
		PositionEntity spots(free_spot),nx,ny,nz
		free_spot=free_spot+1
		If free_spot>num_spots Then free_spot=1

				

		CameraClsMode cam,1,1
		HideEntity cube
		HideEntity hit
		HideEntity enemy
		ShowEntity l3d
		WireFrame 1
		RenderWorld

		CameraClsMode cam,0,0
		ShowEntity cube
		ShowEntity hit
		ShowEntity enemy
		HideEntity l3d
		WireFrame 0
		RenderWorld
		
		
		frame_time = MilliSecs() - frame_start	
		show_info()

		WaitTimer(fps_timer)
		Flip(1)
		FreeEntity l3d
		If slowmo Then Delay 200
	Wend

	ClearWorld	

	End

Function lowest_val#(v1#,v2#,v3#)

	If v1<v2 And v1<v3
		Return v1
	ElseIf v2<v3
		Return v2
	Else
		Return v3
	EndIf

End Function

;
; Display debug info
;
Function show_info()
	
	If fps_timeout
		frame_count = frame_count + 1

		If MilliSecs() > fps_timeout Then
			fps_timeout = MilliSecs() + 1000 
			fps = frame_count 
			frame_count = 0 
		
			If fps < slowest_fps Or slowest_fps = 0 Then slowest_fps = fps
		EndIf 
		
		If frame_time > slowest_frame Then slowest_frame = frame_time
		
		Color 0,255,0
		Text 10,10," Triangles: " + TrisRendered()
		Color 255,255,0
		Text 10,25," Millisecs: " + frame_time
		Text 10,40,"   Slowest: " + slowest_frame
		Color 0,255,255
		Text 10,55,"       FPS: " + fps
		Text 10,70,"     Worst: " + slowest_fps
		Color 255,255,255
	Else
		; First call initialization.
		fps_timeout = MilliSecs() + 1000 
	EndIf
	
End Function
	
	
Function create_3D_line(mesh,x0#,y0#,z0#,x1#,y1#,z1#,r%=255,g%=255,b%=255) 

	If mesh = 0 
		mesh = CreateMesh() 
		surf = CreateSurface(mesh) 
		EntityFX mesh,1+2+16
	Else 
		last_surf = CountSurfaces(mesh)
		surf = GetSurface(mesh,last_surf)
		If CountVertices(surf) > 30000 Then surf = CreateSurface(mesh)
	End If 

	v0 = AddVertex(surf,x0,y0,z0) 
	v1 = AddVertex(surf,x1,y1,z1)  
	v2 = AddVertex(surf,x0,y0,z0)  
	AddTriangle surf,v0,v1,v2
	
	VertexColor surf,v0,r,g,b
	VertexColor surf,v1,r,g,b
	VertexColor surf,v2,r,g,b

	Return mesh 

End Function


I just developed this from a 2D version but didn't have any real use for it. Does it even have any use? :)

I'm sure it's useful, you should add it to the code archives, algorithm section.

Hmmm, maybe - I just can't believe that anything I've done that's maths-based is useful and not already common knowledge. :P

..I needed this functionality by last month damnit :P Neat :)

Really? What for? lol. :P

The car racer- to add car-car collision sounds without having to "meshesintersect".
It's pretty much complete, and has all the sounds already, but meshesintersect is slow, so i wanted to update it.
The above would let me position a sound too.
[edit]

I would have used box collision, but it didnt work for some reason- can't remember what though now.