Game Development Community

Camera movement limits.

by Karl Jahnke · in Torque Game Builder · 10/10/2007 (5:57 pm) · 3 replies

I have a sprite that the camera is linked to (via the mini platformer tut example).

What I'm interested in is to have the camera limited in movment top/bottom at a certain point. I still want it to move up and down, but say at -100 Y, i want the camera to stop following the sprite in Y untill the sprite reaches the "limit" again.

I am a Scripting noob, Im more and artist then a coder. Any help is greatly appreicated.

#1
10/10/2007 (6:49 pm)
The only thing I could think of with regards to this is having an onUpdate function that checks to see if the camera lies above/below point Y, then makes sure it does not go above that point.

if (%camera.getPositionY() > %maxPointY)
    %camera.setPositionY(%maxPointY);
else if (%camera.getPositionY() < %minPointY)
    %camera.setPositionY(%minPointY);

Where the %camera variable refers to the current camera within the t2dSceneWindow.
#2
10/10/2007 (9:26 pm)
You need to do two steps. First, you need to mount the camera to the object. Then you need to set up the camera's view limit.

You have already done the first. If you followed the tutorial I am thinking of, you should have the following line in your player.cs file inside playerClass::onLevelLoaded:

sceneWindow2D.mount($pGuy, "0 0", %force, true);

This mounts the camera onto $pGuy (the player). As you noticed, this will move the camera, but the viewable area will display too much outside of the actual tilelayer. The next step is to limit the camera's view. So, add the following lines:

%theWorldLimit = $pGuy.getWorldLimit();
    %theMinX = getWord(%theWorldLimit, 1);
    %theMinY = getWord(%theWorldLimit, 2);
    %theMaxX = getWord(%theWorldLimit, 3);
    %theMaxY = getWord(%theWorldLimit, 4);
    sceneWindow2D.setViewLimitOn(%theMinX, %theMinY, %theMaxX, %theMaxY);

This will limit the camera's view to the player's world limit. This means you need to be sure you set the player's world limits, but the tutorial should have had you do that. The first line gets the player's world limit which contains several items. Items 1, 2, 3, and 4 of the world limit describe the x/y values you want to use. These are read using the 'getWord' call.

Now save the file and run your game.

EDIT: And you, of course, can set the limits to whatever you want. I just used the player as an example.
#3
10/11/2007 (9:53 am)
Thanks guys. Both are exactly what I needed.