Chase camera with damping
Chase camera with damping
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?
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?
Re: Chase camera with damping
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.
Re: Chase camera with damping
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.
Re: Chase camera with damping
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.
And the ray cast filter is calculated as:
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;
Code: Select all
Func<BroadPhaseEntry, bool> rayCastFilter;
bool RayCastFilter(BroadPhaseEntry entry)
{
return entry != chaseObject.CollisionInformation && (entry.CollisionRules.Personal <= CollisionRule.Normal);
}
Re: Chase camera with damping
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.
Re: Chase camera with damping
Sorry, what do you mean?
Re: Chase camera with damping
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))
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.
Re: Chase camera with damping
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.
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.
Re: Chase camera with damping
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.
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.
Re: Chase camera with damping
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.
The contents of the if of the ray cast just isn't firing. I have no idea why.
Re: Chase camera with damping
Then the ray cast is not hitting anything; are the objects in the space, and are they actually where they appear to be?
Re: Chase camera with damping
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
.

Re: Chase camera with damping
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.
Thanks for the help.