2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Again new question about path patrol :)
#454209
09/01/15 10:48
09/01/15 10:48
|
Joined: Jul 2001
Posts: 4,801 netherlands
Realspawn
OP
Expert
|
OP
Expert
Joined: Jul 2001
Posts: 4,801
netherlands
|
I use this script and it works. The model walks to the end of the path then gets to the start point again and so on. I've learned thanks to JCL also how to remove the character at the end of the path. Now the final thing i would like to understand is how to make the model go back at the end of the path the same route instead of running straight to the starting point thank you for your time
action moveontrack_01()
{
var walk_percentage;
var walking_speed = 0.3;
VECTOR last_pos[3];
VECTOR direction[3];
var distance = 0;
var dist_to_node;
var current_node = 1;
set(my,SHADOW|PASSABLE);
path_set(me, "path_000");
while(1)
{
ent_animate(my, "run", walk_percentage, ANM_CYCLE); // play the "walk" animation
walk_percentage += 6 * time_step; // "3" controls the animation speed
path_spline(me, my.x, distance);
distance += walking_speed ;
vec_diff(direction, my.x, last_pos);
vec_to_angle(my.pan, direction);
vec_set(last_pos, my.x);
wait(1);
}
}
Last edited by Realspawn; 09/01/15 10:48.
|
|
|
Re: Again new question about path patrol :)
[Re: Realspawn]
#454216
09/01/15 13:41
09/01/15 13:41
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, you will need to modify back to zero the 'distance' variable once reached the total length by converting the 'walking_speed' variable to a negative value. You will need to get the total length of a path by 'path_length' before the loop.
var nPathLength = path_length ( me );
Then check the distance value inside the loop and switch the walking speed variable sign.
nDistance += nWalkingSpeed * time_step;
if ( ( nDistance < 0 ) || ( nDistance > nPathLength ) )
{
nWalkingSpeed *= -1;
nDistance = clamp ( nDistance, 0, nPathLength );
}
There are a couple of troubles in your code. You declared 'last_pos' and 'direction' as arrays of three vector structs. You only need one vector struct each.
VECTOR vecDirection; // A single vector struct
VECTOR vecDirection[3]; // An array of vector structs
VECTOR *vecDirection; // A single pointer to a vector struct
VECTOR *vecDirection[3]; // An array of pointers to vector structs
You modify the 'distance' variable by adding the 'walking_speed' constant value. This method is framerate dependant. You need to multiply it by 'time_step' in order to certainly convert a speed to a distance in reference to a time lapse. Hope it helps. Salud!
|
|
|
Re: Again new question about path patrol :)
[Re: Realspawn]
#454225
09/01/15 16:03
09/01/15 16:03
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
I use my own version of the hungarian notation. n for vars: nDistance i for integer: iIndex str for strings: strName vec for vector: vecPosition ang for angles: angOrientation ent for entities: entPlayer and so on... It enhances the readability of the code a lot.
|
|
|
Re: Again new question about path patrol :)
[Re: txesmi]
#454227
09/01/15 16:22
09/01/15 16:22
|
Joined: Jul 2001
Posts: 4,801 netherlands
Realspawn
OP
Expert
|
OP
Expert
Joined: Jul 2001
Posts: 4,801
netherlands
|
It works i am sure this code is not perfect but it works are there thing i should change about this to make it a perfect clean code ? thank you for you help it drove me nuts lol
ENTITY* walker01;
action moveontrack_01()
{
var walk_percentage;
var distance = 0;
var walking_speed =10;
VECTOR vecLastposition;
VECTOR vecDirection;
var vDir[3];
walker01 =me;
path_set(me, "path_000");
var nPathLength = path_length ( me );
while(1)
{
path_spline(walker01, my.x, distance);
vec_diff(vDir,my.x,vecLastposition);
vec_to_angle(my.pan,vDir);
vec_set(vecLastposition,my.x);
distance += walking_speed * time_step;
if ( ( distance < 0 ) || ( distance > nPathLength ) )
{
walking_speed *= -1;
distance = clamp ( distance, 0, nPathLength );
}
wait(1);
ent_animate(my, "walk", walk_percentage, ANM_CYCLE); // play the "walk" animation
walk_percentage += 5 * time_step; // "3" controls the animation speed
}
}
Last edited by Realspawn; 09/01/15 16:38.
|
|
|
Re: Again new question about path patrol :)
[Re: Realspawn]
#454234
09/01/15 19:53
09/01/15 19:53
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
I am not the man to talk about perfection but I can see some little improvements can be done. I guess your path is contained in a horizontal plane. If so, you can compute the entity pan angle with trigonometrics and save some microticks.
my.pan = atan2v ( my.y - vecLastposition.y, my.x - vecLastposition.x );
'vecDirection' is declared but not used. I would delete the 'vDir' array and use 'vecDirection' instead. Put the 'wait' instruction at the end of the loop. It is a good practice. Add it at other place when has a clear purpose and is needed. There is no problem as it is anyway. Be consistent with spaces and line breaks. If one line has spaces between operators, parameters, etc and the next one has no spaces the reading is blurred. Especially when the reader has no idea. Use some sort of hungarian notation. It really deserves the effort.
|
|
|
|