Jump to content
• Advertisement

# Keeping the "camera" a bit above

This topic is 4372 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

Hello. I'm playing around with MDX a bit and I'm trying to recreate a 3D flying game, I got plane moving in the right directions with the polar equation (not the best but it does its job). Now I want my camera to be a bit above the plane (so you dont only see the tail). I've made a method for it, but it doesnt work 100%. when you rotate in the X axis the plane move further / closer to you. It's quite alot of code, so I dont think it's a good idea to post it all. So I ask is there any easy and good way todo this?

#### Share this post

##### Share on other sites
Advertisement
Is your camera rigidly connect with the plane, e.g., does it always face the same direction relative the plane, and is it always supposed to be centered at the same distance---the same point relate to the plane's local coordinate system?

#### Share this post

##### Share on other sites
Ok, Maybe I wasnt clear enough on my first post. The result I want to get (And I get it so long I just rotate the Y axis) is clicky
When I move the X axis upwards, it still looks good but the plane moves a little bit closer to me clicky
And when I rotate it downwards it starts looking awfull clicky . I can make the downward part look quite descent by invert the Z value when the rotation goes below zero, but this give me some other strange results when I rotate in the Y axis. Let me explain how I thought this would work.

If you look at clicky . You'll see my train of thoughts on the code below, The red block is the LookAt vector, and the green is the location of the camera, Firstly I must add 90 degrees to the angle to calculate where the lookat vector should be (above the plane) then set the camera position to the same as the lookat and reset the angles and move the camera, Everything by using the polar equations.

            float xRot = rotations.x, yRot = rotations.y; /* I set the original rotations values in temp variables */            xRot = Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(rotations.x)) * 90); /* Calculates where the relative 90 degree angle should be to the original  */            cam.LookAt = Position; // Sets the lookat position the same as my plane            cam.xRotation = xRot; // Changes the rotation in the camera class, so it has the same angles as the plane (except for the 90 degree change)            cam.yRotation = yRot;            cam.SetLookAt(4); // Calculates where the plane should lookat, (remember that it's a 90 degree angle away from the original plane angle)            if (cam.LookAt.Z > 0 && rotations.x < 0)            {             //   cam.LookAt.Z = -Math.Abs(cam.LookAt.Z);, Inverts the Z value            }            cam.xRotation = rotations.x; // Changes the cameras rotation values back to the planes            cam.Position = cam.LookAt; // Sets the camera position the same as where we are looking            cam.Move(15); // Moves the camera back 15 (what is this unit called? :P)            cam.SetCamera(); // just changes the view point and so on.

And the code for the cam.Move and cam.setlookat methods

        public void SetLookAt(float distance)        {            Vector3 Addvector = new Vector3();            Addvector.Z += Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.X += Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.Y += Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(xrotation)));            LookAt += Addvector * distance;        }

        public void Move(float distance)        {            Vector3 Addvector = new Vector3();            Addvector.Z += Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.X += Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.Y -= Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(xrotation)));            Position += Addvector * distance;        }

Any1 have ANY idea what is causing this?. Thanks in advance!

#### Share this post

##### Share on other sites
Your basic concept is reasonable, but the implementation isn't quite right. Well, I can't do too detailed an analysis without your SetCamera routine.

The first thing that comes to mind is that you are explicitly maintaining a fixed distance from the look-at point, not the position of the plane. Your distance from the plane to the look-at point also remains fixed, but unfortunately because the angles are different for the look-at point offset and the camera offset...the total distance from plane to camera will change. Think about it as a triangle. The offset from the plane to the look-at point is one edge of the triangle. The offset from the look-at-point to the camera is another edge of the triangle. And the final edge is the offset from the camera to the plane. Unless you rotate the first two edges rigidly, the 3rd edge will change length.

So, from your description, it sounds like you are trying to rotate the two edges rigidly, which would work. But...you aren't doing it correctly. The problem may boil down to one bad line:

xRot = Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(rotations.x)) * 90);

It doesn't do what you say you are trying to do. You are wanting xRot to be an angle, in degrees. And in multiplying by 90, it sort of seems to be a reasonable value in degrees. But, it does not do what you say it does. That cosine doesn't belong. I believe you just want to take rotations.x and add 90. No multiplies. No cosines/sines here. All the trig should be in your SetLookAt and Move methods.

#### Share this post

##### Share on other sites
Ok, I changed SetLookAt to
        public void SetLookAt(float distance)        {            Vector3 Addvector = new Vector3();            Addvector.Z -= Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.X -= Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(yrotation)) * Math.Cos(Geometry.DegreeToRadian(xrotation)));            Addvector.Y += Convert.ToSingle(Math.Sin(Geometry.DegreeToRadian(xrotation)));            LookAt += Addvector * distance;        }

and xRot = Convert.ToSingle(Math.Cos(Geometry.DegreeToRadian(rotations.x)) * 90); to xRot += 90, and it's working perfectly now!. Thanks for the help!.

And by the way here is the SetCamera function :)

        public void SetCamera()        {             view = Matrix.LookAtLH(Position, LookAt, Up);            _dev.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, _handle.Width / _handle.Height, 1.0f, far);            _dev.Transform.View = view;        }

Thanks once again for the help :-)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5
• Advertisement

• 33
• 13
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
632570
• Total Posts
3007124

×

## Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!