picked to type

Blitz3D Forums/Blitz3D Beginners Area/picked to type

mindstorms(Posted 2007) [#1]
I have a single surface terrain system, if you will. Basically this consists of walls that morph during gameplay. Each section has a type that links to the triangles (only two each) in the mesh. After using camerapick and pickedtriangle, is there a faster way to determine based from the picked triangle what wall section is "clicked" other than looping through all the sections and comparing the two indexes?

Basically, is there a faster way to find the type associated with a picked triangle?


mindstorms(Posted 2007) [#2]
perhaps some more info would be more helpful...

Here is a test function that I am using to create the "walls" (there are many types, this is the basic block, without any special things)

Function add_wall(x,y,z,mesh)
	Local mesh1 = CreateMesh()
	Local surf1 = CreateSurface(mesh1)
	Local vbtr = AddVertex(surf1,x+5,y-7.5,z+5)
	Local vbtl = AddVertex(surf1,x-5,y-7.5,z+5)
	Local vbbr = AddVertex(surf1,x+5,y-7.5,z-5)
	Local vbbl = AddVertex(surf1,x-5,y-7.5,z-5)
	Local vttl = AddVertex(surf1,x-5,y+7.5,z+5)
	Local vttr = AddVertex(surf1,x+5,y+7.5,z+5)
	Local vtbl = AddVertex(surf1,x-5,y+7.5,z-5)
	Local vtbr = AddVertex(surf1,x+5,y+7.5,z-5)
	
	AddTriangle(surf1,vbbl,vbtr,vbtl)	;bottom
	AddTriangle(surf1,vbtr,vbbl,vbbr)	;bottom
	AddTriangle(surf1,vbtl,vttl,vbbl)	;left
	AddTriangle(surf1,vtbl,vbbl,vttl)	;left
	AddTriangle(surf1,vtbl,vtbr,vbbr)	;front
	AddTriangle(surf1,vbbr,vbbl,vtbl)	;front
	AddTriangle(surf1,vbbr,vtbr,vttr)	;right
	AddTriangle(surf1,vttr,vbtr,vbbr)	;right
	AddTriangle(surf1,vttr,vttl,vbtl)	;back
	AddTriangle(surf1,vbtl,vbtr,vttr)	;back
	AddTriangle(surf1,vttl,vttr,vtbr)	;top
	AddTriangle(surf1,vtbr,vtbl,vttl)	;top
	
	AddMesh mesh1,mesh
End Function


Not shown, but I will assign from a mesh two of the triangles to be "clickable", and store their address in an array of types representing their position. I have two ways of implementing finding which triangle was clicked: One would be two create everything in order, following the array, and then take whatever the index returned from a pick and divide by two (round down), then convert the one dimensional array info into my two dimensional array, to find the type that it is in. Whenever a block is created or destroyed I would have to delete everything and re-create the mesh all over again. The other method would be to loop through the array and check every triangle against the picked one and find a match.

I hope some of this made sense...


octothorpe(Posted 2007) [#3]
Sounds simple: create an index of triangle numbers to wall object pointers.

Dim tri_to_wall.wall(MAX_TRIS)

; add triangles for wall object
tri_index = AddTriangle(...)
tri_to_wall(tri_index) = the_wall

; find picked wall
local the_wall.wall = tri_to_wall(PickedTriangle(...))


Make sense?

If it isn't feasible to use an array to index (because you are going to continually add triangles) use a hash table instead. They're super fast for lookups!


mindstorms(Posted 2007) [#4]
wooppss...


mindstorms(Posted 2007) [#5]
Thanks, octothorpe, that is alot simpler than I would have imagined... :)

I assume that when deleting triangles, the other triangle's indexes change, making me have to re-index the list whenever I deleted a section of the wall...

edit: figured it out, thanks. Bummer there is no delete triangle command/ delete vertex command.


octothorpe(Posted 2007) [#6]
While you cannot delete triangles, you can move their vertices to a graveyard off-screen and keep a list of them so they can be re-used. This is probably only feasible if the triangles involved each own their own three vertices.