I am creating a randomly-generated environment for a game I'm developing. I am using OpenGL
and coding in Java
.
I am trying to randomly place trees in my world (to create a forest), but I don't want the models to overlap (which happens when two trees are placed too close to each other). Here's a picture of what I'm talking about:
I can provide more code if necessary, but here's the essential snippets. I am storing my objects in an ArrayList
with List<Entity> entities = new ArrayList<Entity>();
. I am then adding to that list using:
Random random = new Random();
for (int i = 0; i < 500; i++) {
entities.add(new Entity(tree, new Vector3f(random.nextFloat() * 800 - 400,
0, random.nextFloat() * -600), 0, random.nextFloat() * 360, 0, 3, 3, 3);
}
where each Entity
follows the following syntax:
new Entity(modelName, positionVector(x, y, z), rotX, rotY, rotZ, scaleX, scaleY, scaleZ);
rotX
is the rotation along the x-axis, and scaleX
is the scale in the x-direction, etc.
You can see that I am placing these trees randomly with random.nextFloat()
for the x
and z
positions, and bounding the range so the trees will appear in the desired location. However, I would like to somehow control these positions, so that if it tries to place a tree too close to a previously placed tree, it will recalculate a new random position. I was thinking that each tree Entity
could have another field, such as treeTrunkGirth
, and if a tree is placed in the distance between another tree's location and it's treeTrunkGirth
, then it will recalculate a new position. Is there a way to accomplish this?
I am happy to add more code snippets and details as necessary.
treeTrunkGirth
instead of a constant to determine the min distance for placing a tree if they need to vary. – Pikalek Sep 08 '16 at 21:02