Chase camera with damping

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Chase camera with damping

Post by al9191 »

I want to implement a camera which follows the player around. At the moment it simply sits a certain offset behind the player, however, most games have some form of damping so that the camera follows the target more like it is on a reasonably tight spring so it moves behind it watching the player.

I was wondering if it made sense to actually model this as the camera being an object in the game attached to the player by a spring?

Or would it best to do this camera strictly with XNA and look at XNA forums for ideas?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Chase camera with damping

Post by Norbo »

I would not recommend using a physical object for a chase camera. It will behave a lot like a physical object, and that's not always a desirable property for a camera. Controlling the camera through specialized logic and perhaps an obstacle-detecting ray cast would indeed be better.
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

Ok I have found an XNA chase camera example, I will use that to try and work out how to do it by modelling the camera using logic and if needs be ray casting to sit it in front of obstacles.
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

I have tried looking at the camera from BEPU demos. But I cannot get it work properly with the existing camera code I had in place. I have a camera which tracks a target in similar way to the camera class from demo.
However, I am trying to implement the ray casting part.

I have a Vector3 for the camera position relative to the target and also in the world space.
I have a Vector3 for the target position and their rotation etc.

So I assume what I need is a Vector3 for the direction from target to the camera, then ray cast in that direction starting from the target. Then, move the camera to the first hit location.

This is the code to calculate the camera position which a view matrix is calculated from. This is where I need to move the camera to in front of any obstacles between camera and target.

Code: Select all

Vector3 cameraPos = Matrix3X3.Transform(newPosition, chaseObject.BufferedStates.InterpolatedStates.OrientationMatrix);
            cameraPos += chaseObject.BufferedStates.InterpolatedStates.Position;

            Vector3 facing = cameraPos - chaseObject.Position;
            float distance = Vector3.Distance(cameraPos, chaseObject.Position);

            Vector3 position;
            RayCastResult result;
            if (chaseObject.Space.RayCast(new Ray(cameraPos, facing), distance, rayCastFilter, out result))
            {
                position = result.HitData.Location;
            }
            else
            {
                position = cameraPos;
            }
            return position;

And the ray cast filter is calculated as:

Code: Select all

       
Func<BroadPhaseEntry, bool> rayCastFilter;
        bool RayCastFilter(BroadPhaseEntry entry)
        {
            return entry != chaseObject.CollisionInformation && (entry.CollisionRules.Personal <= CollisionRule.Normal);
        }
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Chase camera with damping

Post by Norbo »

The 'distance' is in terms of the length of the ray direction. In other words, the furthest point that can be found by a ray cast is origin + direction * maximum distance. Other than that, nothing pops out as wrong.
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

Sorry, what do you mean?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Chase camera with damping

Post by Norbo »

Code: Select all

Vector3 facing = cameraPos - chaseObject.Position;
float distance = Vector3.Distance(cameraPos, chaseObject.Position);
...
if (chaseObject.Space.RayCast(new Ray(cameraPos, facing), distance, rayCastFilter, out result))
The 'distance' passed into the ray cast does not mean that the furthest point is 'distance' units away. It means that it is up to facing.Length() * 'distance' total units away (equal to the squared length of facing).

If you want the maximum distance to be such that it only extends up to the goal camera position and no futher, the maximum distance should be 1. That's because the current ray direction is the full offset from the target to the camera, not a normalized direction.
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

Ah ok, thanks.

So should I make the direction a unit vector?

I have tried it without a limit on the distance of the ray cast, but it still doesn't work. The ray cast just doesn't fire, even if there is an obstacle between the target and the camera.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Chase camera with damping

Post by Norbo »

Using a full offset as the direction with a maximum distance of 1 is perfectly fine and would be a tiny bit faster than doing the unnecessary normalization.

I don't see anything in the code which would explain why the ray cast wouldn't fire; do you mean it just doesn't hit anything? I would recommend verifying that the data used to construct the ray's origin and direction are valid.
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

I have tested them, I know that the origin and direction are correct. They are used to position the camera normally and that works.

The contents of the if of the ray cast just isn't firing. I have no idea why.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Chase camera with damping

Post by Norbo »

Then the ray cast is not hitting anything; are the objects in the space, and are they actually where they appear to be?
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

This is really strange. I will have a look into it, there is a chance the physics representation of the obstacle isn't as the graphics version looks. Can't believe I didn't think of that, thanks :).
al9191
Posts: 68
Joined: Wed Nov 16, 2011 11:19 pm

Re: Chase camera with damping

Post by al9191 »

The problem was with one of the obstacles had incorrect scale so ray casts were missing it. Also, for some reason I have discovered even though when printing the ray direction it makes sense, it isn't quite right, as I know the objects are detectable now, but it doesn't always detect them with the origin and direction I have in the code sample above. Therefore, they must be in-correct somehow.

Thanks for the help.
Post Reply