listing nearest entities to the camera

Blitz3D Forums/Blitz3D Beginners Area/listing nearest entities to the camera

stayne(Posted 2008) [#1]
I have 100 entities and I want to display a list ('Text' is fine for now) of the top 10 entities closest to the camera. My entities are contained in a Type and have an 'ID' field that is set during creation (For i = 1 to 100, entity\ID = i, etc). Not sure if that is the most efficient way to sort through them... but regardless I can't get my head around this. The list needs to be updated according to where the camera is at all times.


Warner(Posted 2008) [#2]
I would create a type to sort the distances. This type should be able to hold a distance, and some reference to the entity.

Each frame update/main loop, clear the list that contains the distances.
Loop through all entities, and add their distance to the list.

But before adding a new distance to the list, first check at what point in the list it should be inserted. Loop through the list. If one of the distances in the list is bigger than the new one, insert the new one right before the one in the list, and leave the loop.

If the list contains no distances, or all distances in the list are smaller than the new distance, the new distance should be placed at the end of the list.


stayne(Posted 2008) [#3]
Thanks Warner! This works but I am having trouble listing each entity on a new line... the text is all on the same line on the Y axis.


Pirate(Posted 2008) [#4]
Info$="This door is locked"+Chr(13) ;The text message
Info$=Info$+"You need the Green Key" ;Second line of text

using the +Chr(13) will make another line of text...hope this helps...


Warner(Posted 2008) [#5]
Well, I think Chr$(13) would only work with text files. It could also be Chr$(13)+Chr$(10) or even Chr$(0), can't exactly remember. I usually try them all out, and then forget again. Maybe I don't understand the problem correctly, but you are printing everything to the screen after the sorting is completed, right ? And -just to eliminate that option- you are using the 'y' parameter of Text ?


stayne(Posted 2008) [#6]
Here is my code. What a horrible mess eh? Thanks everyone for the help.


Type listitem
    Field name$
    Field dist#
    Field id
End Type

Type enemy
    Field mesh
    Field name$
    Field id
End Type

For i = 1 To 50
    e.enemy = New enemy
    e\mesh = CreateCube()
    e\id = i
    PositionEntity e\mesh,Rnd(-5000,5000),Rnd(-100,100),Rnd(-5000,5000)
    e\name = "Enemy"
Next

Repeat
        ......

	For e.enemy = each enemy
		If EntityDistance(camera, e\mesh) < 1000
			this.listitem = New listitem
			this\name$ = e\name
			this\dist = EntityDistance (camera,e\mesh)
			this\id = e\id ;
			addtolist(this.listitem)
			Delete this
		EndIf
	Next

	Flip

Until KeyHit (1)

End

Function addtolist(this.listitem)
	
	Text 10,10,this\name+" - "+this\id+" - "+this\dist+Chr$(13)
	;Text3D(font2,-500,0,this\name+" - "+this\id+" - "+this\dist,0)
	
End Function




Warner(Posted 2008) [#7]
Here is the code I wrote to try things out, hopefully it helps:



stayne(Posted 2008) [#8]
Warner it works flawlessly and taught me a great deal. Thank you so much for taking the time to suss that out.