Jump to content
  • Advertisement
Sign in to follow this  
rkennedy9064

Camera View question

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

I've been messing around with some code and different tutorials, and seem to have a basic understanding of how this camera works, but was wondering if anyone could help explain the math behind the code. This code places a camera directly behind a model, and positions it so that it is looking at whatever the model is looking at based on its altitude and azimuth, then sets the view and uses the models position to base where the camera is positioned. The main questions I had were...
1. What exactly is the code getting when it takes the sine and cosine of the altitude and azimuth? (the Math::CosSin function calculates the cosine and sine and stores them in a 2D vector)
2. I'm assuming that in the view code, its taking the values it calculated from the previous function and builds a view for the camera to look at, but how does multiplying those values create a view for the camera?
3. Finally, how does Modding the view vector with the right vector create the down vector for the view?

I would really appreciate it if someone knew how exaclty the code worked, or had a reference site that would help me understand it a bit better. I get what its doing, but want to understand how it all works underneath. Any help would be greatly appreciated. This is the code for it

[font="Consolas"][font="Consolas"]PlayerController *controller = [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]static_cast[/font][/font][/font][font="Consolas"][font="Consolas"]<PlayerController*>(model->GetController());

Vector2D t = Math::CosSin(controller->GetModelAzimuth());
Vector2D u = Math::CosSin(controller->GetModelAltitude());
Vector3D view(t.x * u.x, t.y * u.x, u.y);
Vector3D right(t.y, -t.x, 0.0F);
Vector3D down = view % right;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]const[/font][/font][/font][font="Consolas"][font="Consolas"] Point3D& position = model->GetWorldPosition();
Point3D p1(position.x, position.y, position.z + 1.5F);
Point3D p2 = p1 - view * 4.0F;

[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
The code with a few comments:

// Everything from here...
Vector2D t = Math::CosSin(controller->GetModelAzimuth());
Vector2D u = Math::CosSin(controller->GetModelAltitude());
Vector3D view(t.x * u.x, t.y * u.x, u.y);
// ...to here is a 'spherical-to-Cartesian coordinate conversion'.
// Searching for 'spherical coordinates' should tell you more
// about this.

// This uses the 'perp' vector operation to construct a vector
// perpendicular to the forward vector as determined by the
// azimuth/yaw (this gives you the 'side' vector for the
// transform).
Vector3D right(t.y, -t.x, 0.0F);

// This computes the third vector of the basis as the cross
// product of the other two. (Note however that unclear operator
// overloading of this sort should generally be avoided. IMO, at
// least.)
Vector3D down = view % right;

// I'm not sure how 'p2' is used, but the point 'p2' is set to
// be (presumably) above and behind the target point with respect
// to the camera's current basis.
const Point3D& position = model->GetWorldPosition();
Point3D p1(position.x, position.y, position.z + 1.5F);
Point3D p2 = p1 - view * 4.0F;

Share this post


Link to post
Share on other sites
Thanks for the help, the spherical to cartesian was what I needed to know. I found a website with a diagram showing how the angles are used to plot a point., and I just looked at the definition of the overloaded operators and the mod operator does find the cross product of the vectors. Also, the p2 is used later on to place the camera in the world, I just forgot to post that code. Thanks for taking the time to help me understand.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!