get speed

Posted By: Blobfist

get speed - 04/03/18 01:51

Hello everybody,

I was wondering how I could get the actual speed on an entity (moving using c_move).

The actual speed is needed in order to determin, if the entity is moving of not, aka. is potentially stuck or not.

I could only find commands to get the speed of PhysX objects in the Manuel, which unfortunately did not explain the build up.

Speed = distance/time, so is it possible to get the value of the x and y axis of the entity every frame, without killing the memory?

Please help me out.
Posted By: Dooley

Re: get speed - 04/03/18 05:57

If you are talking about c_move, it is a combination of the two vectors being used to move the entity.
Quote:
c_move(ENTITY* entity,VECTOR* reldist,VECTOR* absdist,var mode)

So the .x portion of the first vector (reldist) added to the .x portion of the second vector (absdist) would give you the total forward speed of the entity.

Then you would create three public variables, one for forward, one for side to side movement, and one for up/down movement.

Just after your c_move event, you can assign those variables like this:

c_move(my,my_move,nullvector,IGNORE_MODELS);
my_forward = my_move.x;
my_side = my_move.y;
my_up = my_move.z;

if you are using an absolute speed vector (like wind_speed or some other external factor) you could add this as well.

c_move(my,my_move,wind_speed,IGNORE_MODELS);
my_forward = my_move.x + wind_speed.x;
my_side = my_move.y + wind_speed.y;
my_up = my_move.z + wind_speed.z;

Then use these three variables to access the speed of the object in question.

There are probably other more efficient ways to do this, but this should work, and I don't think it will cause any kind of problem with memory.
Posted By: Quad

Re: get speed - 04/03/18 07:40

c_move returns distance covered, if it returns <=0 it means it's blocked by something in the direction you are trying to move. RTFM.
Posted By: 3run

Re: get speed - 04/03/18 10:37

Hey

It should be something like this:
Code:
action hero(){
	
	var moving_distance = 0, moving_speed = 0, is_moving = 0;
	
	while(my){
		
		moving_distance = c_move(my, dist, absdist, FLAGS);
		
		if(moving_distance > 0){
			
			moving_speed = moving_distance / time_step;
			is_moving = 1;
			
		}
		else{
			
			moving_speed = 0;
			is_moving = 0;
			
		}
		
		wait(1);
		
	}
	
}

Greets!
Posted By: Blobfist

Re: get speed - 04/16/18 21:38

Here I got the character stuck, thus it does not actually move.
c_move however, thinks it is moving with an speed of 2.

Code:
my.move_speed = c_move(me,vector(dist_ahead,0,0),NULL,IGNORE_PASSABLE | GLIDE);






The question still remains, how can you get the actual speed on an entity?
Posted By: Kartoffel

Re: get speed - 04/17/18 07:18

subtract the position before the move instruction from the position after the move instruction (giving you the vector the object has actually moved). this lets you calculate the length.
Posted By: txesmi

Re: get speed - 04/17/18 10:27

Originally Posted By: Dooley
if you are using an absolute speed vector (like wind_speed or some other external factor) you could add this as well.

c_move(my,my_move,wind_speed,IGNORE_MODELS);
my_forward = my_move.x + wind_speed.x;
my_side = my_move.y + wind_speed.y;
my_up = my_move.z + wind_speed.z;


@Dooley
I am sorry but your example is wrong. Those two vectors are in two different coordinate systems. It needs to convert one of them to the system of the other in order to get a rational result.
Posted By: 3run

Re: get speed - 04/17/18 13:33

Originally Posted By: Blobfist
Here I got the character stuck, thus it does not actually move.
c_move however, thinks it is moving with an speed of 2.

Code:
my.move_speed = c_move(me,vector(dist_ahead,0,0),NULL,IGNORE_PASSABLE | GLIDE);






The question still remains, how can you get the actual speed on an entity?
That's not a speed, but distance covered, and it will change with the framerate. Check my example, if you want to get framerate independent movement speed.

Greets.
Posted By: Blobfist

Re: get speed - 04/18/18 15:01

Sorry 3run, that didn't work.


However, I found a solution that works very good grin :

I set an fakeposition every second to the actual position of the character.
If the character is suppose to move (established with c_move), but the actual position is the same or similar as the fake position, and if actual position stays as such for an second (or less), then the character is stuck!

Code:
var dist_ahead = 10;//speed to travel
var moving_distance = c_move(me,vector(dist_ahead,0,0),NULL,IGNORE_PASSABLE | GLIDE);//c_move code that establishes the moving distance



Code:
VECTOR fakepos;//this is the fake pos
var fakeposdist;//this will be the distance between the original position and the fake position
var poswait = 1;//Update the fake position every second
var calcstuck = 0;//we are not potentially stuck at the beginning
//////////
if(moving_distance>0)//character thinks it's moving
{
if(calcstuck >=1)//character is stuck!
{
my.move_speed = 0;//no movement
//preform I`m stuck action here
}
else{my.move_speed = moving_distance;}// movement is c_move speed
}
else{my.move_speed = 0;}//no movement
//
if(poswait>0){poswait -= time_step / 16;}//update fake position every second
else
{
poswait = 1;
vec_set(fakepos.x,my.x);	
}
//
fakeposdist = vec_dist(my.x,fakepos.x);//find out distance between the fake position and actual position
if(fakeposdist <= 50){calcstuck += time_step / 16;}//if the distance if the same or very similar to the actual position, then check how long
else{calcstuck = 0;}//nothing is stuck

Posted By: 3run

Re: get speed - 04/18/18 15:02

Originally Posted By: Blobfist
Sorry 3run, that didn't work.
Sorry, but it works pretty good for me in all of my project.
Anyway, glad that you figured things out.
© 2024 lite-C Forums