Jump to content
  • Advertisement
Sign in to follow this  

"Sliding" Camera

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
...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 this post


Link to post
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_vel
D = cur_pos

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!