Game Development Community

Checking if 'object' in view

by Christian Stock · in Torque Game Builder · 04/04/2006 (10:18 pm) · 3 replies

I'm programming basic AI for my enemy objects and I need to check if they can see the player. Objects can rotate freely in space, so I need to check if an enemy is facing the player (basically).

I know how to do this using some basic maths (not that hard really), but my question is if there is a TorqueScript function that does the job? My algorithm is basically checking what the angle between the two objects is, and then comparing that to the orientation of the enemy object. Is there something simple I can use like I can use VectorDist for distances (even just for finding the relative direction of the player object to the enemy object)?

Also, what impact does VectorDist have on framerates? It seems very fast, but just like to know if it's optimised or doing a sqrt() every call...

Christian

#1
04/21/2006 (8:12 pm)
In case other people wonder how to do this, I came up with the following:

function AIPlayer::get_vector( %this, %player )
{
	%dir = VectorSub( %this.get_pos(), %player.get_pos() );
	%ndir = VectorNormalize( %dir );

	%view = - mSin( mDegToRad( %this.get_dir() ) ) SPC mCos( mDegToRad( %this.get_dir() ) );

	%angle = mRadToDeg( mAcos( VectorDot( %view, %ndir ) ) );

	%cross = VectorDot( VectorCross( %ndir, %view ), "1 1 1" ); 

	if ( %cross < 0 )
		return -%angle;
	else
		return %angle;
}
#2
04/22/2006 (1:53 am)
It's hard to understand that without also seeing get_pos and get_dir. Can you post those also?
#3
04/22/2006 (2:05 am)
Sorry, they are just wrapper functions for getPosition and getRotation of the respective objects. I thought that would be fairly obvious. If it's still unclear I can dig those functions out, but my code is a bit more complex as my player object isn't a sprite but rather wraps a sprite, so there's quite a bit more to it. get_pos is simply the location "x y" (sprite.getPosition) and get_dir is an angle in degrees (sprite.getRotation), therefore I'm converting it to the normalised vector %view. I hope this makes sense.