Game Development Community

dev|Pro Game Development Curriculum

Fluid Camera (TGB)

by Jeffrey Sweeney · 10/22/2008 (9:22 am) · 4 comments

if (!isObject(Camera)) {
%template = new BehaviorTemplate(Camera);
%template.friendlyName = "Camera";
%template.behaviorType = "Camera";
%template.description = "Makes the camera follow this object";


%template.addBehaviorField(speed, "speed the camera follows (lower = faster)", float, 50.0);
}

function Camera::onBehaviorAdd(%this) {
sceneWindow2D.startCameraMove();

%this.owner.enableUpdateCallback();
}


function Camera::onUpdate(%this) {
sceneWindow2D.setCurrentCameraPosition(
(getWord(sceneWindow2D.getCurrentCameraPosition(), 0) + ((%this.owner.getPositionX() - getWord(sceneWindow2D.getCurrentCameraPosition(), 0)) / %this.speed)),
(getWord(sceneWindow2D.getCurrentCameraPosition(), 1) + ((%this.owner.getPositionY() - getWord(sceneWindow2D.getCurrentCameraPosition(), 1)) / %this.speed))
);
}

About the author

Recent Blogs


#1
11/25/2008 (11:45 pm)
how to add collision with camera,can let camera check triggers
#2
05/18/2009 (2:30 pm)
wouldn't it be better to just mount the camera to the object in question?... because while your code works flawlessly, it may add some heavy calculations every 32ms and sometimes you might need that timeframe for some other stuff...
#3
08/06/2010 (2:33 pm)
Dear community,

This is my first post, I am new in tgb 2D and I am creating a simple platform game.

I'd like to get the opposite effect of this behavior.
when the character moves, the camera moves back the character, and I want the camera is in front of the character.
how I can fix it?
I thought about changing the signs in the calculation of the position of the camera, but it is a dirty solution and also does not work

thanks you very much!!

(i'm sorry, my english is very poor)
#4
08/06/2010 (2:57 pm)
ah! I forgot it!

also I have another problem with this behavior.

if I copy the code and paste it in my project, when the character is running, this appears twice when it reaches the limit of the camera, as if no double buffer or take a long time to refresh.

thanks!