Facing another object?

Posted By: Jack2010

Facing another object? - 05/17/10 21:29

Hey guys! I think I read somewhere that it's possible to make an object face another through a vec_ function? I'm unsure about this, but does anyone know if there is such a function or if an example of one exists?

Thanks!
Posted By: Superku

Re: Facing another object? - 05/17/10 21:37

Use vec_diff and vec_to_angle (see Manual for detailed information).
VECTOR temp;
...
vec_diff(temp,ent1.x,ent2.x);
vec_to_angle(ent2.pan,temp);
ent2.tilt = 0; //only turn left/right

This code makes ent2 face ent1.
Posted By: Jack2010

Re: Facing another object? - 05/17/10 22:18

Thanks for the help Superku! Works great, a lot better than what I could have thought of grin.
Posted By: Jack2010

Re: Facing another object? - 05/18/10 14:58

Sorry for the double post, but the edit button isn't working (atleast not on my browser)

Is there a way to slow down the movement of the object to face another (using the code posted by Superku), so the user actually sees the movement instead of the object immediately changing direction?

I'm guessing it would be something like so:

var temp_pan = atan(my_vector.x/my_vector.y); // Copied from manual
var temp_tilt = asin(my_vector.z/length(my_vector)); // ^
var i;
for(i = 0; i < temp_pan; i++)
{
angle.pan += 1;
}
for(i = 0; i < temp_tilt; i++)
{
angle.tilt += 1;
}

Could anyone confirm this? I can't seem to get it working frown.

Thanks!
Posted By: TrackingKeks

Re: Facing another object? - 05/18/10 15:35

Code:
var temp_pan = atan(my_vector.x/my_vector.y); // Copied from manual
var temp_tilt = asin(my_vector.z/length(my_vector)); // ^
var i;
for(i = 0; i < temp_pan; i++)
{
angle.pan += 1*time_step;
wait(1);
}
for(i = 0; i < temp_tilt; i++)
{
angle.tilt += 1*time_step;
wait(1);
}


Posted By: Superku

Re: Facing another object? - 05/18/10 17:59

VECTOR temp,temp2;
...
vec_diff(temp,ent1.x,ent2.x);
vec_to_angle(temp2,temp);
ent2.pan += ang(temp2.x-ent2.pan)*0.1*time_step;

Try this. If the entity is turning too fast, replace the last line with the following:

ent2.pan += clamp(ang(temp2.x-ent2.pan)*0.1,-5,5)*time_step;

and adjust the max. speed by replacing 5 with different values.
Posted By: Jack2010

Re: Facing another object? - 05/18/10 18:38

Thanks guys! I'll try both now wink

Haha, only forum I've found where you can get to the point, helpful and efficient answers grin
Posted By: Jack2010

Re: Facing another object? - 05/18/10 19:29

AARGH! The edit button still isn't working frown

Superku, I've just tried your code and nothing seems to happen :S, its probably me doing something wrong. Just to check, ent1 is the object to face and ent2 is the object to face ent1. BTW, should I place my code where you've placed "..."? And does this code still include pan, tilt and roll angles?

Thanks for your answers!!
Jack.
Posted By: painkiller

Re: Facing another object? - 05/18/10 19:36

from the workshops:

Code:
// get the angle to the enemy			
			VECTOR vDirection;
			ANGLE vTargetAngle;
			vec_diff(vDirection,enemy.x,my.x);
			vec_to_angle(vTargetAngle,vDirection);
// vAngle is now the angle to the enemy.		
// Turn right or left depending on the difference
// between the current and the target pan angle			
			my.pan += time_step * sign(ang(vTargetAngle.pan - my.pan));


Posted By: Jack2010

Re: Facing another object? - 05/18/10 19:45

Originally Posted By: painkiller
from the workshops:

Code:
// get the angle to the enemy			
			VECTOR vDirection;
			ANGLE vTargetAngle;
			vec_diff(vDirection,enemy.x,my.x);
			vec_to_angle(vTargetAngle,vDirection);
// vAngle is now the angle to the enemy.		
// Turn right or left depending on the difference
// between the current and the target pan angle			
			my.pan += time_step * sign(ang(vTargetAngle.pan - my.pan));



Thanks painkiller! I've just added the following line:
my.tilt += time_step * sign(ang(vTargetAngle.tilt - my.tilt));

and it works perfectly!

Thanks everyone for the help! I'm gonna have to read up more on engine functions wink.
© 2023 lite-C Forums