Placing trees on uneven terrain

Hello, I’m trying to randomly place trees around a map but having a bit of trouble getting them at the right height. I have the x and y coordinates sorted but don’t know how I would get the z.

I’ve had a look at using a collision ray pointing down and getting the position it collides but I have no idea how to do that. :frowning: I’m still pretty new to Panda3D.

Is there an easy way to this or would I need to use a collision ray? The number of trees would be about 70.

Thanks for any help :slight_smile:

It depends, I think, on how you’re handling your terrain:

If you’re using a GeoMipTerrain (you do mention “uneven terrain”), then you should be able to simply use “myTerrain.getElevation(x, y)” (altered to suit your code ad names). See here.

If you’re not, but you do have collision geometry, then, in short, the idea would be to create a CollisionRay, attach it to a new NodePath, and then for each tree place the NodePath at the target x- and y- location, tell the traverser to traverse and use the result.

The easiest source for this second method might be the Roaming Ralph sample; “Ralph” would presumably be replaced by a blank NodePath, and of course the movement and obstacle code would all presumably be ignored. You could also look at the “Clicking on 3D Objects” page of the manual; while likely having less superfluous code, it might also call for more modification to the core code, as it’s intended for a ray located at the camera’s position and directed by the mouse, rather than a vertical ray never rotated.

Yay that works perfectly thanks :smiley:

I’m using geoMipTerrain for my map and the terrain.getElevation(x,y) works awesome. Thanks for the help.

Oops I spoke too soon :confused: It works fine on the flat ground but for some reason the heights are coming out too low. Most of them are around 0.5 or 0.6

When I multiply it by 100 the hight seems fine (though the occasional one is a little bit above ground)

Is that normal? :frowning:

You should multiply the height by the z-scale of your geomipmapterrain. Unaltered this terrain type has a height between 0 and 1. That’s normal.

The codewise simple way would be to place trees by hand and writing down their locations or just dumping them to disk as a single bam file.

Thanks :smiley: How would I find the z-scale (sorry if that’s a dumb question :confused: ) or is there a default value?

The terrain is randomly generated each time so I can’t really place them by hand :frowning:

Thanks for the help

you can use the getSz() functions on the geometry-nodepath of the terrain, if you only have the geomip object the code would look like:

terrainRoot = terrain.getRoot() # where 'terrain' is the geomip object of your terrain
scaleZ = terrainRoot.getSz()

(if the terrain is generated by the current object it would of course be self.terrain.getRoot() )

Thanks it’s working fine now :smiley: