Game Development Community

Help me to rotate pathcamera toward player?

by kingkong · in Torque Game Engine · 05/26/2006 (1:49 am) · 3 replies

I want to use pathcamera for showing player with different views in some cases.
so each path has only one marker like below.

new Path(View1) {
isLooping = "0";

new Marker(CamPos1) {
position = "0 -400 50";
rotation = "1 0 0 0";
scale = "1 1 1";
seqNum = "1";
type = "Normal";
msToNext = "1000";
smoothingType = "Spline";
};
};
new Path(View2) {
isLooping = "0";

new Marker(CamPos2) {
position = "50 -400 50";
rotation = "1 0 0 0";
scale = "1 1 1";
seqNum = "1";
type = "Normal";
msToNext = "1000";
smoothingType = "Spline";
};
};


and then before i call pathCamera.followPath("MissionGroup/View1"), i need to rotate CamPos1 toward player.. but i don't know how to rotate the Marker "CamPos1" toward player or even toward some objects.
i'm a 3d newbie. i don't understand vectors and matrices yet enough... i'm studying...

anybody can help?

thanks.

#1
05/26/2006 (3:31 am)
I may be able to help. i wrote a little function to do just that. But mine is limited to XY targeting.

$PI = 3.14159265358979323846;


function InstantAimTo(%object,%location,%pos){
	
	if (!%pos)
		%pos=%object.getposition();
		
	%xform = %object.gettransform();
	%Rdirection= getword(%xform,5);
	%angle= getword(%xform,6);
	
	%forward = GetObjectToLocAngle(%object,%location);
	%right = GetObjectToRightAngle(%object,%location);
	%left = GetObjectToLeftAngle(%object,%location);

	if (%right >= %left){
		%dir = "left";
		}

	else{
		%dir = "right";
	}
	
	
	if (%dir $= "left")
		%forward=%forward*-1;
	
		if (%Rdirection <0){
			%angle =(2*$PI-%angle);
			}
			
//		echo("result : "@%angle+%forward);
		%result = %angle+%forward;
		%newXform = %pos SPC "0 0 1" SPC %result;
		%object.settransform(%newxform);
}


function GetObjectToLocAngle(%sourceObj,%location){
	%eyeVec =VectorNormalize(%sourceObj.getforwardvector()); 
	%toObjVec =VectorSub(%location,%sourceObj.getPosition());
	%toObjVec = setword(%toObjVec,2,"0");
	%dot =VectorDot(%eyeVec,VectorNormalize(%toObjVec));
	%rad =mAcos(%dot);
	return %rad;
}

function GetObjectToRightAngle(%sourceObj,%location){
	%eyeVec =VectorNormalize(%sourceObj.getrightvector()); 
	%toObjVec =VectorSub(%location,%sourceObj.getPosition());
	%toObjVec = setword(%toObjVec,2,"0");
	%dot =VectorDot(%eyeVec,VectorNormalize(%toObjVec));
	%rad =mAcos(%dot);
	return %rad;
}

function GetObjectToLeftAngle(%sourceObj,%location){
	%eyeVec =VectorNormalize(%sourceObj.getleftvector()); 
	%toObjVec =VectorSub(%location,%sourceObj.getPosition());
	%toObjVec = setword(%toObjVec,2,"0");
	%dot =VectorDot(%eyeVec,VectorNormalize(%toObjVec));
	%rad =mAcos(%dot);
	return %rad;
}


this requires two new console methods unless you want to figure out the Right and left vectors some other way.....


in sceneobject.cc, right below getforwardvector, i added these
ConsoleMethod( SceneObject, getRightVector, const char*, 2, 2, "Returns a vector indicating the direction this object is facing.")
{
   char *returnBuffer = Con::getReturnBuffer(256);
   const MatrixF& mat = object->getTransform();
   Point3F dir;
   mat.getColumn(0,&dir);
   dSprintf(returnBuffer,256,"%g %g %g",dir.x,dir.y,dir.z);
   return returnBuffer;
}
ConsoleMethod( SceneObject, getLeftVector, const char*, 2, 2, "Returns Left Vector.")
{
   char *returnBuffer = Con::getReturnBuffer(256);
   const MatrixF& mat = object->getTransform();

	//Get UpVector
   Point3F UpVector;
   mat.getColumn(2,&UpVector);

   //Get ForwardVector
   Point3F ForwardVector;
   mat.getColumn(1,&ForwardVector);
   
	Point3F LeftVector;
	mCross(UpVector,ForwardVector,&LeftVector);

	dSprintf(returnBuffer,256,"%g %g %g",LeftVector.x,LeftVector.y,LeftVector.z);
   return returnBuffer;
}
ConsoleMethod( SceneObject, getUpVector, const char*, 2, 2, "Returns a vector indicating the direction this object is facing.")
{
   char *returnBuffer = Con::getReturnBuffer(256);
   const MatrixF& mat = object->getTransform();
   Point3F dir;
   mat.getColumn(2,&dir);
   dSprintf(returnBuffer,256,"%g %g %g",dir.x,dir.y,dir.z);
   return returnBuffer;
}


hmm how to use? it's pretty simple


InstantAimTo(%object,%location,%pos){


%object is the object you're applying the rotation to, duh
%location is the location you want it to aim at, probably something like the player
%pos is optional. but you can move the %object somewhere else while it's doing the transform, if not it uses the object current position.


InstantAimTo(%marker,%player.getposition());

should pretty much do what you want. It will instantly point to the player. But as i mentioned, it is has limited rotation.

For paths.. i think it's perfect. so you shouldn't have any issues. Do the paths determine where the camera faces when using pathcamera?
#2
05/26/2006 (12:50 pm)
@Ramen

it really helps me to understand vectors and matrices much more..
it works perfectly. thanks!!

for getting left, right, up vectors, i use script functions instead of making console methods.
i found script function to get them ====>echoVectors


// ------------------------------------------------------------------------------------
function GetRightVector(%obj)
{
// getting Angular Axis
%aa = getWords(%obj.getTransform(), 3);
%tmat = VectorOrthoBasis(%aa);
%rv = getWords(%tmat, 0, 2);

return %rv;
}

// ------------------------------------------------------------------------------------
function GetLeftVector(%obj)
{
// getting Angular Axis
%aa = getWords(%obj.getTransform(), 3);

%tmat = VectorOrthoBasis(%aa);
%lv = "-" @ getWords(%tmat, 0, 2);

return %lv;
}

// ------------------------------------------------------------------------------------
function GetUpVector(%obj)
{
// getting Angular Axis
%aa = getWords(%obj.getTransform(), 3);

%tmat = VectorOrthoBasis(%aa);
%uv = getWords(%tmat, 6,8);

return %uv;
}

Thank you so much for your kindness again!
#3
05/26/2006 (10:14 pm)
The tricky part about getting angles from objects is it returns the actual difference in angles, like it may say the different is 45 degrees... but is the rotation 45 degrees or 315?

My little big of code compares the right and left angles to determine which direction to actually rotate to match up. I was having hell for awhile getting the objects to point till i actually did some thinking and did a compare of the left and right vectors. there may be a quicker way to do it, but this is all just prototype coding.