Path Camera with mouse free-look?
by rlevine · in Torque Game Engine · 03/07/2007 (1:04 pm) · 0 replies
I initially posted this on the PathCamera resource page before realizing that probably wasn't the right place. Then mistakenly posted it in the public forums...woops. Hopefully this is in the right spot now.
I was able to get a PathCamera setup and flying through my level thanks to all the great posts on the subject. Since then, I've been trying to integrate a mouse-look mode so that when the path camera is stopped, I can look around with mouse. Here's how I got it working. This basically picks up where the initial setup of the pathcamera leaves off. See some of these posts for information on getting the pathCamera setup.
www.garagegames.com/mg/forums/result.thread.php?qt=30849
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9690
EDIT - removed example code from original post for clarity
There were quite a few steps to setting this up. All of the changes I made were in client/default.bind.cs, server/game.cs, pathcamera.h, pathcamera.cc and pathcamera.cs. This is perhaps not the best method of getting this done, and certainly not the only way, but it worked for me;)
First, I added two variables to pathCamera.h.
Then in pathCamera.cc I intialize them like so (outside of any member functions since these are static variables)
Next, in pathcamera.cc in the constructor (PathCamera::PathCamera()) expose those variables to console
Now, you have a choice here. I created a new action map called lookMap in default.bind.cs, and I push it to the stack when I create the player in game.cs, but alternatively, you could just add this into the default keybindings.
in default.bind.cs add the following:
Also, you may want to alter your toggleCamera function (also in default.bind.cs) to look like this:
Well, we haven't done anything to how the pathCamera actually works yet. Here's the meat and potatoes. In pathCamera.cc change the interpolateMat function to look like this:
You may also want to reset your pitch and yaw when you change states. I added this to my PathCamera::setState() function:
viola! You should now have a pathcamera that follows your path (if it worked before adding these changes) and looks left, right, up and down with the mouse. I added a getState accessor to the pathCamera class so I could compare the current state of the camera, then setup keybindings to toggle the pathcamera between forward and stop, and also mapped keys to make it go forwards and backwards on the path.
I struggled with this for a few days and flat out had to figure it out on my own. Hopefully, someone else will find this helpful.
-Gavin
I was able to get a PathCamera setup and flying through my level thanks to all the great posts on the subject. Since then, I've been trying to integrate a mouse-look mode so that when the path camera is stopped, I can look around with mouse. Here's how I got it working. This basically picks up where the initial setup of the pathcamera leaves off. See some of these posts for information on getting the pathCamera setup.
www.garagegames.com/mg/forums/result.thread.php?qt=30849
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9690
EDIT - removed example code from original post for clarity
There were quite a few steps to setting this up. All of the changes I made were in client/default.bind.cs, server/game.cs, pathcamera.h, pathcamera.cc and pathcamera.cs. This is perhaps not the best method of getting this done, and certainly not the only way, but it worked for me;)
First, I added two variables to pathCamera.h.
static F32 mLookPitch; static F32 mLookYaw;
Then in pathCamera.cc I intialize them like so (outside of any member functions since these are static variables)
// must initialize our static variables F32 PathCamera::mLookPitch = 0; F32 PathCamera::mLookYaw = 0;
Next, in pathcamera.cc in the constructor (PathCamera::PathCamera()) expose those variables to console
// expose our static vars to console so we can adjust with mouse
Con::addVariable("mvLookPitch", TypeF32, &mLookPitch);
Con::addVariable("mvLookYaw", TypeF32, &mLookYaw);Now, you have a choice here. I created a new action map called lookMap in default.bind.cs, and I push it to the stack when I create the player in game.cs, but alternatively, you could just add this into the default keybindings.
in default.bind.cs add the following:
if(isObject(lookMap))
lookMap.delete();
new ActionMap(lookMap);
function lookyaw(%val)
{
$mvLookYaw += getMouseAdjustAmount(%val);
}
function lookpitch(%val)
{
$mvLookPitch += getMouseAdjustAmount(%val);
}
function GameConnection::setPathCamBindings()
{
lookMap.bind( mouse, xaxis, lookyaw);
lookMap.bind( mouse, yaxis, lookpitch);
lookMap.push();
echo("lookMap KeyBindings Set"); // if you want feedback
}Also, you may want to alter your toggleCamera function (also in default.bind.cs) to look like this:
function toggleCamera(%val)
{
if (%val)
{
commandToServer('ToggleCamera');
// add the following to avoid problems when switching from your pathcamera back to free cam in the editor
lookMap.pop(); // get rid of our overridden keybindings
moveMap.push(); // push the default actionmap back on
}
}Well, we haven't done anything to how the pathCamera actually works yet. Here's the meat and potatoes. In pathCamera.cc change the interpolateMat function to look like this:
void PathCamera::interpolateMat(F32 pos,MatrixF* mat)
{
CameraSpline::Knot knot;
mSpline.value(pos - mNodeBase,&knot);
knot.mRotation.setMatrix(mat);
// Create an adjustment Quaternion based on our mouse inputs.
QuatF qRotAdjust(EulerF(-mLookPitch, 0.0f, -mLookYaw));
// put a break point here and make sure your pitch and yaw values are getting updated.
qRotAdjust.normalize();
QuatF qAdjusted; // Make a new QuatF to store our new rotation.
qAdjusted.mul(qRotAdjust, knot.mRotation); // order is important here!!
qAdjusted.normalize();
qAdjusted.setMatrix(mat); // set the mat with our adjusted rotation
mat->setPosition(knot.mPosition); // remember to setPosition (column3 is 0'd out by QuatF::setMatrix)
}You may also want to reset your pitch and yaw when you change states. I added this to my PathCamera::setState() function:
void PathCamera::setState(State s)
{
// reset our variables. should really only do this when we are changing mode to Stop,
// but no other modes are using it right now so this will save an if check.
mLookYaw = 0.0f;
mLookPitch = 0.0f;
mState = s;
setMaskBits(StateMask);
}viola! You should now have a pathcamera that follows your path (if it worked before adding these changes) and looks left, right, up and down with the mouse. I added a getState accessor to the pathCamera class so I could compare the current state of the camera, then setup keybindings to toggle the pathcamera between forward and stop, and also mapped keys to make it go forwards and backwards on the path.
I struggled with this for a few days and flat out had to figure it out on my own. Hopefully, someone else will find this helpful.
-Gavin
About the author