• 13
• 15
• 27
• 9
• 9

# Camera View question

This topic is 2568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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;