Snap to Ground

Posted By: 82RJZAE

Snap to Ground - 07/28/12 20:23

Hi, can anyone provide a snippet about how to snap a model to the ground using c_trace? I can't seem to get it to work.
Posted By: MasterQ32

Re: Snap to Ground - 07/28/12 20:34

To snap a variable, you can use this:
var snapped_x = grid_size * integer(nonsnapped_x / grid_size);
Posted By: 82RJZAE

Re: Snap to Ground - 07/28/12 20:41

I guess what I really meant was that I have a model placed into the level via script and I would like to have that model either moved up (if it's inside the ground) or down (if it's above the ground) via changing its z coordinate using a c_trace.
Posted By: Uhrwerk

Re: Snap to Ground - 07/28/12 20:50

Then show us the code you already have. We will be glad if we can help you.
Posted By: 82RJZAE

Re: Snap to Ground - 07/28/12 21:05

That's the thing; I'm a little confused as to where I should be tracing from to account for both cases of having a model placed in the ground or above the ground. I have tried tracing from my.x to a point below the character and I have tried tracing from the lowest vertex of the model to the ground both without getting the correct solution. My latest attempt was the following code:

Code:
VECTOR vec_min;
VECTOR vec_max;
wait(1);
c_setminmax(me);
vec_for_min(vec_min, me);
vec_for_max(vec_max, me);
result = c_trace(vec_max, vector(vec_max.x,vec_max.y,vec_max.z-1000), IGNORE_ME | IGNORE_MODELS | IGNORE_PASSABLE | USE_BOX | IGNORE_SPRITES); // trace from the origin to the floor and store the result
my.z -= target.z;
my.z += (vec_max.z-vec_min.z);


1. traced from the top vertex to a position below
2. lower the distance traced from the top vertex to the position below (c_trace returns the distance)
3. then add the height of the entity (max vertex - min vertex)

In theory, the above solution should work for models placed in the ground but it doesn't seem very efficient and it also doesn't work. tongue Any tips or better ways to approach this case?

Thanks for your replies!
Posted By: Arrovs

Re: Snap to Ground - 07/28/12 21:16

Tracing is good because USE_BOX uses box borders for distance(at least i think so).
But i think your code for placing object isnt exatcly right.
It should be as
Code:
vec_set(my.x, target.x);//place object at scanned point
my.z += my.min_z;//possible you can add some more quants if you vant it to not stuck in ground(if using c_move aftervards)


If my.min_z is negative just give -my.min_z;
Posted By: Uhrwerk

Re: Snap to Ground - 07/28/12 23:44

I think you misunderstood the documentation on vec_for_min and vec_for max. These functions sets the given vector to the maximum and minimum of the entity in object space. A pretty simple approach to place an entity on the ground is to first lift it up by a certain value, like "my->z += some_distance". Then you can trace from the entities position in a downwards direction:
Code:
result = c_trace(my.x,vector(my.x,my.y,my.z-2*some_distance),IGNORE_ME | etc.);

You can then set the entities position to target and add entity.min_z to the z position of the entity.
Posted By: 82RJZAE

Re: Snap to Ground - 07/29/12 03:56

That's true! I didn't consider that approach. Thanks! laugh
© 2024 lite-C Forums