Again new question about path patrol :)

Posted By: Realspawn

Again new question about path patrol :) - 09/01/15 10:48

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 laugh

thank you for your time laugh

Code:
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);

	}
}


Posted By: txesmi

Re: Again new question about path patrol :) - 09/01/15 13:41

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.
Code:
var nPathLength = path_length ( me );



Then check the distance value inside the loop and switch the walking speed variable sign.
Code:
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.
Code:
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!
Posted By: Realspawn

Re: Again new question about path patrol :) - 09/01/15 13:43

great thank you so much grin i will start testing this right away grin
Posted By: txesmi

Re: Again new question about path patrol :) - 09/01/15 14:21

glad to help laugh
Posted By: Realspawn

Re: Again new question about path patrol :) - 09/01/15 15:41

not doing well so far but keep ttrying lol

why do you have the letter N infront of all ?
Posted By: txesmi

Re: Again new question about path patrol :) - 09/01/15 16:03

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.
Posted By: Realspawn

Re: Again new question about path patrol :) - 09/01/15 16:22

It works laugh 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

Code:
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
	}
}


Posted By: txesmi

Re: Again new question about path patrol :) - 09/01/15 19:53

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.
Code:
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. grin
Posted By: Realspawn

Re: Again new question about path patrol :) - 09/01/15 21:01

Super it all works laugh because this works i had no problem now to make it remove at the end point laugh and to make it restart at the beginning laugh

thank you for hinting pointing me in the right direction laugh
© 2024 lite-C Forums