# "Sliding" Camera

This topic is 3656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my 2D game, My camera has an X, and a Y, and a "TargetX" and a "TargetY" (what the camera is trying to look at). As of now, TargetX and TargetY are unnecessary because the CameraX and CameraY will translate to it immediately. What I want however, is to add a vertical and horizontal velocity to my camera, and have it "track" to the TargetX and TargetY rather than simply following it. Two things I've seen are Cameras that speed up to the destination, and slow down as it approaches it, stopping on a dime. The other one, and what I imagine is the easier one, is where the camera gains an immediate burst in speed and slows down to its destination. My problem is that I don't know how to program either of these though. Thanks a ton in advance.

##### Share on other sites
...Just figured out the latter method. For anyone interested (which there may or may not be any since it's perfectly logical that I could be retarded), I find the distance between the Camera and its target, and I basically keep telling the camera to move a tenth of the way there until the distance moved is so small that as far as pixels are concerned, it doesn't exist.

##### Share on other sites
The former happens by solving an equation system of the form:

     p(t) = Aa3 + Ba2 + Ca + D      p(0) = current position ∂p/∂t(0) = current velocity     p(T) = destination ∂p/∂t(T) = 0

Where T is the duration of the travel (you generally want this one to be a constant set to something small, such as perhaps one second, so that you always track the target closely). This is a four-equation linear system with coordinates A, B, C and D, which you can solve normally. Off the top of my head, you get something like:

A = ...B = ...C = cur_velD = cur_pos

##### Share on other sites
a simple 'placeholder' method.

float relaxation = 0.5f; // arbitrary value in range [0...1].

cameraX += (targetX - cameraX) * relaxation;
cameraY += (targetY - cameraY) * relaxation;

Note that it is frame rate dependent, but it can be it frame rate independent with a bit of work.

The second order equation solving ToohrVyK suggests would give you compete control over the smoothness and speed of the camera reaching the target, but it's more maths.

Basically, it's similar to what I would do for a 'smooth' mouse control.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634060
• Total Posts
3015300
×