Camera as Smash Brothers
by Joao Caxaria · in Torque Game Builder · 12/22/2008 (4:26 pm) · 6 replies
Hey guys,
I'm looking to get my camera always showing my 2 players, showing the game very close if the players are together and showing it zoomed out if they move away from each other, making them always visible, wherever they may go.
Is there a already existing script (there are so many resources already...) or, if not, any pointer on how to do it properly for me to start?
Thanks!
I'm looking to get my camera always showing my 2 players, showing the game very close if the players are together and showing it zoomed out if they move away from each other, making them always visible, wherever they may go.
Is there a already existing script (there are so many resources already...) or, if not, any pointer on how to do it properly for me to start?
Thanks!
About the author
#2
12/23/2008 (3:36 am)
Thanks, I'll give it a go!
#3
I'll update with the details once I've got it implemented.
12/23/2008 (4:16 am)
I'm working the kinks out of a similar concept with the exception being zooming the camera in on the player based on proximity to the nearest interactable environment object (think lunar lander camera but a bit more dynamic).I'll update with the details once I've got it implemented.
#4
If I get something worth sharing, I'll also post it here.
12/23/2008 (4:28 am)
Thanks, that would be great. If I get something worth sharing, I'll also post it here.
#5
12/24/2008 (9:09 am)
I did something like this a while ago. I'll try to post it here if I have time to dig it up.
#6
It's ugly, sloppy and probably not the best solution, but it seems to kind of work and maybe it will help someone somehow. If you improve on it or have suggestions I'd appreciate a post here.
I coded it as a behavior..
12/25/2008 (10:37 am)
I was prototyping a fighting game a year or two ago, and this is what I came up with for that.It's ugly, sloppy and probably not the best solution, but it seems to kind of work and maybe it will help someone somehow. If you improve on it or have suggestions I'd appreciate a post here.
I coded it as a behavior..
//-----------------------------------------------------------------------------
/*
Mount this to a sceneobject.
Notes:
Camera uses setViewLimitOn using the world limits of the object this behavior is attached to.
Camera currently only pans horizontally
*/
//-----------------------------------------------------------------------------
if (!isObject(FightingCameraBehavior))
{
%template = new BehaviorTemplate(FightingCameraBehavior);
%template.friendlyName = "Fighting Game Camera";
%template.behaviorType = "Camera";
%template.description = "Camera that stays between two objects.";
%template.addBehaviorField(xSpeed, "Percentage of horizontal camera speed", float, 100);
%template.addBehaviorField( object1 , "Object 1", object, "", t2dSceneObject );
%template.addBehaviorField( object2 , "Object 2", object, "", t2dSceneObject );
%template.addBehaviorField( window, "The t2dSceneWindow camera to mount with.", string, "sceneWindow2D" );
%template.addBehaviorField( cameraMountForce, "The camera mount force.", float, 1 );
%template.addBehaviorField( zoomFactor, "The zoom factor.", int, 400 );
}
function FightingCameraBehavior::onBehaviorAdd(%this)
{
}
function FightingCameraBehavior::onLevelLoaded(%this, %scenegraph)
{ if (!isObject( %this.window )) return;
%currPos = %this.window.getCurrentCameraPosition();
%this.oldPosX = getWord(%currPos, 0);
%this.oldPosY = getWord(%currPos, 1);
%this.window.setCurrentCameraPosition( %this.owner.getPosition() SPC "100 75" );
%this.window.mount( %this.owner , "0 0", %this.cameraMountForce, true);
%this.keepUpdating();
//Set camera bounds
%Bounds = %this.owner.WorldLimitMin SPC %this.owner.WorldLimitMax;
%BoundsCam = (getword(%Bounds,0)-2) SPC (getword(%Bounds,1)-7.5) SPC (getword(%Bounds,2)+2) SPC (getword(%Bounds,3)+7.5);
%this.window.setViewLimitOn( %BoundsCam );
}
function FightingCameraBehavior::keepUpdating( %this ){
%this.updateCameraPosition();
%this.keepitgoing = %this.schedule( 8, "keepUpdating" );
}
function FightingCameraBehavior::updateCameraPosition(%this)
{ if (!isObject( %this.window )) return;
%O1X = %this.object1.getPositionx();
%O2X = %this.object2.getPositionx();
%vecBetweenX = t2dVectorDistance( %O1X SPC "0" , %O2X SPC "0");
if ( %O1X > %O2X )
%this.owner.moveTo( %O1X - %vecBetweenX/2 SPC %this.owner.getPositionY() , %this.xSpeed, 1, 1, 1, 0.01);
else
%this.owner.moveTo( %O2X - %vecBetweenX/2 SPC %this.owner.getPositionY() , %this.xSpeed, 1, 1, 1, 0.01);
// The closer they get zoom in, the further they go, zoom out.
%this.window.setCurrentCameraZoom( 1 - mAbs( %vecBetweenX/%this.zoomFactor ) );
}
Associate Phillip O'Shea
Violent Tulip
function updateCamera() { %cameraSize = sceneWindow2d.getCurrentCameraSize(); %playerA = $playerA.getPosition(); %playerB = $playerB.getPosition(); %playerOffset = t2dVectorSub(%playerB, %playerA); %cameraZoom = 1.0; if (mAbs(%playerOffset.x) > %cameraSize.x) { %cameraZoom = getMax(%cameraSize.x / mAbs(%playerOffset.x), %cameraZoom); } if (mAbs(%playerOffset.y) > %cameraSize.y) { %cameraZoom = getMax(%cameraSize.y / mAbs(%playerOffset.y), %cameraZoom); } sceneWindow2d.setCurrentCameraZoom(%cameraZoom); } function getMax(%a, %b) { if (%a > %b) { return %a; } return %b; }Note, this is completely untested!
What it is trying to do is zoom out based on the offset between players. From what I recall (not near the camera source right now), if your camera width is 100 and you want to view 120, you zoom out 20%, (or set your zoom to 0.8).
Give it a whirl and let me know how you get on!