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. 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.
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.