VisibleEntityRadius()

Blitz3D Forums/Blitz3D Programming/VisibleEntityRadius()

Genexi2(Posted 2003) [#1]
Well, since I grew up with B2d, I was always able to tell exactly what the collidable objects looks like, but in B3d, they are but instead invisible sphere's in the spherical collision mode.

Since I liked knowing exactly when the objects would collide in B2d, I recently started work on my VisibleEntityRadius() function that'll throw in a visible collision sphere for your objects......

Problem is, I'm doin this at high school durin' lunch, so I only have access to the demo version, so I cant make it work with the ellipsoid stuff yet.....

Well, here's the code so far :

<See the updated form of the code below>


Only thing I'm unsure so far is the way I'm resizin' the visible radius, is it fine as it is, or is there something I should touch up on?

Also, on B3d v1.66 here, when I move my sphere directly right into the other sphere, then let go, and press either up or down, the sphere disappears into thin air leavin a NAN for its co-ords....anyone getting this in the latest version?

Other than that, I cant seem to find any other problems with it, and since it seems to work...I'm kinda proud of myself...I aint usually too good at doing these things on my own :-/


Mathieu A(Posted 2003) [#2]
It's seems very useful Thanks Genexi2


Todd(Posted 2003) [#3]
Hi,

I don't see the bug you're talking about here, I have v1.85 here. But about the way you're resizing the radius, there's one problem with it. That's that you're using an Integer instead of a Float. That means that you can't have a radius of 0.5, it'll be rounded up to 1. Just add a '#' to the 'radius' varible where you declare your function and that should fix it. Other than that it's a pretty useful function, thanks. Now you just need to do VisibleEntityBox :)


Genexi2(Posted 2003) [#4]
Todd, I already noticed the float thing there, already I've added alot more, and its more of a library now than a single function. :-/

Here's the example code :
;----------------------------------;
;VisibleRadius Library Function Ex.;
;----------------------------------;

Graphics3D 640,480,0,2


cam = CreateCamera()
light = CreateLight()

Global sphere2radius# = 4

CameraViewport cam, 0,0,640,480


; 1 of 2 little spheres to test the collisions on
sphere = CreateSphere()
EntityColor sphere, 255,0,0
EntityType sphere, 2 
UpdateNormals sphere
PositionEntity sphere, 5,0,10


; 2 of 2 little spheres to test the collisions on
sphere2 = CreateSphere()
EntityColor sphere2, 0,255,0
EntityType sphere2, 2 
UpdateNormals sphere2
PositionEntity sphere2, -5,0,10


; our type to hold the visible EntityRadius's
Type VisRad
	Field sphere, radius#
End Type	

;Create our visible EntityRadius's
VisRadius(sphere,  3)
VisRadius(sphere2, sphere2radius)

; Set the collisions to slide
Collisions 2,2,1,2

While Not KeyHit(1)
Flip
Cls


;Sphere2 and Camera movement lines
If KeyDown(205)
	TranslateEntity sphere2, 0.2,0,0
ElseIf KeyDown(203)
	TranslateEntity sphere2, -0.2,0,0
EndIf	

If KeyDown(200)
	TranslateEntity sphere2, 0,0.2,0
ElseIf KeyDown(208)
	TranslateEntity sphere2, 0,-0.2,0		
EndIf

If KeyDown(32)
	TranslateEntity cam, 0.2,0,0
ElseIf KeyDown(30)
	TranslateEntity cam, -0.2,0,0
EndIf	

If KeyDown(17)
	TranslateEntity cam, 0,0.2,0
ElseIf KeyDown(31)
	TranslateEntity cam, 0,-0.2,0		
EndIf

If KeyHit(16)
	HideAllVisRadius()
ElseIf KeyHit(18)
	ShowAllVisRadius()
EndIf	

If KeyDown(12)
	sphere2radius = sphere2radius - 0.05
	
	ChangeVisRadius(sphere, sphere2radius)
ElseIf KeyDown(13)
	sphere2radius = sphere2radius + 0.05
	
	ChangeVisRadius(sphere, sphere2radius)
EndIf 	
	
If KeyHit(2)
	HideVisRadius(sphere)
ElseIf KeyHit(3)
	ShowVisRadius(sphere)
EndIf 

If KeyHit(41)
	WireFrame (-WireFrame + 1)
EndIf	 			

UpdateWorld
RenderWorld

Text 0,0, "Press Up\Down\Left\Right Arrow Keys to move the green sphere"
Text 0,15, "Or WASD to move the camera"

Text 0,30, "Q to hide all, E to show all"
Text 300,60, "EntityRadius of Sphere2 (red sphere) : " +ReturnVisRadius(sphere2)

Wend
End

Include "visrad.bb"


And the library :
;-------------------------;
;  VisibleRadius Library  ;
;-------------------------;
;  Author : Genexi2       ;
;-------------------------;
;Use this however ya like,;
; no need to gimmy credit ;
;       either            ;
;-------------------------;


Function VisRadius#(theobject, radius#)
	EntityRadius theobject, radius

	v.visrad = New visrad

	v.visrad\sphere = CreateSphere(16,theobject)
	v.visrad\radius = radius
	EntityColor v.visrad\sphere , 190,190,255
	

	EntityAlpha v.visrad\sphere , 0.5
	ScaleEntity v.visrad\sphere, radius, radius, radius 
	UpdateNormals v.visrad\sphere
End Function


Function ShowVisRadius(theobject)
	For v.visrad = Each visrad
		If GetParent(v.visrad\sphere) = theobject
			ShowEntity v.visrad\sphere
		EndIf
	Next	  
End Function


Function HideVisRadius(theobject)
	For v.visrad = Each visrad
		If GetParent(v.visrad\sphere) = theobject
			HideEntity v.visrad\sphere
			Exit
		EndIf
	Next	
End Function


Function ShowAllVisRadius()
	For v.visrad= Each visrad
		ShowEntity v.visrad\sphere 
	Next	
End Function


Function HideAllVisRadius()
	For v.visrad= Each visrad
		HideEntity v.visrad\sphere 
	Next
End Function


Function ChangeVisRadius(theobject, radius#)
	For v.visrad = Each visrad
		If GetParent(v.visrad\sphere) = theobject
			EntityRadius theobject, radius
			
			ScaleEntity v.visrad\sphere, -v.visrad\radius, -v.visrad\radius, -v.visrad\radius
			ScaleEntity v.visrad\sphere, radius, radius, radius
			
			v.visrad\radius = radius
			Exit
		EndIf
	Next
End Function


Function ReturnVisRadius(theobject)
	For v.visrad = Each visrad
		If GetParent(v.visrad\sphere) = theobject
			Return v.visrad\radius
			Exit
		EndIf
	Next
End Function


Currently I'm seeming to have a problem with returning the correct size of the entity's radius using the "Return" command in my ReturnVisRadius() function.....


Todd(Posted 2003) [#5]
Looks good, but the problem with ReturnVisRadius() is that it's returning an Integer. Add the '#' sign to the end of the function, so it looks like this:
Function ReturnVisRadius#(theobject)...

You have to specify the type to return if it's not an integer, the same goes for strings.