Adding physics to camera

Jan 18, 2010 at 4:57 PM


after solving my previous problems I have a new one. I'm using the code from ExamplePlayer with the EularOrientation.
Here is the important code:

    private void UpdateCamera()
            const int offsetX = 200;
            const int offsetY = offsetX;
            MouseState mouseState = Engine.MouseState;
            float dX = mouseState.X - offsetX;
            float dY = mouseState.Y - offsetY;
            cameraOrientation.Angle2 -= dX / 1000;
            cameraOrientation.Angle1 -= dY / 1000;
            Mouse.SetPosition(offsetX, offsetY);

            camera.Position = Component.PositionWorld - camera.Forward * 64;

Now there is a problem with navigating through my level: the camera does not collide with the walls.
So I can turn the camera i a way that a wall is inbetween my camera and my player... That looks kind of ugly...

So how can I add some physics to the camera in order to prevent the camera going through walls?Here are some screenshots of the problem. 1   2
Can anyone help me with this problem?


Jan 18, 2010 at 6:35 PM

I think you'll just have to pan in the camera toward the ball whenever a 3D segment (or maybe a ray) from the ball to the camera collides with the environment's collision mesh. The actual calculation will be done with JigLibX and the result applied to Ox's camera. Looks like the JigLibX guys wrote up some documentation on this functionality here - As to the actual vector math behind it, you'll need to consult your local Linear Algebra reference :)

Jan 20, 2010 at 7:28 AM
Edited Jan 20, 2010 at 7:29 AM

i might be completely wrong or missing the point, but i think you can attach the camera to a physics object maybe a ball

by using

camera.Position = ball.Position;

etc, and then control the ball with user input instead of the camera. pretty sure you can set a "visible" toggle in the level editor too.

just a thought :)

Jan 21, 2010 at 11:50 AM

Yes, I got the same idea, but there is one problem... The connection between my player model und and the camera body... I wanted to use something like a spring but there is no such a feature in JiglibX... =(

So, how can I simulate a similar behaiviour?

Thank you all =)