ent_nextvertex(ENTITY*, VECTOR* pos );
A7.62 Returns the number of a terrain vertex next to a given position. Can be used for deforming terrain parts.
Parameters:
ENTITY* - Terrain entity pointer.
pos - XYZ Position for determining the vertex number. Only the x and y coordinates are relevant.
Returns
Vertex number, or 0 when the function failed.
Speed:
Fast
Remarks:
- Terrain vertices are arranged in rows and columns in x and y direction. This allows easily to find the neighbor vertices to a given vertex number.
-
The returned vertex number can be used for ent_getvertex /ent_setvertex. It is not identical to the mesh vertex number of an individual terrain mesh, which depends on chunking and LOD.
Example:
// use a dynamic array to mark already-occupied places
char* terrain_places = NULL;
// place an entity at a random position on terrain
function ent_place(ENTITY* ent,ENTITY* terrain)
{
if (!ent || !terrain) return;
var num,ground_z;
// generate terrain_places char array with the same size as the terrain vertices number
if (terrain_places == NULL)
terrain_places = (char*)sys_malloc(ent_status(terrain,1)*sizeof(char));
var i = 0;
while (i++ < 10) // make 10 attempts to place it
{
// calculate random position on terrain
ent.x = terrain.min_x + random(terrain.max_x-terrain.min_x);
ent.y = terrain.min_y + random(terrain.max_y-terrain.min_y);
// get the vertex number at that place
num = ent_nextvertex(terrain,ent.x);
if (!terrain_places[num-1]) // when place not already occupied
break;
}
terrain_places[num-1] = 1; // mark place as occupied
ent_getvertex(terrain,hit,num);
ent.z = hit.z - ent.scale_z*ent.min_z - 2; // plant entity on terrain ground
}
function main()
{
level_load("terrain.hmp");
var i;
for (i=0; i<500; i++)
ent_place(ent_create("tree2.mdl",NULL,NULL),level_ent);
sys_free(terrain_places); // not really necessary - is automatically freed at exit
}
See also:
ent_next, ent_status, ent_getvertex
► latest version online