Torque3D Top Down Static Camera w/ Player Control
by Brandon Russell · 08/11/2010 (3:46 pm) · 9 comments
Recently I need a camera system for a proof of concept game I am working on that allowed me to have a statically fixed camera with a top-down view orientation, BUT still give me full control of movements of the player down below. It would mimic an old school 2D top down game but in 3D of course :)
Using just the vanilla Torque3D new project, I will show you one way to achieve this as I have using just simple Torque Script.
First, open up Scripts\\Server\\GameCore.cs
Scroll down around line 710, and place this as the last line in the function GameCore::spawnPlayer:
This will force the server to call a function on the spawning client which we will utilize to setup our camera system and send the position of the spawn point. This will also be beneficial later on for executing anything we need to whenever we are triggered as spawning back up in game.
Finally, you need to select a file in Scripts\\Client to add our new MsgPlayerSpawn function too. I choose to place mine in Scripts\\Client\\serverConnection.cs, you may however place it where ever you like. Paste the following code in your selected client file:
What I am doing in the top few lines of the function is creating a manual transform. I am using the position (x,y,z) sent to us from the server but adding 15 to the Z (up) vector so that it places us up in the air above the position. The next 3 lines I'm locking our rotation to the X axis so that we will rotate up/down or Pitch, with an amount of 90 Degrees down. Then I'm telling the camera to orbit around this fixed point and be locked or no movement. The last 2 lines is simply making sure the camera object is properly set and that we still have control over the player entity.
Now you should be ready to go. Try it out for yourself ingame! The camera should be stationed up in the air above the player spawn point and never move. Plus you have full control over the player running around down below you.
Hope you enjoy!
Using just the vanilla Torque3D new project, I will show you one way to achieve this as I have using just simple Torque Script.
First, open up Scripts\\Server\\GameCore.cs
Scroll down around line 710, and place this as the last line in the function GameCore::spawnPlayer:
messageClient(%this, 'MsgPlayerSpawn', "", %spawnPoint.position);
This will force the server to call a function on the spawning client which we will utilize to setup our camera system and send the position of the spawn point. This will also be beneficial later on for executing anything we need to whenever we are triggered as spawning back up in game.
Finally, you need to select a file in Scripts\\Client to add our new MsgPlayerSpawn function too. I choose to place mine in Scripts\\Client\\serverConnection.cs, you may however place it where ever you like. Paste the following code in your selected client file:
addMessageCallback( 'MsgPlayerSpawn', handlePlayerSpawnMessage );
function handlePlayerSpawnMessage(%msgType, %msgString, %trans)
{
%pX = getword(%trans, 0);
%pY = getword(%trans, 1);
%pZ = getword(%trans, 2) + 15; // Change the 15 to whatever height above the player you wish
%rX = 1;
%rY = 0;
%rZ = 0;
%dd = mDegToRad(90);
ServerConnection.setFirstPerson(false);
LocalClientConnection.camera.setOrbitPoint(%pX SPC %pY SPC %pZ SPC %rX SPC %rY SPC %rZ SPC %dd, 0, 0, 0, "0 0 0", true);
LocalClientConnection.setCameraObject(LocalClientConnection.camera);
LocalClientConnection.setControlObject(LocalClientConnection.player);
}What I am doing in the top few lines of the function is creating a manual transform. I am using the position (x,y,z) sent to us from the server but adding 15 to the Z (up) vector so that it places us up in the air above the position. The next 3 lines I'm locking our rotation to the X axis so that we will rotate up/down or Pitch, with an amount of 90 Degrees down. Then I'm telling the camera to orbit around this fixed point and be locked or no movement. The last 2 lines is simply making sure the camera object is properly set and that we still have control over the player entity.
Now you should be ready to go. Try it out for yourself ingame! The camera should be stationed up in the air above the player spawn point and never move. Plus you have full control over the player running around down below you.
Hope you enjoy!
#2
It may be something obvious I am not getting :P
08/16/2010 (7:34 am)
With this change in place, pressing Alt+C to swap cameras causes a crash (I can't give the exact error I am at work, but will post it later), do you guys have any idea why?It may be something obvious I am not getting :P
#3
I am using the lastest Beta download of T3D and can not reproduce the crash. However, what is happening is "Alt C" is firing the ToggleCamera() which in turn is called on the server side inside core/scripts/server/command.cs (line: 36) serverCmdToggleCamera(%camera) ... it's filtering down into the first part of the if statement b\c we do have control of the client so it's changing the control to be the camera and not the player. If you wanted this feature you could in theory add the proper commands to that if statement to change the camera back from an OrbitPoint() camera type. And then add the lines back to the ELSE statement to set it back to the way it was before when it goes back to control of the player.
As far as the crash though, I'm not getting a crash so it could be due to an older version of T3D or custom code somewhere triggering it.
08/16/2010 (1:53 pm)
Trigger:I am using the lastest Beta download of T3D and can not reproduce the crash. However, what is happening is "Alt C" is firing the ToggleCamera() which in turn is called on the server side inside core/scripts/server/command.cs (line: 36) serverCmdToggleCamera(%camera) ... it's filtering down into the first part of the if statement b\c we do have control of the client so it's changing the control to be the camera and not the player. If you wanted this feature you could in theory add the proper commands to that if statement to change the camera back from an OrbitPoint() camera type. And then add the lines back to the ELSE statement to set it back to the way it was before when it goes back to control of the player.
As far as the crash though, I'm not getting a crash so it could be due to an older version of T3D or custom code somewhere triggering it.
#4
Ok sorry, bad explanation;
Right now I have a slightly different set-up;
I based it off of your code and I get the error using yours or mine, it's when you are in game mode (executed from Torsion say) then you hit F11 to go into the world editor, THEN press Alt+C, I get the error:
"SFX3DWorld::setListener - listener objects must not be registered for tracking"
I have a feeling what you said above will be related...?
08/16/2010 (4:53 pm)
Hey Brandon,Ok sorry, bad explanation;
Right now I have a slightly different set-up;
ServerConnection.setFirstPerson(false); LocalClientConnection.camera.setOrbitObject(LocalClientConnection.player, "0 0 0", "0", "0", "0", false, "0 -20 6", true) ; LocalClientConnection.setCameraObject(LocalClientConnection.camera);
I based it off of your code and I get the error using yours or mine, it's when you are in game mode (executed from Torsion say) then you hit F11 to go into the world editor, THEN press Alt+C, I get the error:
"SFX3DWorld::setListener - listener objects must not be registered for tracking"
I have a feeling what you said above will be related...?
#5
I'd bet to say it's some custom code or addon you have in your game vs. mine or the vanilla new project install. If I had to take a guess, SFX3DWorld sounds like some Sound object type of class (SFX being the general form for sound throughout the code). I guess that class is some form of a world sound manager? I'm not familiar with that and the normal T3D code so I'm guessing it's some custom code. I'd start by looking there or maybe researching what that particular error msg could be related to.
Hope that can at least help point you in a right direction to figuring it out.
08/16/2010 (6:41 pm)
I just tried out your code in a new project and it seems to work fine. Even following the steps you gave. No crashes. I'd bet to say it's some custom code or addon you have in your game vs. mine or the vanilla new project install. If I had to take a guess, SFX3DWorld sounds like some Sound object type of class (SFX being the general form for sound throughout the code). I guess that class is some form of a world sound manager? I'm not familiar with that and the normal T3D code so I'm guessing it's some custom code. I'd start by looking there or maybe researching what that particular error msg could be related to.
Hope that can at least help point you in a right direction to figuring it out.
#6
And of course, thank you for the original resource ;)
08/16/2010 (7:02 pm)
Ok no worries thanks for your help, I haven't tried it on a fresh install, and I have been playing with the source in various places, I will do a fresh install!And of course, thank you for the original resource ;)
#7
09/01/2010 (3:50 pm)
this is pretty cool. The camera remains static, so would be good to make it so that it follows the player keeping it centered.
#9
ive seen lots of resources and we really appreciate all this, but adding some pics of the actual resource outcome will be even greater.
thanks again and keep up the good work.
09/18/2010 (8:53 pm)
pics will be nice aswell,ive seen lots of resources and we really appreciate all this, but adding some pics of the actual resource outcome will be even greater.
thanks again and keep up the good work.

Torque Owner Alex Mault
Slopeside Studios