Camera mounting
by Adam Beaumont · in Torque Game Builder · 11/12/2007 (5:48 am) · 13 replies
I've got a camera mounted to my player inside a tile layer and its working ok, in that the camera is following my guy around when he moves. What I want to do though is the have the camera not move until the player moves outside of an area in the middle of the screen. There must (!?) be a way to do it just not sure how - anyone know?
#2
I'm sure in torque2d there was something that allowed you to specify this - did that not make it into TGB?
11/17/2007 (6:37 pm)
Thanks for the reply. I did think about doing something like this (altho I was going to test the player coords every so often, using world limits as you suggest might be better) but I was worried it was going to be a bit cumbersome and slow and give me a jerky camera. I have to admit I didn't try it so maybe its worth giving it a go.I'm sure in torque2d there was something that allowed you to specify this - did that not make it into TGB?
#3
One of these two ways should allow the camera to "scroll" rather than jump to the new positon.
I'm not sure what you mean by torque2d versus TGB? This isn't something I know from some hidden documentation its just my best guess, if that answers your question.
11/17/2007 (7:40 pm)
I may be able to grab the camera object and perform a moveto on it, so it scrolls to the new positon. However, I have a feeling the position of a camera object may not be the same as that of normal sceneobjects ... If that doesn't work construct your own cameramoveto function which does uses a repeated schedule to incriment the camera position to the desired target.One of these two ways should allow the camera to "scroll" rather than jump to the new positon.
I'm not sure what you mean by torque2d versus TGB? This isn't something I know from some hidden documentation its just my best guess, if that answers your question.
#4
Seems to work ok but clearly have to be very careful as even adding in the slightest computation to on update really kills the performance.
My comment about torque2d was that it (torque2d) appears to have been the forerunner for TGB and in torque 2d I am sure this behaviour came for free. I had a quick look through some very old code that i had today but couldn't find it, so maybe I just dreamt it!!!!!
Thanks for the help...
11/18/2007 (4:11 am)
Ok so I did this by changing the behaviour I was using to implement movement - in the on update function I added something to get the world coords, convert them to screen coords, then check them against an invisible box in the middle of the screen. If outside the box (and not already mounted) I mount the camera, if in the box (and not already unmounted) then I dismount the camera.Seems to work ok but clearly have to be very careful as even adding in the slightest computation to on update really kills the performance.
My comment about torque2d was that it (torque2d) appears to have been the forerunner for TGB and in torque 2d I am sure this behaviour came for free. I had a quick look through some very old code that i had today but couldn't find it, so maybe I just dreamt it!!!!!
Thanks for the help...
#5
I'm not farmiliar with this being a "feature" in the past, but I did find a cool camera feature which prevents it from looking outside of your levels edges while mounted to a moving object, like the player. Although I couldn't find it again looking just now...
11/18/2007 (8:48 am)
Hey, mounting / unmounting the camera is a pretty nice idea, especially since you can specify a mount "strength" this should look nice. I'm not farmiliar with this being a "feature" in the past, but I did find a cool camera feature which prevents it from looking outside of your levels edges while mounted to a moving object, like the player. Although I couldn't find it again looking just now...
#6
If you get the limits of your level, you can put those values into the above line of code.
11/18/2007 (9:04 am)
James, to limit the camera from looking outside of the level edges, you need to set the limits on the camera.sceneWindow2D.setViewLimitOn(%theMinX, %theMinY, %theMaxX, %theMaxY);
If you get the limits of your level, you can put those values into the above line of code.
#7
11/18/2007 (2:47 pm)
Can you get the level edges dynamically in script or do you have to set up a script object in each level to specify them?
#8
However hardcoding them in script or overlaying a sceneobject or whatever would work fine.
11/18/2007 (3:39 pm)
There isn't really an "edge" to a level, it just goes on and on. So you do need to specify it somewhere to use setViewLimitOn. I personal did this by by defining the player's world limits to be the edge of my map.sceneWindow2D.mount($player,"0 0",5,1);
%theWorldLimit = $player.getWorldLimit();
%theMinX = getWord(%theWorldLimit, 1);
%theMinY = getWord(%theWorldLimit, 2);
%theMaxX = getWord(%theWorldLimit, 3);
%theMaxY = getWord(%theWorldLimit, 4);
sceneWindow2D.setViewLimitOn(%theMinX, %theMinY, %theMaxX, %theMaxY);However hardcoding them in script or overlaying a sceneobject or whatever would work fine.
#9
Here's what I do. Using the editor, I set specific world limits for each object I am interested in using. For example, I did two things. I set my map layer world limits and my player's world limits. These happen to be the SAME exact values in my case, but they don't have to be. As long as you specifically define your world limits, you can then extract good values.
I based this behavior on the already available MountCamera behavior. I just added the ability to select an object's world limits.
Here is the important section from the Behavior. You will see it looks very much like the code from James. For the behavior, I can select which object contains the world limits. This has been defined in %this.limitObject and is selected from a behavior dropdown. This code goes in the scheduleMount function.
I added the code below to the first part of the behavior code as a way to select a specific object from a dropdown.
11/18/2007 (4:09 pm)
I did exactly as James shows. I actually created a behavior to mount the camera to an object and to allow you to set the camera world limits to another object's world limits. I calculated the world limits using the getWord functions like James described.Here's what I do. Using the editor, I set specific world limits for each object I am interested in using. For example, I did two things. I set my map layer world limits and my player's world limits. These happen to be the SAME exact values in my case, but they don't have to be. As long as you specifically define your world limits, you can then extract good values.
I based this behavior on the already available MountCamera behavior. I just added the ability to select an object's world limits.
Here is the important section from the Behavior. You will see it looks very much like the code from James. For the behavior, I can select which object contains the world limits. This has been defined in %this.limitObject and is selected from a behavior dropdown. This code goes in the scheduleMount function.
sceneWindow2D.mount(%this.owner, %this.xMount, %this.yMount, %this.mountForce, true); %theWorldLimit = %this.limitObject.getWorldLimit(); %theMinX = getWord(%theWorldLimit, 1); %theMinY = getWord(%theWorldLimit, 2); %theMaxX = getWord(%theWorldLimit, 3); %theMaxY = getWord(%theWorldLimit, 4); sceneWindow2D.setViewLimitOn(%theMinX, %theMinY, %theMaxX, %theMaxY);
I added the code below to the first part of the behavior code as a way to select a specific object from a dropdown.
%template.addBehaviorField(limitObject, "The object's world limit to use", object, "", t2dSceneObject);
#10
I get the world limits from the first tile layer - the thinking being that the level designer can assign world limits to that and then I use code to determine what they are and lock the camera to them. Works quite well:
Bit of a hack and might want to tidy up the first bit to check if all the stuff exists but looks good. Thanks again.
11/19/2007 (5:01 am)
Cool - got it working really nicely now.I get the world limits from the first tile layer - the thinking being that the level designer can assign world limits to that and then I use code to determine what they are and lock the camera to them. Works quite well:
// see if we can get the tile map and set the view limits %scenegraph = sceneWindow2D.getSceneGraph(); %tileMap = %scenegraph.getGlobalTileMap(); %tileLayer = %tileMap.getTileLayer(0); // set some view limits on the camera %theWorldLimit = %tileLayer.getWorldLimit(); %theMinX = getWord(%theWorldLimit, 1); %theMinY = getWord(%theWorldLimit, 2); %theMaxX = getWord(%theWorldLimit, 3); %theMaxY = getWord(%theWorldLimit, 4); sceneWindow2D.setViewLimitOn(%theMinX, %theMinY, %theMaxX, %theMaxY);
Bit of a hack and might want to tidy up the first bit to check if all the stuff exists but looks good. Thanks again.
#11
Don't mount the camera to the player.
Mount the camera to an object and make the object smaller than the camera view. Move the object toward the player (move to behaviour will do it).
Set the object world limits to control the camera limits.
Anyone see any problems with this? It functions well although might not be the most efficient way.
Mark
12/28/2007 (6:31 am)
Ok you got it working but i want to suggest a low tech version.Don't mount the camera to the player.
Mount the camera to an object and make the object smaller than the camera view. Move the object toward the player (move to behaviour will do it).
Set the object world limits to control the camera limits.
Anyone see any problems with this? It functions well although might not be the most efficient way.
Mark
#12
05/12/2008 (3:58 pm)
I tried Steven's method, but the Object dropdown didn't have any objects in it. What am I missing?
#13
06/06/2008 (8:43 am)
You probably need to give the object a name in the "Scripting" seciton
Associate James Ford
Sickhead Games
Just one possible solution.
Actually heres another... give your player a behavior so you can perform a check on the player's positon each tick (look at some of the sample behaviors for how this can be done). Also store the "box" within the behavior and do the same thing as above, but with this method you still have worldlimits to use for other things.