Original Post
Hi All,
Heres a code snip that I use all the time, and find very useful.
This takes an Entity with lots of segments and shifts the segments around their pivot point down to the terrain ground level. (Actually if the object is placed 10 units above ground level, all segments shift to 10 units above ground level)
I have used this in the past for lots of things,
ie Create a Clump of 10 or so trees, as one object with 10 segments. Each segments pivot is based at the foot of the tree. Now run the entity through this once during setup and it repositions each segment down to ground level. (place large groups of trees on hills with ease)
Currently using it in my flight simulator to position houses/buildings on hills. I have created a 2x2 city block, with about 20 buildings as one object and just duplicate this one object around the terrain.
Benefits,
A lot easier than placing 10 trees individually.
Faster, as PR only needs to check the BBox of one object to determine if off screen, rather than 10
//////////////////////////////////////////////////////
// move each segment in an entitys pivot to the ground
//////////////////////////////////////////////////////
void SegmentsToGround(PR_ENTITY *ent)
{
int i;
PR_REAL x,y,z;
PR_REAL x2,y2,z2;
PR_ORIENTATION *orient;
x = ent->orientation.location.x;
z = ent->orientation.location.z;
y = GAME_GetTerrainHeight (x, z); //replace with your height checking routine
orient=&ent->orientation;
for (i = 0; i < ent->shape->num_segments; i++)
{
tvector[0] = ent->segment_orientation.location.x * orient->scale.x;
tvector[1] = 0;
tvector[2] = ent->segment_orientation[i].location.z * orient->scale.z;
PR_Transform (orient->rot_mat);
x2 = ent->segment_orientation[i].location.x;
z2 = ent->segment_orientation[i].location.z;
y2 = (GAME_GetTerrainHeight (x+tvector[0], z+tvector[2])-y);
PR_PositionSegment(ent, i, x2,y2,z2);
}
}
Hope it is of use to someone.
Cheers
Darrin