C_Trace to Ground

Posted By: 82RJZAE

C_Trace to Ground - 07/22/10 20:37

Hi, I am coding a basic movement script and am having some difficulties with the c_trace function.

It is not returning the value that I thought it should. For example, when I place the player on a block in the level, it returns a value of about 40. This only occurs when the level is first started. The movement code seems to function fine after the first "drop" into the level.



I am using the following line to trace the distance to the ground:

Code:
distance_to_ground = c_trace(my.x, vector(my.x,my.y,my.z-500), IGNORE_ME | IGNORE_PASSENTS | IGNORE_PASSABLE | IGNORE_SPRITES | USE_BOX);


After that, I use the following code to simulate the acceleration due to gravity:

Code:
if(distance_to_ground >= 0)
	{
		accelerate(move_to_vector.z, (-3 * time_step), 0);
			if(move_to_vector.z < (distance_to_ground * -1))
				{
					move_to_vector.z = distance_to_ground * -1 * time_step;
				}
	}
else
	{
		move_to_vector.z = -1 * distance_to_ground;
	}

...

c_move(me, move_to_vector, nullvector, GLIDE | IGNORE_SPRITES | IGNORE_PASSABLE);


In the above image, the player flies into the sky because c_trace returns a negative value (distance_to_ground = -40). If I place the player 1 quant above the ground in WED, he will fall down at a high move_to_vector.z value (= -52).

Any suggestions that might fix this?
Posted By: Rich

Re: C_Trace to Ground - 07/23/10 03:13

I think this is becuase c_trace traces from the model's origin, and not it's "feet". Try this for your trace (untested):

Code:
distance_to_ground = c_trace(vector(my.x,my.y,my.z+my.min_z), vector(my.x,my.y,my.z-500), IGNORE_ME | IGNORE_PASSENTS | IGNORE_PASSABLE | IGNORE_SPRITES | USE_BOX);


Posted By: DJBMASTER

Re: C_Trace to Ground - 07/23/10 04:01

You can also look up 'floor_dist' in the manual.
Posted By: 82RJZAE

Re: C_Trace to Ground - 07/23/10 08:01

@Rich: When I used that trace it returned a value of -504.856 (-292.285 when the player was 1 quant above the ground in WED) for distance_to_ground. In other words, it still caused the player to fly into the sky. =P

@DJBMASTER: Interesting, but that function does not seem to offer as much freedom compared to the c_trace function.
Posted By: tD_Datura_v

Re: C_Trace to Ground - 07/23/10 22:05

Probably, if c_trace returns a negative value (i.e. the model is IN the ground), the model should not be pushed OUT of the ground all in one frame. Instead, the model might be pushed OUT of the ground gradually over time, just as it is probably dropped gradually over time.

Posted By: 82RJZAE

Re: C_Trace to Ground - 07/24/10 03:10

The model should not show up as being that far into the ground though. At most I would suspect that if it fell into the ground c_trace would return a negative value not far from 0. Is there possibly something wrong with the way my c_trace was setup?

When I use:
Code:
distance_to_ground = c_trace(my.x, vector(my.x,my.y,my.z-500), IGNORE_ME | IGNORE_PASSENTS | IGNORE_PASSABLE | IGNORE_SPRITES | USE_BOX);


and place the player flat on the ground in WED, the initial value distance_to_ground returns is -42.856. Whereas, if I place the player 1 quant above the ground in WED, the initial value distance_to_ground returns is 13.000.

On the other hand, after the intial "drop" to the ground the distance_to_ground variable seems to function as it should! When the player's feet are on the ground, the distance_to_ground variable reads close to 0.
Posted By: Nidhogg

Re: C_Trace to Ground - 07/24/10 09:24

Maybe the player's collision box is set wrong.
Posted By: 82RJZAE

Re: C_Trace to Ground - 07/24/10 19:39

Doing further tests I think the problem is the acceleration code. For example, if I press the TAB or F11 keys or anything that uses the beep function, during my descent to the ground, it causes move_to_vector.z to increase faster than normal. Could this problem be attributed to the time_step function?

EDIT: Removing time_step from the acceleration due to gravity code seems to reduce the initial speed the player accelerates downwards; however, I am still having the problem where c_trace is returning very low negative numbers when I place the model flat on a block in WED. If I jump, and allow the acceleration due to gravity place me back on the block, c_trace will return 0. Can anyone explain why this happens?

EDIT2: It turns out that the reason I was getting such a low initial value for c_trace was due to the bounding box exceeding the floor. Thank you everyone for your help!
© 2024 lite-C Forums