Game Development Community

Using a vector to change player facing

by Walter Yoon · in Torque Game Engine · 03/09/2006 (9:02 pm) · 4 replies

Apologies if this is a simple question, but I've had no luck searching on my own and I'm really at the end of my rope.

Within a weapon script, I want to set the player's facing to match a vector.

Specifically, I have the player object %player and a target object %target.

I then determine the direction vector between the two (%vector) using
VectorNormalize( VectorSub( %target.getPosition(), %player.getPosition() ) );

Now, how do I use %vector with %player.setTransform() to make the player face in the direction of the vector? More specifically, how do I convert the 3-float %vector into a 4-float value that is compatible with setTransform()?

#1
03/10/2006 (12:00 am)
Not sure if this will help, but I wrote a function to face the player camera in a certain direction (given point A and Point B), which uses MathUtils::createOrientFromDir(vec); which I think does what you need... create a full Matrix out of a direction vector... maybe you can use the concept for your needs and put it into your player class or see if the function is exposed to scripting...
// game/camera.h:
void lookAt(Point3F &dir, const Point3F &pos, const Point3F &lookAtPos);

// game/camera.cc:
ConsoleMethod( Camera, lookAt, void, 4, 4, "")
{
   Point3F dir, pos, lookAtPos;
   Camera *camObj = (Camera *) object;
   camObj->setControlDirty();
   
    dSscanf(argv[2],"%f %f %f",
       &pos.x,&pos.y,&pos.z);
    dSscanf(argv[3],"%f %f %f",
       &lookAtPos.x,&lookAtPos.y,&lookAtPos.z);
   
   camObj->lookAt(dir, pos, lookAtPos);

}
void Camera::lookAt(Point3F &dir, const Point3F &pos, const Point3F &lookAtPos)
{
    Point3F vec = lookAtPos - pos;
    F32 yawAng, pitchAng;
    MatrixF m = MathUtils::createOrientFromDir(vec);
    m.setColumn(3,pos);
    setTransform(m);
}

From script:
%client.camera.lookAt(%playerPos, %targetPos);
cheers,
beffy
#2
03/10/2006 (5:25 am)
You could use my rotateShape resource to do this. It's at :

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9480

First get the bearing of the target, as in

%transform = %player.getTransform();
   %x1 = getWord(%transform, 0);
   %y1 = getWord(%transform, 1);
   %targetTransform = %target.getTransform();
   %x2 = getWord(%targetTransform, 0);
   %y2 = getWord(%targetTransform, 1);
   %bearing = mAtan(%x2-%x1, %y2-%y1);

Then use the rotateShape method to turn the bot towards the target:

rotateShape(%player,%bearing);

rotateShape has a complete set of options for controlling turning rate and stepsize and for scheduling completion routines so there is a lot of advantage in it.
#3
03/10/2006 (9:24 am)
Thanks for the help everyone; I didn't use any of the posted code, but reading it all enabled me to figure this out. I ended up exposing getAnglesFromVector() in mathUtils.cc as a console function, and using it on the vector to get a yaw value that I could then use with setTransform().
#4
03/10/2006 (9:38 am)
Be aware that to get the rotation you want using setTransform(), you will need to do it the way its done in rotateShape (or use rotateShape) because of the internal gimbal issues that arise. Try it and you will see what I mean. Or save your self the time of finding out the hard way, which is what happened to me when I tried to do this. This problem is precisely the motivation behind the rotateShape resource.