Veteran Member
|
07-17-2014
, 17:10
[Metamod] Lie flat (to surface)
|
#1
|
Anyone have or can convert this for metamod?
Code:
static Float:origin[3], Float:traceto[3], trace = 0, Float:fraction, Float:angles[3], Float:angles2[3]
pev(ent, pev_origin, origin)
pev(ent, pev_angles, angles)
// We want to trace downwards 10 units.
xs_vec_sub(origin, Float:{0.0, 0.0, 10.0}, traceto)
engfunc(EngFunc_TraceLine, origin, traceto, IGNORE_MONSTERS, ent, trace)
// Most likely if the entity has the FL_ONGROUND flag, flFraction will be less than 1.0, but we need to make sure.
get_tr2(trace, TR_flFraction, fraction)
if (fraction == 1.0) return
// Normally, once an item is dropped, the X and Y-axis rotations (aka roll and pitch) are set to 0, making them lie "flat."
// We find the forward vector: the direction the ent is facing before we mess with its angles.
static Float:original_forward[3]
angle_vector(angles, ANGLEVECTOR_FORWARD, original_forward)
// If your head was an entity, no matter which direction you face, these vectors would be sticking out of your right ear,
// up out the top of your head, and forward out from your nose.
static Float:right[3], Float:up[3], Float:fwd[3]
// The plane's normal line will be our new ANGLEVECTOR_UP.
get_tr2(trace, TR_vecPlaneNormal, up)
// This checks to see if the ground is flat. If it is, don't bother continuing.
if (up[2] == 1.0) return
// The cross product (aka vector product) will give us a vector, which is in essence our ANGLEVECTOR_RIGHT.
xs_vec_cross(original_forward, up, right)
// And this cross product will give us our new ANGLEVECTOR_FORWARD.
xs_vec_cross(up, right, fwd)
// Converts from the forward vector to angles. Unfortunately, vectors don't provide enough info to determine X-axis rotation (roll),
// so we have to find it by pretending our right anglevector is a forward, calculating the angles, and pulling the corresponding value
// that would be the roll.
vector_to_angle(fwd, angles)
vector_to_angle(right, angles2)
// Multiply by -1 because pitch increases as we look down.
angles[2] = -1.0 * angles2[0]
// Finally, we turn our entity to lie flat.
set_pev(ent, pev_angles, angles)
Can't get mine to work:
Code:
inline Vector DotProduct2(const Vector& a, const Vector& b) { return Vector(a.x-b.x,a.y-b.y,a.z-b.z); }
{
TraceResult tr;
Vector angles = pev->angles, angles2;
// We want to trace downwards 10 units.
Vector vecDot = DotProduct2( pev->origin, Vector ( 0, 0, 10 ) );
UTIL_TraceLine( pev->origin, vecDot, ignore_monsters, ENT(pev), &tr );
// Most likely if the entity has the FL_ONGROUND flag, flFraction will be less than 1.0, but we need to make sure.
if ( tr.flFraction == 1.0 ) return;
// Normally, once an item is dropped, the X and Y-axis rotations (aka roll and pitch) are set to 0, making them lie "flat."
// We find the forward vector: the direction the ent is facing before we mess with its angles.
Vector original_forward;
UTIL_MakeVectorsPrivate( angles, original_forward, NULL, NULL );
// If your head was an entity, no matter which direction you face, these vectors would be sticking out of your right ear,
// up out the top of your head, and forward out from your nose.
Vector right, forward;
// The plane's normal line will be our new ANGLEVECTOR_UP.
// This checks to see if the ground is flat. If it is, don't bother continuing.
if ( tr.vecPlaneNormal.z == 1.0 ) return;
// The cross product (aka vector product) will give us a vector, which is in essence our ANGLEVECTOR_RIGHT.
right = CrossProduct( original_forward, tr.vecPlaneNormal );
// And this cross product will give us our new ANGLEVECTOR_FORWARD.
forward = CrossProduct( tr.vecPlaneNormal, right );
// Converts from the forward vector to angles. Unfortunately, vectors don't provide enough info to determine X-axis rotation (roll),
// so we have to find it by pretending our right anglevector is a forward, calculating the angles, and pulling the corresponding value
// that would be the roll.
forward = UTIL_VecToAngles( angles );
right = UTIL_VecToAngles( angles2 );
// Multiply by -1 because pitch increases as we look down.
angles.z = -1.0 * angles2.x;
// Finally, we turn our entity to lie flat.
pev->angles = angles;
(*g_engfuncs.pfnServerPrint)("Lie flat\n");
}
Last edited by Rirre; 07-17-2014 at 20:37.
|
|