Sign in to follow this  
deavik

3rd person camera spring

Recommended Posts

All racing games use springs on cameras don't they? But the funny thing is, say when your acceleration is longitudinal, the cameras pretty much stick to the car. But they have a smooth slow damped motion when turning. My camera lags behind too much if I try to get a nice visual effect while turning. Any hints?

Share this post


Link to post
Share on other sites
Why not use different dampenings for forward/backward and turning motion?
That is they way I have done it, and it behaved like in most games.

Share this post


Link to post
Share on other sites
Thanks for writing in, gulgi! Yeah, I can incorporate different dampings, but how do I make it specific to longitudinal or rotatory motion? As I have it now, the ultimate position of the camera depends on only one external factor - the position of the car. So how does the camera differentiate whether it is turning or moving straight?

Share this post


Link to post
Share on other sites
Hey, chill out. You're lucky you even got a reply. Half the time I don't even get a reply to my posts. How is the car turning? Using an angle? Use this angle to determine the new camera position, but instead of instantly changing the camera's placement to that position, add a number to the position until it's moved there. This would give you the spring effect.

Share this post


Link to post
Share on other sites
Hey Ignisdeus, even if I'm not getting replies, I'm loyal to these formus. I resent your comments, yes sir! And I've got the spring effect in place actually, I was thinking of how to limit the spring effect to turning motion. I have an idea - to clamp the distance of the camera from the car. Does anyone think that will do the needful? Yes, even you IgnisDeus ...

Share this post


Link to post
Share on other sites
Let's keep a friendly tone in here shall we.

These are some offhand calculations I made just now. I don't know if they will fit into your model but here goes.

This damping model is based on that you have an angle (in the plane) for the car denoted θcar and a corresponding angle for the camera denoted θcamera. To make the camera move from it's initial angle θcamera to the angle θcar parallel to the car you could interpolate between the two angles to get a new intermediate angle θnew for the camera, using

θnew = θcamera + (θcar - θcamera) * F(Δt)

where F(Δt) is some fuction of the time duration Δt and acts as interpolation factor. For this particular damping model

F(Δt) = 1 - e-k * Δt

where k is defined by

k = (ln 2) / Δt1/2.

In this definition for k, Δt1/2 is the average time you want for the camera to turn half the way to the car's direction. You can pick any value for Δt1/2. The final formula is

θnew = θcamera + (θcar - θcamera)(1 - e-k * Δt).

Use this formula every frame with Δt as the duration since the last frame to calculate the new angle for the camera. I think it should work quite well. I haven't tried it myself though[smile]. Tell me if you do.

Share this post


Link to post
Share on other sites
Heh. I just realized that the formula for θnew in my previous post can be greatly simplified using powers of two instead of powers of e, eliminating the need for k altogether. The formula becomes

θnew = θcamera + (θcar - θcamera)(1 - 2-Δt / Δt1/2).

Δt1/2 is still a constant value that you pick yourself.

Share this post


Link to post
Share on other sites
Don't worry anyone, everything's fine with me and ignisdeus! [smile] I don't know what made me write that ... any way, no hard feelings Ignisdeus ... and I know how you feel - it's frustrating to not get answered, but this place has helped me SO much that I think you should give it a fair chance too.

staaf, I have a spring code, like the one you described, only it interpolates position, not angle. So if I implement it your way, I have to ... let's see ... find out the new angle, then subtract a set scalar (dist betn camera and car) in the direction of theta, and that will be the position of the camera! I'll give it a try and post back.

Share this post


Link to post
Share on other sites
staaf, I got your code working, but I have taken the angles as negative when snticlockwise (0 to 180 degrees) and positive when clockwise. The problem occurs when the car's angle jumps from -179 to say 179. Do you have any suggestions?

Share this post


Link to post
Share on other sites
I think you could extend the model to handle such cases if you replace

car - θcamera)

in the above expression with

(absmin( φ1, φ2 ))

where

φ1 = θcar - θcamera

and

φ2 = θcar - θcamera - (360 * signcar - θcamera)),

and where absmin() is a function that picks out the argument with the smallest absolute value and sign() is a function that returns the sign of the argument (1 or -1).

This should if I'm not mistaken pick out the closest way around the circle. For example if θcar = 179 and θcamera = -179 the result of this expression would be -2, making the camera strive for -181 which in turn will be mapped to 179; the car's direction.

Share this post


Link to post
Share on other sites
staaf, it's working grrreat! You are a math genius! All I had to do was clamp the values of theta_cam so that they didn't go beyond 360 and -360 degrees. Works like a charm! Thanks a lot for your help!

rating++

Share this post


Link to post
Share on other sites
I'm glad it worked out for you. Now that I got to think about it I noted that directed angles can be a pain. Perhaps I should try to make an effort some time to wrap up this functionality in some kind of toolset to handle directed angles in a controlled way.

Anyway, see you around.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this