Game Development Community

Facing another player/object, Redux

by Novack · in Torque Game Engine · 05/25/2009 (9:28 pm) · 9 replies

Hi, inhabitants of the double G hills! Im in need of a hand here.

Found this resource to face an object by rotating the player around the Z axis, commonly known as Yaw.

It works like a charm for Yaw, but I need to adjust the Pitch as well, that is, to rotate the Player/Camera around the X Axis. Hence, my initial mod over the resource, denoted by ***1***, surprisingly not working (surprisingly for me, thats it ;).

After some extensive reading, I tried a second approach, by using MatrixCreateFromEuler() (denoted in the code below as ***2***) which neither work, and in fact produced the same effect as with approach 1: The Pitch rotation not only doesnt work, but also affects the Yaw (!?)

So I have at this point two doubts:
  • The math part, on how Im getting the Pitch angle (*** Critical Point A ***)
  • The whole thing of setting the Pitch not working for some alien reason
function Player::lookAtTarget(%this, %target)
{
   // This is not the problem, but commented out for simplicity
   // %target.eyePos = getWords(%target.getEyeTransform(), 0, 2);
   // %this.eyePos = getWords(%this.getEyeTransform(), 0, 2);   
   // %forward = VectorSub(%target.eyePos, %this.eyePos);

   %forward = VectorSub(%target.getPosition(), %this.getPosition()); 
   
   %x = getWord(%forward, 0);
   %y = getWord(%forward, 1);
   %z = getWord(%forward, 2);
   
   %angleYaw = -mAtan(-%x, %y);
   %anglePitch = -mAtan(%z, mSqrt(%x*%x + %y*%y)); // *** Critical Point A ***
                                                   // Im asuming this is correct,
                                                   // for getting the Pitch angle, 
                                                   // salvaged it from other resource
   
   %tYaw = %this.getPosition();
   %tYaw = %tYaw SPC "0 0 1" SPC %angleYaw;
   %this.setTransform(%tYaw);

   // ***1***
   //%tPitch = %this.getPosition();
   //%tPitch = %tPitch SPC "1 0 0" SPC %anglePitch;
   //%this.setTransform(%tPitch);

   // ***2***   
   //%tmpM = MatrixCreateFromEuler(%anglePitch SPC "0" SPC %angleYaw); 
   //%tFinal = getWords(%this.getPosition(), 0, 2) SPC GetWords(%tmpM, 3, 6);     
   //%client = %this.getControllingClient();
   //%client.camera.setTransform(%tFinal); 
}

Thanks in advance for any math genious (or not), that can visualize my mistake.

#1
05/25/2009 (9:46 pm)
Good luck on Pitch... as far as 'ControlObject' is concerned, from what I have read, it is impossible to do through script alone...
#2
05/25/2009 (9:54 pm)
Mhh. Do you remember where did you read that? If I can confirm the hint, I would start working C++ side... What a hassle.
#3
05/25/2009 (11:56 pm)
It seems to remember that the player class is build to always have its Z up.

If that's really the case, you can't rotate the player around its X axis.

For the camera, it is possible with the advanced camera resource (I tested it). Don't know with stock TGE camera code.

Nicolas Buquet
www.buquet-net.com/cv/
#4
05/26/2009 (12:29 am)
I never tried using this for players, but you may derive some use out of it, if only to tell you what rotation you need.
function pointToPos(%posOne, %posTwo)
{
    // sub the two positions so we get a vector pointing from the origin in the
    // direction we want our object to face
    %vec = VectorSub(%posTwo, %posOne);
	
    // pull the values out of the vector
    %x = firstWord(%vec);
    %y = getWord(%vec, 1);
    %z = getWord(%vec, 2);
	
    // this finds the distance from origin to our point
    %len = vectorLen(%vec);
	
    // -------- X
    // given the rise and length of our vector this will give us the angle in radians
    %rotAngleX = mATan(%z, %len);
	
    // -------- Z
    // get the angle for the z axis
    %rotAngleZ = mATan(%x, %y);
	
    // create 2 matrices, one for the z rotation, the other for the x rotation
    %matrix = MatrixCreateFromEuler("0 0" SPC %rotAngleZ * -1);
    %matrix2 = MatrixCreateFromEuler(%rotAngleX SPC "0 0");
	
    // now multiply them together so we end up with the rotation we want
    %finalMat = MatrixMultiply(%matrix, %matrix2);
	
    // we're done, send the proper numbers back
    return getWords(%finalMat, 3, 6);
}
#5
05/26/2009 (5:09 am)
Thanks for the info Nicholas. I dont completely follow what you say though, what you mean by always have its Z up?

@Michael, Interesting, much appreciated, I will test that today.

#6
05/26/2009 (6:37 am)
there's a few places in the C code which make the assumption (and enforce it) that player objects are always vertical. ie, that the player's local Z axis is parallel to the world's Z axis.

eg, try setting the transform of your player to 90-degrees around X:
%playerObj.setTransform("0 0 0 1 0 0" SPC mDegToRad(90))
you'll notice that nothing happens.

i think overcoming this is a fair bit of work.
you might consider using the flying vehicle as your player class instead,
although i'm not sure what other strings are attached to that.
#7
05/26/2009 (9:17 am)
Orion expressed it better than me.

There was a resource about quaternions who seems to allow player not to be always vertical : www.garagegames.com/community/resources/view/16149

Hope it can help.

Nicolas Buquet
www.buquet-net.com/cv/
#8
05/26/2009 (3:01 pm)
Again, thank you guys. I will consider carefully what to do.
#9
11/19/2009 (3:27 am)
how i can make that the player face to a mouse click or mouse position?, i tried some options without lucky... ex. when click on the terrain, you dont have a %targetObject

i have captured the onMouseClick from the playgui and works fine (left and rigth), like a RTS