Game Development Community

Camera Not Working

by C.Eric Middleton · in Torque X 2D · 04/14/2010 (9:40 pm) · 5 replies

I have been working on a game that up until now has only had one scene, and everything was fine. When my team and I went to add a splash screen and then transition to the scene we had, the camera broke. It will not mount to anything any more. Here is how I am trying to mount it:

List<ISceneContainerObject> AllObjects = new List<ISceneContainerObject>();
T2DSceneGraph mySceneGraph = (T2DSceneGraph)TorqueObjectDatabase.Instance.FindObject("MyDefaultSceneGraph");
mySceneGraph.FindObjects(new Vector2(0.0f, 0.0f), 10000.0f, TorqueObjectType.AllObjects, (uint)0xFFFFFFFF, AllObjects);

foreach (ISceneContainerObject obj in AllObjects)
{
T2DSceneObject CurrentObject = obj as T2DSceneObject;
if (obj is T2DSceneObject)
{
if (CurrentObject.TestObjectType(TorqueObjectDatabase.Instance.GetObjectType("followBox")))
{
camera.Dismount();
followBox = CurrentObject;
camera.Mount(followBox, "", false);
if (camera.IsMounted)
{
int i = 0;
}
camera.FarDistance = 10000.0f;
}
}
}

The variables are define in the private fields like this:
//Camera Control Variables
static T2DSceneCamera camera = new T2DSceneCamera();
static T2DSceneObject followBox;
static Vector2 followBoxPos = new Vector2(0.0f, 0.0f);

I'm trying to mount the camera to an object in the scene( the followBox) which is just a blank scene object with a velocity. Now, the camera won't mount to the box at all and the if statement never executes (I place a break in there to test the camera mounting);

Does anyone know what the problem is?

#1
04/14/2010 (10:47 pm)
I'd extensively check to make sure that the camera I'm using is the same one that is currently being used for the rendering. If you loaded a second scene, maybe its a different camera. Maybe check the XML for the "name" of the camera, and when debugging, check if the name of the camera you are using is the current scene camera that you were looking for.
#2
04/15/2010 (4:43 am)
There's a TXB bug that doesn't allow you to change the name of your camera object. So no matter what you type in the box...your camera is always called "Camera." That's one possibility.

#3
04/15/2010 (9:34 am)
You have two scenes loaded at the same time? (rather than unloading one scene and then loading the next).

The first thing that springs to mind is that you are getting the wrong scenegraph and/or objects have been added to the wrong scenegraph when the scene is loaded. You say that the if statement doesn't execute - so basically it's not finding the follow box. Or in other words the follow box is not in the scenegraph instance you retrieved from the database. TX gets a bit confused when you load multiple scenes at the same time and needs a little help...

By default all scenegraphs are named "MyDefaultSceneGraph". But when you ask the database for that it will just return the first one it comes across with that name - which may or may not be the one you are after. A second problem is that when objects in a scene are loaded they will grab the first scenegraph the database happens to give them - which again, might not be the right one.


Solution Part 1: as soon as you load a scene always rename the scenegraph to something meaningful right away. This way you will be able to get the right scenegraph instance later.

TorqueSceneData sceneData = Game.Instance.SceneLoader.Load(filename);
T2DSceneGraph sceneGraph = (T2DSceneGraph)sceneData.SceneData.Find(delegate(object obj) { return obj is T2DSceneGraph; });
sceneGraph.Name = "SomethingMeaningful";


Solution Part 2: as soon as you've loaded a scene, go through it and make sure all the objects are in the right scenegraph.

foreach (object obj in sceneData.Objects)
{
    T2DSceneObject sceneObj = obj as T2DSceneObject;

    if (sceneObj != null)
    {
        sceneObj.SceneGraph = sceneGraph;
    }
}

#4
04/15/2010 (9:35 am)
Also, as the other guys said, check the camera is the correct one. You can rename it at the same time you rename the scenegraph. Oh, and set it's scenegraph while you're at it as well :)

T2DSceneCamera camera = (T2DSceneCamera)sceneData.SceneData.Find(delegate(object obj) { return obj is T2DSceneCamera; });
camera.Name = "MeaningfulCameraName";
camera.SceneGraph = sceneGraph;

Edit: You may also need to set the camera as the one to be used by the GUISceneview as a single sceneview can only have one active camera.
#5
04/15/2010 (1:42 pm)
I managed to fix it by setting the setting the camera to the default camera in the scene graph. Thanks for the help!