Game Development Community

Problem mounting camera to object

by Warren Simington · in Torque X 2D · 06/22/2009 (7:33 am) · 6 replies

I'm having an issue getting a camera to mount to an object in my TX2D project. I was following an example out of the "Complete Guide to Torque X" book and it says to enter the following code in the BeginRun() function in the game.cs file:

T2DAnimatedSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DAnimatedSprite>("player");
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("SceneCamera");
_camera.Mount(_player, "", true);

I have named my object "player", and the camera "SceneCamera". The problem is, when I run this code, it finds my "player" object, but keeps returning null for the SceneCamera object. After a little more troubleshooting, it appears that my camera name is not being retained after saving the scene and project in TX Builder. If I close the project in TX Builder and re-open it, I find that the camera name is blank.

Does anyone know of a workaround for this issue? Is there a way to get the camera name to "stick" in the TX Builder?

Thanks in advance!

#1
06/23/2009 (2:52 pm)
If you open up your txscene file, you should see a line near the top:
<Camera2D name="Camera">
I find if you change the properties, for instance X, in TXB and save the scene, it will write that into the txscene file, yet changing the name in TXB never seems to change it in the file.

So I would say if you're not going to define your own camera, but want to use the default, always use the name "Camera" in FindObject().
#2
06/23/2009 (3:37 pm)
Thanks, Scott. That did the trick.
#3
06/24/2009 (7:53 pm)
ok so i followed the steps mentioned above and everything works fine both objects are found, player moves but the Camera is not mounting to the player.
Any ideas?

heres my code if it helps:
protected override void BeginRun()
{
base.BeginRun();

// load our scene objects from XML. Torque X is designed to load game data from XML, but this is not strictly required;
// anything in an XML file can also be created manually in C# code. The SceneLoader is provided by TorqueGame and can be
// used to load and unload XML files.
SceneLoader.Load(@"data\levels\levelData.txscene");

//get a hold of the camera object
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");

// get a hold of the player object
T2DAnimatedSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DAnimatedSprite>("objPlayer");

_camera.Mount(_player, "", true);

}
#endregion

//======================================================
#region Private, protected, internal fields
static Game _myGame;

T2DSceneCamera _camera;
T2DAnimatedSprite _player;

#endregion
}
#4
06/25/2009 (7:05 pm)
The only difference that I can see between your code and mine is that I'm just using a T2DSceneObject for the player object (which I expect shouldn't make a difference), and that my "ownedbymount" flag is set to false when I mount the camera to the player object. My code is as follows:

T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
T2DSceneObject _player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("PlayerTank");
_camera.Mount(_player, "", false);
#5
06/25/2009 (10:51 pm)
@Randell: It's tough to tell just from your code listed there; it seems correct on the surface. Hopefully, you can view the Torque source code when debugging. If so, I would recommend putting a breakpoint on the line inside T2DSceneCamera.ProcessTick that reads
base.ProcessTick(move, elapsed);
Then enter the base ProcessTick method and step through one line at-a-time. The code at the start of that method checks if the object is mounted, and if so, follows or snaps to the position of its mount point. Looking at some values in the watch window, maybe you can find a clue about why it is not tracking the player.

If the camera is not mounting in the first place, maybe enter the _camera.Mount() method to see what is going on inside there.

Good luck.
#6
06/26/2009 (6:14 pm)
cool thanks guys Ill try this