PDA

View Full Version : [AI movement] Jumping using movegravity()



IzNoGoD
18th May 2013, 09:51
jump(start, end)
{
height = end[2] - start[2];
v0 = 275;
t0 = v0 / getcvarint("g_gravity");
s0 = 0.5 * getcvarint("g_gravity") * t0 * t0;
height -= s0;
if(height > 0)
return false; //no jumping possible. Target too high.
t1sq = -2 * height / getcvarint("g_gravity");
time = int((sqrt(t1sq) + t0) * 20) / 20;
xdist = end[0] - start[0];
ydist = end[1] - start[1];
xspeed = xdist / time;
yspeed = ydist / time;
self movegravity((xspeed, yspeed, v0), time);
wait time;
self.origin = end;
return true;
}

Just some math...
To change this function into fall(), just set v0 to 0, t0 to 0 or s0 to 0. (or remove all associated stuff)

kung foo man
18th May 2013, 11:10
Thats a nice function :)

Though missing sqrt, so I post your intsqrt() and the 2 sqrt-function of the extension:



sqrt(arg) { return closer(800, arg); }
invSqrt(arg) { return closer(801, arg); } // check: http://www.beyond3d.com/content/articles/8/

intsqrt(input)
{
output = 0;
while(output * output < input)
output++;
if(maps\mp\_utility::abs(input - output * output) > maps\mp\_utility::abs(input - (output - 1) * (output - 1)))
output--;
return output;
}


File is here: http://killtube.org/downloads/cod2/exampleserver/main/std/math.gsc

randall
18th May 2013, 17:26
I have also wrote a sqrt function, its accurate to 6 decimals:

sqrt( b )
{
a = 0;
for ( i = 1; i <= 1000000; i *= 10 )
{
while ( a * a <= b )
a += (1 / i);

if ( a * a > b )
a -= (1 / i);
}

return a;
}
To increase the accuracy write a bigger number in 'for' cycle.

And an other mathematical function:

vectorCross(vec1, vec2)
{
result =
(vec1[1]*vec2[2] - vec2[1]*vec1[2]) -
(vec1[0]*vec2[2] - vec2[0]*vec1[2]) +
(vec1[0]*vec2[1] - vec2[0]*vec1[1]);

return result;
}

serthy
19th May 2013, 09:27
I have also wrote a sqrt function...

my one:

sqrt( x )
{
y = x;

for( z = x *0.25 ; abs( z - y ) >= 0.001 ; y = ( z + ( x / z ) ) * 0.5 )
{
z = y;
}

return y;
}