Can I give the camra a world limit?
by Robert Carroll · in Torque Game Builder · 10/20/2009 (9:00 pm) · 12 replies
Hi, I got the mount to camera behavior, and was wondering if I could make it stop moving on the edges of my map? Like most games (top-down) like this TGB game Super Cell.
About the author
Stay Up all night playing PS3 ;) add me PSN: RCBASEBALL13.
#2
www.crazymonkeygames.com/Hover-Bot-Arena-2.html
If you go to the edge of the map the camera will stop scrolling.
Sorry if I don't get it im new to Torque
10/21/2009 (7:04 pm)
Sorry Im a n00b but I guess what your saying is to set its view but... What I ment was make it so the camera is like this gamewww.crazymonkeygames.com/Hover-Bot-Arena-2.html
If you go to the edge of the map the camera will stop scrolling.
Sorry if I don't get it im new to Torque
#3
10/21/2009 (8:09 pm)
Ehrlichmann's suggestion is right. Your camera view is separate from its limits. The "setViewLimitOn" will clamp your camera to stay within the limits specified. It will give you the behavior that "Hover Bot Arena 2" has.
#4
10/21/2009 (8:26 pm)
Heres What I got I feel so stupid I don't know where to put it. And this is such a easy code :(if (!isObject(CameraMountBehavior))
{
%template = new BehaviorTemplate(CameraMountBehavior);
%template.friendlyName = "Mount Camera to This";
%template.behaviorType = "Effects";
%template.description = "Mounts the camera to the current object.";
%template.addBehaviorField(xMount, "X location in target to mount to.", float, 0);
%template.addBehaviorField(yMount, "Y location in target to mount to.", float, 0);
%template.addBehaviorField(mountForce, "Strength of mounting. 0 = rigid", float, 0);
}
function CameraMountBehavior::onAddToScene(%this, %scenegraph)
{
%this.owner.schedule(1000, "scheduledMount");
}
function CameraMountBehavior::scheduledMount(%this)
{
echo ("It's on!");
sceneWindow2D.mount(%this.owner, %this.xMount, %this.yMount, %this.mountForce, true);
sceneWindow2D.setViewLimitOn(%x1, %y1, %x2, %y2);
}
#5
10/21/2009 (8:37 pm)
Ok, I got it, It had a parse error on the (C) info??? I deleted that and now it sorta works the camera won't move at all now so IDK whats going on.
#6
it will take you some experimenting, but eventually you'll find the values you want.
also, if you wanna update that dinamically, you need to recalculate that, so keep that in mind.
10/21/2009 (11:05 pm)
you need to set the values your camera will be clamped on... so, replace X1, X2 and so on with concrete values (say, your camera view is 100, 70 ... and you want it to stay in the center, you can enter values like -50, -35, 50, 35)...it will take you some experimenting, but eventually you'll find the values you want.
also, if you wanna update that dinamically, you need to recalculate that, so keep that in mind.
#7
10/21/2009 (11:57 pm)
Well what I have is a player that I want the camera on. Then I set the world limits of the player, copied them onto the camra code then saved it still dosn't move.
#8
- you mount the camera to the player
- you setup the controls for the player (make sure they work b4 mounting the camera if you want)
- make a level large enough, so the player can go out of scope if the camera doesnt moves with him, so you can try the camera movement in it
- if you dont wanna do the above, you can change the zoom factor, so the scope is reduced, and so you HAVE to move the camera if you wanna see the whole level.
- setup the camera limits to your likes
- mount the camera on the player and try your level.
check every step i've given you here (they are NOT in an exact order, but the way i wrote it, should work), and it should work with no problems.
the latest game i made with TGB uses this extensively, so i know that it will work for you too, if you do things right.
edit: one more thing, the setViewLimitOn, works better if you know your world (level) limits, since it will limit the camera to the whole level, but you can still manage what scope to show ever frame... you can do this by playing with the Zoom levels.
i hope you understand what i just said... cuz i have the impression it was not that clear.
10/22/2009 (1:24 am)
the camera wont move, unless you make the player move... here's the thing- you mount the camera to the player
- you setup the controls for the player (make sure they work b4 mounting the camera if you want)
- make a level large enough, so the player can go out of scope if the camera doesnt moves with him, so you can try the camera movement in it
- if you dont wanna do the above, you can change the zoom factor, so the scope is reduced, and so you HAVE to move the camera if you wanna see the whole level.
- setup the camera limits to your likes
- mount the camera on the player and try your level.
check every step i've given you here (they are NOT in an exact order, but the way i wrote it, should work), and it should work with no problems.
the latest game i made with TGB uses this extensively, so i know that it will work for you too, if you do things right.
edit: one more thing, the setViewLimitOn, works better if you know your world (level) limits, since it will limit the camera to the whole level, but you can still manage what scope to show ever frame... you can do this by playing with the Zoom levels.
i hope you understand what i just said... cuz i have the impression it was not that clear.
#9
====
I got the numbers off the world limits of my player which are the same as the map size.
10/22/2009 (1:54 am)
Ok, I understood :) but I did mount the camera to the player (Original)it worked fine then I put the code in it wouldn't move from its default pos.====
if (!isObject(CameraMountBehavior))
{
%template = new BehaviorTemplate(CameraMountBehavior);
%template.friendlyName = "Mount Camera to This";
%template.behaviorType = "Effects";
%template.description = "Mounts the camera to the current object.";
%template.addBehaviorField(xMount, "X location in target to mount to.", float, 0);
%template.addBehaviorField(yMount, "Y location in target to mount to.", float, 0);
%template.addBehaviorField(mountForce, "Strength of mounting. 0 = rigid", float, 0);
}
function CameraMountBehavior::onAddToScene(%this, %scenegraph)
{
%this.owner.schedule(1000, "scheduledMount");
}
function CameraMountBehavior::scheduledMount(%this)
{
echo ("It's on!");
sceneWindow2D.mount(%this.owner, %this.xMount, %this.yMount, %this.mountForce, true);
sceneWindow2D.setViewLimitOn(%x-951.658, %y-821.672, %x946.740, %y849.063);
}=======I got the numbers off the world limits of my player which are the same as the map size.
#10
it should be something like:
now, when i wanna mount the camera to the player, all i do is this:
i do it this way, becase for some reason (i dont remember it right now), if teh camera is mounted to the player, wont accept the limits given afterwards.
so, in your case, i assume, because i dont work with behaviours yet (i preffer to code my functions the OOP way) that your last function should be something like:
give it a try and lemme know...
10/22/2009 (2:03 pm)
i hve the slight impresion that thissceneWindow2D.setViewLimitOn(%x-951.658, %y-821.672, %x946.740, %y849.063);is wrong.
it should be something like:
sceneWindow2D.setViewLimitOn(-951.658, %-821.672, 946.740, 849.063);
now, when i wanna mount the camera to the player, all i do is this:
sceneWindow2D.setViewLimitOn(-91, -71, 91, 71); sceneWindow2D.mount($player);
i do it this way, becase for some reason (i dont remember it right now), if teh camera is mounted to the player, wont accept the limits given afterwards.
so, in your case, i assume, because i dont work with behaviours yet (i preffer to code my functions the OOP way) that your last function should be something like:
sceneWindow2D.setViewLimitOn(951.658, 821.672, 946.740, 849.063); sceneWindow2D.mount(%this.owner);
give it a try and lemme know...
#11
10/22/2009 (2:12 pm)
one more thing... are you sure that thats the numbers you wanna use for your camera limits?... the player world limits are usually (by default) WAY TOO BIG, so if you dont mind, check the level limits, not the player limits again (if you could take a pic of the level would be better), and modify the data accordingly.
#12
===============================
I used your code and it brings the camera it the bottom left corner of my map.
===============================
10/22/2009 (8:21 pm)
Heres a Screenshot of my level.===============================
I used your code and it brings the camera it the bottom left corner of my map.
===============================
Torque Owner Ehrlich
if its the former, by using this command:
will do the trick (you just need to input the correct values there).
if its the latter, there are a couple ways of achieving that... one of them is mounting the camera to the player, so when he moves, the camera moves (remember to use the command i just gave you... it will help).
another one, is to move the camera when the player moves, but w/o mounting... you need to make certain calculations for that, but it works something like:
- player moves
-- if player is away from the center of the screen, move camera acordingly
those are the easier ways... there are more methods, but they work acording to your design...
laters.