This project has moved and is read-only. For the latest updates, please go here.

Farseer camera class + SmoothStep...

Jan 31, 2011 at 10:19 PM
Edited Jan 31, 2011 at 10:20 PM

Either the smooth step stuff in the camera class is broken, or I'm misunderstanding something....

My understanding of smoothstep is that it takes three variables... the Start, the End, and what fraction of the way you are between the two.

If I want to get from Here to There in 5 seconds, and 3.5 seconds have passed, then SmoothStep tells me where I should be at that time.

The current SmoothStep code in the camera class is wonky.  It uses the current position as the "Start", and uses the next zoom step as the "End", and a constant value to say where I am between the two.

I'm working on fixing it to work the way I expect, but just wanted to get some confirmation that I haven't misunderstood something.



PS:  The smooth stepping "works", in that it gets from here to there, but it doesn't work properly, giving the correct "fade in, fade out" effect.

Jan 31, 2011 at 10:46 PM

Take a look at the camera2d class in the source control. It has changes since the last release. If you have improvements, upload a patch and I will take a look at it.

Feb 1, 2011 at 12:45 PM

The camera is pretty broken at the moment. I rewrote the whole thing yesterday and made it a lot simpler for the new samples framework. There is still some work left to do though and I haven't tested the new camera properly yet.

The current use of smooth step is okayish i think even though it is a bit "wonky". The "correct" application of smooth step just works if you have a fixed start and end point like e.g. following a path. When you track a body and change the cameras target location on every frame in the worst case, you don't want to have a power curve between each start and end point as both (or at least one if you keep the start fixed) constantly move around. That would result in a series a unexpected speedups and slowdowns.

The "problem" with the current camera is that it sets the position directly instead of always just changing the target location. What I am currently doing is using smooth step to set the cameras velocity based on the distance to the target and the direction is calculated independently.

Feb 5, 2011 at 12:00 AM

I've updated my own local copy of the camera class, using the release version as a base.

The general idea I used was as follows:

When you want to move the camera, you call a method called SetTargetLocation, passing a target location and a time-to-move.  This sets up some member variables:

        public void SetTargetPosition(Vector2 targetPosition, float TimeInSeconds)
            StartMovePosition = Position;
            TargetMove = targetPosition;
            CurrentMoveTime = 0;
            EndMoveTime = TimeInSeconds;


Then, in Update, it's as easy as:


            CurrentMoveTime += (float)gametime.ElapsedGameTime.TotalSeconds;

            if (Position != TargetMove)
                Position = Vector2.SmoothStep(StartMovePosition, TargetMove, CurrentMoveTime / EndMoveTime);


If you want to instantly move the camera, just pass in a time-to-move of 0 (but it won't take effect until the next update cycle).

Copy and paste for Zoom and Rotation, and voila!

I'll see if I can get it cleaned up and submit a patch.