Assigning meshes to arrays

Blitz3D Forums/Blitz3D Programming/Assigning meshes to arrays

BlackD(Posted 2004) [#1]
Yes, I know all about types :) but just wondering if theres a particular reason (performance wise) why the following example is bad? (in relation to array use)

	Graphics3D 800,600
	camera=CreateCamera()
	light=CreateLight(1)
	RotateEntity light,90,0,0
	Dim sphere(100,1)
	For i = 1 To 100
		sphere(i,0) = CreateSphere()
		PositionEntity sphere(i,0),Rand(-10,10),Rand(-5,5),Rand(5,50)
		sphere(i,1) = i
		Next
	RenderWorld
	For i = 1 To 100
		CameraProject camera,EntityX(sphere(i,0)),EntityY(sphere(i,0)),EntityZ(sphere(i,0))
		Color 0,0,0
		Text ProjectedX(),ProjectedY(),sphere(i,1),1,1
		Next
	Flip
	WaitKey()
	End
Not concerned about the graphical "clutter" - thats just there for testing.

+BlackD


Ross C(Posted 2004) [#2]
Nope. I sometimes uses arrays like this too. Some ppl are dead set against it though. Whatever way works really. I sometimes do...

Dim Sphere(100,2)


the last dimension - 0 = X coord 1 = Y coord 2 = Z coord. 3 = entity

To make it easier, you can set up global variables to plug in, so you don't need to remember number


Global X = 0
Global Y = 1
Global Z = 2
Global Ent = 3

Dim Sphere(100,3)

Sphere(5,Ent) = CreateSphere()
Sphere(5,X) = 10
Sphere(5,Y) = 20
Sphere(5,Z) = 10




BlackD(Posted 2004) [#3]
Thanks Ross. Hehe :) I never thought of using Globals for that purpose - its like an array-based type (using names instead of numbers - very useful) :D


Shifty Geezer(Posted 2004) [#4]
Why do you need to record the X,Y,Z values when the entity structure already holds these?

Just dim Spheres, CreateSphere(), then to get it's coordinates use EntityX#(sphere(n)) or whatever the functions are. Otherwise your duplicating information.


BlackD(Posted 2004) [#5]
He was giving an example, thats all. :) There are several times you'd need to know the XYZ position of them outside of the entity state. For instance when using EntityPick - you only get the info on the entity you're picking, if you want to maniuplate multiple spheres in relation to one you're clicking on, then you need to know their positions, especially since you'll have to do a project to find out the actual handle of the sphere at that location in order to manipulate it.


BlackD(Posted 2004) [#6]
Unless of course, you use entitylinepick extensively. ;)

Quick terrain manipulation demo I just made. first time using entitypick. :) Mouse1 lowers the terrain. Unlike "terrain", each square is a separate surface which can be textured individually.

	Graphics3D 800,600,0,2
	camera=CreateCamera()
	light=CreateLight(2)
	LightConeAngles light,360,360
	PositionEntity light,10.5,0,-3
	AmbientLight 255,255,255
	LightColor light,-10,-10,-10
	RotateEntity light,10,0,0
	Dim map(20,15,0)
	SeedRnd MilliSecs()
	
	For x = 1 To 20
		For y = 1 To 15
			map(x,y,0)=CreateMesh()
			surface=CreateSurface(map(x,y,0))
			AddVertex surface,0,0,0,0,0
			AddVertex surface,0,0,1,0,1
			AddVertex surface,1,0,0,1,0
			AddVertex surface,1,0,1,1,1
			AddTriangle surface,0,1,2
			AddTriangle surface,3,2,1
			PositionEntity map(x,y,0),x,0,y
			brush=CreateBrush(Rand(255),Rand(255),Rand(255))
			PaintEntity map(x,y,0),brush
			EntityPickMode map(x,y,0),2
			FreeBrush brush
			UpdateNormals map(x,y,0)
			Next
		Next
		
	PositionEntity camera,10.5,10,-3
	RotateEntity camera,60,0,0
	PointEntity light,map(10,5,0)
	UpdateWorld
	RenderWorld
	
	While Not KeyHit(1)
	RenderWorld
	If MouseDown(1) Then
		CameraPick camera,MouseX(),MouseY()
		If PickedEntity() Then
			MoveEntity PickedEntity(),0,-.1,0
			LinePick PickedX()-1,1000,PickedZ(),0,-2000,0	;adjust entity on left side
			If PickedEntity() Then 
				surface=GetSurface(PickedEntity(),1)
				vertexdown surface,2
				vertexdown surface,3
				LinePick PickedX()+1,1000,PickedZ(),0,-2000,0
				End If
			LinePick PickedX()+1,1000,PickedZ(),0,-2000,0	;adjust entity on right side
			If PickedEntity() Then 
				surface=GetSurface(PickedEntity(),1)
				vertexdown surface,0
				vertexdown surface,1
				LinePick PickedX()-1,1000,PickedZ(),0,-2000,0
				End If
			LinePick PickedX(),1000,PickedZ()+1,0,-2000,0 ;adjust entity on upper side
			If PickedEntity() Then
				surface=GetSurface(PickedEntity(),1)
				vertexdown surface,0
				vertexdown surface,2
				LinePick PickedX(),1000,PickedZ()-1,0,-2000,0
				LinePick PickedX()-1,1000,PickedZ()+1,0,-2000,0
				If PickedEntity() Then
					surface=GetSurface(PickedEntity(),1)
					vertexdown surface,2
					LinePick PickedX()+1,1000,PickedZ()-1,0,-2000,0
					End If
				LinePick PickedX()+1,1000,PickedZ()+1,0,-2000,0
				If PickedEntity() Then
					surface=GetSurface(PickedEntity(),1)
					vertexdown surface,0
					LinePick PickedX()-1,1000,PickedZ()-1,0,-2000,0
					End If
				End If
			LinePick PickedX(),1000,PickedZ()-1,0,-2000,0 ;adjust entity on lower side
			If PickedEntity() Then
				surface=GetSurface(PickedEntity(),1)
				vertexdown surface,1
				vertexdown surface,3
				LinePick PickedX(),1000,PickedZ()+1,0,-2000,0
				LinePick PickedX()-1,1000,PickedZ()-1,0,-2000,0
				If PickedEntity() Then
					surface=GetSurface(PickedEntity(),1)
					vertexdown surface,3
					LinePick PickedX()+1,1000,PickedZ()+1,0,-2000,0
					End If
				LinePick PickedX()+1,1000,PickedZ()-1,0,-2000,0
				If PickedEntity() Then
					surface=GetSurface(PickedEntity(),1)
					vertexdown surface,1
					LinePick PickedX()-1,1000,PickedZ()+1,0,-2000,0
					End If
				End If
			End If
		End If
	Flip
	Wend
	End
	
	Function VertexDown(surface,number)
		VertexCoords surface,number,VertexX(surface,number),VertexY(surface,number)-.1,VertexZ(surface,number)
		End Function



aab(Posted 2004) [#7]
I use arrays for holding Meshes: it comes in usefull
eg:

it works for me. i sometimes use other values for ammo, maxammo, recoil configuration etc.