I am going to be spawning enemies in random locations based on the location of the player. Is there anyway to check to see if a point falls inside of an object? I don’t want an enemy to be spawned inside of a hill.
As the BGE (as nearly all game engines) uses a surface based model description such operations are pretty expensive and sometimes just impossible (e.g open and manifolded mesh objects).
Nevertheless there are over ways to get sufficient results.
A) create an action that covers many spawn locations. You can play a random frame to place an emitter object before it adds a new object. This is pretty easy and very save.
B) You place the emitter somewhere and detect with a ray what z-coordinate to place.
I’m sure there are more options.
The simplest method would be to perform a raycast upwards and check if you hit something. Provided that the object’s center is at its base, you can then compare the z values of the target position, base position of the hit object and hit position to determine if you’re inside the object.
The best way to handle this would be to prevent enemies from spawning in the terrain in the first place. This is usually done by using designated spawn points or having a spawn mesh.
Spawn points would be just a list of empties placed in valid spawn locations. When you want to spawn an enemy, just pick a random empty and add the enemy at its location.
A spawn mesh is just a mesh whose vertices act as spawn points. Construct the mesh over valid spawning areas, then just chose a random vertex as your spawn position (or a little bit above it if the mesh is at terrain level). You could potentially use your current terrain mesh as a spawn mesh or at least use it as a basis for making one (for example, just duplicate the terrain and remove vertices over any areas you don’t want enemies to spawn in).
Note that it’s still possible a spawn point may be obstructed (the player or a movable object may be on top of a spawn point), so it’s still a good idea to make sure the spawn point is valid. You can do this by adding a temporary collision box (that’s the same size as the enemy you’re going to spawn) at the chosen spawn point. On the next frame, check if it collides with anything using a collision sensor attached to it. If there is no collision, then you can go ahead and spawn your enemy. Otherwise just chose another spawn point.