Game Development Community

camera distance changes

by Greg M · in Torque 3D Professional · 09/12/2011 (3:05 pm) · 6 replies

I am testing some different camera options and have run across an issue.
When starting the level, the camera appears at one distance.
After toggling first person and back, it appears to be at a different distance.

defining camera in gameCore.cs that the end of

function GameCore::onClientEnterGame(%game, %client)

with
// Make the camera overhead/offset
   if(isObject(%client.camera) && isObject(%client.player))  
   {
   %client.setCameraObject(%client.camera);
   %client.camera.setOrbitObject(%client.player, mDegToRad(35) @ " 0 0", 0, 20, 20, true, "0 0 1", true);
   %client.setFirstPerson(false);
   }

here are the results, before toggle
img585.imageshack.us/img585/9775/startingcamera.jpg
and after
img851.imageshack.us/img851/7004/aftertogglecamera.jpg
Am I missing something simple to get the camera to either set properly to start or keep the same distance after toggling first person view.

Another issue I am having is with the editor. with the cameraOrbitObject set got into editor with F11.
This usually places you in First person. then hit F11 again to play the level. Toggle the camera back to 3rd person and run around. the camera is no longer following the player object.

here is after editor mode - you can see the camera is no longer following the player and is also at a new distance from the player
img847.imageshack.us/img847/3053/aftereditor.jpg

#1
09/12/2011 (7:36 pm)
so far this is what has been done to avoid this but the delay is annoying.
// paste the following function into game/core/scripts/server/commands.cs script file
function serverCmdTogglePlayerView(%client)
{
   // Prevent camera toggling while the "hack" schedule is active
   if (isEventPending(%client.lockControlSchedule))
      return;

   if (%client.getCameraObject() != %client.camera)
   {
      %dist = 20;
      %client.setFirstPerson(false);
      %client.camera.setVelocity("0 0 0");
      %client.setControlObject(%client.camera);
      %client.setCameraObject(%client.camera);
      %client.camera.setOrbitObject(%client.player, mDegToRad(35) @ " 0 0", %dist, %dist, %dist, false, "0 0 1", false);
      %client.lockControlSchedule = %client.schedule(100, setControlObject, %client.player);
   }
   else
   {
      %client.player.setVelocity("0 0 0");
      %client.setFirstPerson(true);
   }
   clientCmdSyncEditorGui();
}

// edit game/scripts/client/default.bind.cs script file and change the function
function toggleFirstPerson(%val)
{
   if (%val)
   {
      ServerConnection.setFirstPerson(!ServerConnection.isFirstPerson());
   }
}

// to this
function toggleFirstPerson(%val)
{
   if (%val)
   {
//      ServerConnection.setFirstPerson(!ServerConnection.isFirstPerson());
      commandToServer('TogglePlayerView');
   }
}

// All done

and in gameCore.cs remove all previous edits and replace with
serverCmdTogglePlayerView(%client);

thanks to TRON on IRC for the quick fix/hack

also have the source change from www.garagegames.com/community/forums/viewthread/112553/1#comment-749203

EDIT: This change has also force the camera to behave after leaving editor :)
#2
09/18/2011 (12:51 pm)
@Greg - I think I might have this covered in my last post on the RTS Prototype blog. Try tracking the orbit distance using float values and see if it doesn't behave better.
#3
09/18/2011 (1:31 pm)
yes you did, and I have commented on the blog. Thanks for that.
now just setting up min.max distance limiters so you can't go to far back or through the terrain.

EDIT: not sure if this is the best way ( feel free to correct it), but here is what I have done to limit camera distance adjustment.
in /scripts/server/commands.cs I changed function serverCmdadjustCameraDistance()to look like this

// Server command that adjusts camera height
function serverCmdadjustCameraDistance(%client, %adjustment)
{
   // Take the current camera position (a vector)
   // Then add or subtract from the Z element, based on
   // the %adjustment value passed in
   if(%dist = %client.orbitDistance && (%client.orbitDistance < -20.0 || %client.orbitDistance > 20.0))
   {
      if(%client.orbitDistance < -20.0)
         %dist = %client.orbitDistance +0.1;
         
      else
         %dist = %client.orbitDistance -0.1;
   }
      
   else
   %dist = %client.orbitDistance + %adjustment;
   %client.camera.setOrbitObject(%client.player, mDegToRad(35) @ " 0 0", -20.0, 20.0, %dist, true);
   %client.orbitDistance = %dist;
   //echo(" -- orbitDistance : "@%dist);
}

This and the rest of my camera code has been adjusted to closer follow the RTS Tutorial located in the documentation here
docs.garagegames.com/torque-3d/official/index.html?content/documentation/Scripti...

and incase the docs are not updated to Richards revamp here is the blog with those changes.
www.garagegames.com/community/blogs/view/21247
#4
09/18/2011 (4:57 pm)
How's it "feel" now? I think you might want to store the camera's offset as well (I might be misinterpreting this field - I think this is the one you want) to store the current view angle (or orbital position?) so that it can be restored later - or you could try to calculate a position that is behind the player. I'd have to fiddle with it some to get it figured out.
#5
09/18/2011 (5:09 pm)
still tweaking angles and distances but it's much closer to what I want now. I does remember distance when toggling between 1st person and orbit mode. Also it still starts up in the wrong position and you have to toggle to 1st person and back to get it to set the proper defined distance. not too noticeable with the zoom working though, but still irritating.
Once I get a couple other things finished up I might write up my first ever blog. Now that's a scary thought.

Hope you didn't lose too much of your weekend for this.
#6
09/18/2011 (5:19 pm)
If you want it to persist through level changes you'll need to set and update it in your preferences (the $pref:: namespace). Then it'll persist through restarts, too. Just use that as your source for the %dist variable when spawning the player.

What bugs me is facing the wrong way when toggling back and forth. I think you can store the offset (using camera.getOffset()) and then use it when setting the camera back to orbit mode. Again, to get this to persist beyond the current level or on respawn you would have to store it somewhere persistent like the preferences.

As for the weekend, this IS play! Maybe I'm not all there....