planting trees

Blitz3D Forums/Blitz3D Programming/planting trees

Rook Zimbabwe(Posted 2004) [#1]
If I had a texturemap terrain and I wanted to plat a few trees on it randomly...

What would be the best method to use...

I was thinking:

For newtree = 1 to 50
copyentity tree
treex=rnd(-50,50)
treey=rnd(-50,50)
treez=20
put newtree,treex,treey,treez
next
I can't eem to make it workie! Maybe I need to use some form of pickentity to check the location X and Y to make sure the tree is above ground???
-RZ


Bremer(Posted 2004) [#2]
If using a blitz terrain couldn't you just read the terrain height of the terrain at the position you want to place it?

TerrainHeight# ( terrain,grid_x,grid_z )


Rook Zimbabwe(Posted 2004) [#3]
I did it... Y'all are probably gonna hurt me but once again I forgot to put GLOBAL before putting trees in a function. I just had to modify the puttarget function from my other game. trees look nice. Now if I could only make them not appear in the buildings!

Oh yeah... the code for all those who want it.
First you can load a bumpmap texture or a mesh to create the ground... I decided to use a bumpmap here.
Global terr=LoadTerrain("venus2.jpg")
ScaleEntity terr,25,240,25
PositionEntity terr,-10*512,0,-10*512
EntityFX terr,1
EntityType terr,2	;Load terrain
If you don't make it global then you can't find it when we look at it later to plunk balls or trees or whatever it is down on the surface!

OK then you create your entity... I used spheres here cause everyone has that mesh built in to B3D.
dot=CreateSphere(8)
ScaleEntity dot,4,4,4
Type tree
	Field entity
	Field treex
	Field treey
	Field treez
End Type
Notice that I used a type. These things confused me greatly at first... now only a little. : )

The type field holds the entityhandle, the x location, the y location and the z location.

There is a great tut on types and handles... more than one... in the TUTORIALS section of this board.

OK next I filled in a few random trees...
For tt= 1 To 100
t.tree = New tree ;					where did "T" come from... I made it up. I could
t\entity = CopyEntity (dot) ;			have used a letter or any word not reserved
t\treex=Rnd(-150,150) ;						NOTE " \ " not " / "
t\treey=Rnd(-250,250)
t\treez=Rnd(-600,600) ; 						Since B3D uses both + and - nums I had to use Rnd
Next ;	
that gave me a number of trees to throw around.

Next I created a function called "put_trees()" and placed it after END
End

Function put_trees()
	For t.tree = Each tree
		EntityType t\entity,2
		where1=TerrainY(terr,t\treex,t\treey,t\treez)
		PositionEntity t\entity,t\treex,where1,t\treez
		Next
			
End Function 
I call this function before the program starts if the trees are static and not a moving thing... if the trees are entitys that can be shot or something I look at my tutorial on selecting using types here:
http://www.blitzbasic.co.nz/Community/posts.php?topic=35127

I call this function simply by putting its name
put_trees()

While Not KeyHit(1)
As a small note you may have to adjust the height of your sphere or tree or whatever depending on where the 0,0,0 point is in its mesh. In the spheres created here the 0 point is in the center so all the spheres are sticking out of the grouund from the middle.

And thats it...

Good luck and happy coding!