Home » Community » Forums » » Creating a Useful Camera Class
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Creating a Useful Camera Class
Post Reply 
The description on the mainpage of GDNet is incorrect. It describes the previous article.

I'm not back. I'm just visiting.

 User Rating: 1469   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Michael, thanks for this article. just yesterday i thought: "hey, my camera class sucks, it needs flyby features and other stuff". today, what a coincidence, your article :)

but i noticed that there's no link to the source... can someone fix that?

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

got it! the "a href"-link was written in the html code, but it contained a typo (1260 instead of 2160)


here's the link:


http://www.gamedev.net/reference/articles/2160/source.txt

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Nice!

I'm about to replace the camera code in my engine soon, so it's good to see an article about it. Good math explanations.

 User Rating: 1221   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

When your explaining polar coordinates you state

x = radius*sin(theta)
z = radius*cos(theta)


Wouldn't it be the other way around (atleast given you description)?

 User Rating: 1070   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Actually, yes you are right. If you look above a few lines it is explained correctly there. just did a typo for the last few lines of it. It should be:

x = radius*cos(theta)
z = radius*sin(theta)

The rest of the article should be correct though.
Thanks for paying such close attention to my math ;)

-Mike

 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Can you explain slideCamera(float h, float v)?

I have used your camera it works really fantastic the camera is really flexible. I have implemented all other effects only
panning in current screen is remaining. I couldn't get the
working panning effect with the help of slideCamera function.

Please let me know.



MOV AL, 0x00
MOV AL, ME

 User Rating: 1014   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I have a question: Is the "Target" Vector a unit vector?

 User Rating: 1004   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Where is the Vector3 definition found? Can't seem to find it in any DirectX file as of yet. Is it C#/Managed DirectX only? If so, is there an equivelant in C++/DX9?

Would a D3DXMATRIX work?

 User Rating: 1020   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by c_back
Where is the Vector3 definition found? Can't seem to find it in any DirectX file as of yet. Is it C# only?

There isnt one but there is a class called D3DXVECTOR3 that should work, look at the SDK to figure out how to use it, its simple.

______________________________________________________________________________________
With the flesh of a cow.

 User Rating: 1218   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

thanks :)

 User Rating: 1020   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Sweet. Thanks for the camera class (and crystal clear math explanations) Michael. Very generous.


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

A bit of a change for this camera class. It almost works better to leave the up vector as (0,1,0) most of the time unless the angle of the camera gets below or above +- PI/2 (this way the camera can be upside-down) and setting the up vector to (0,-1,0). I'm sure many of you have already figured this out, but I just thought I would add it in case.

--------------------------------------------------------------------------
Michael Schuld

 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Great article, has saved me from more hair loss, as my original camera class was refusing to work properly. Maths is reletivly simple, yet effective - and a good excuse to brush up on my trig. :S

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Thanks for the Camera class... I donno if this makes sense but is it possible to make all this using Matrix Transformations? Ignore if this sounds like plain stupid but i thought that since these are as such vectors... maybe a simple transformation could solve rotations, translations and the rest easily

Still the Camera class rocks and the explanations are very good. The only thing i can't understand is the CameraUp vector. Can you just leave it as it is all the way? What are the implications of this. Thanks ;)

Btw pls be gentle this is my very first post :p

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The upVector is basically a way of orienting the camera. If you think about a physical camera for a second it can be really easy to explain. Since our camera is defined by three vectors, position, target, and upvector, all you have to do is think about what using each of these pieces of information will do for you and see what is still needed and how what is left fills those needs

1. Position: obviously we need a location for our camera, but just telling it where it is doesn't say what it is showing us or in what wat the camera is rotated around any of its axes.

2. Target: this takes care of two of the axes of rotation for us. If something is above us and to the left then we target the camera with a vector like (-4, 3, 0), but this will still not tell us how the camera is rotated around its z axis (imagine taking a picture and then standing on your head and taking it again, same position and target, but your upvector has changed and so has the picture)

3. The final piece of the puzzle tells the rendering device which direction is up according to the camera so it know not to draw everything upside down or sideways. Play with the values in the upVector and you will see very quickly what changes this vector can make.

That is the simplest terms I can put it in. Granted it is 3:45 in the morning and it also happens to be Christmas, but I think it was pretty coherent and well laid out. Anyone else's explanations on the topic of upVectors are definately welcome.

--------------------------------------------------------------------------
Michael Schuld

 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

First off I would like to thank mikeschuld for writing this article and providing the source code for it. I am new to DirectX and I was excited when I finally figured out how to display my own meshes on the screen with custom textures and all. I made a GameObject class, and then the next step seemed to be making a camera class. I tried making one myself and then realized I needed to learn some more 3D math to be able to finish it. I used mikeschuld's implementation from the source code, and it almost works perfect. However, I am a little confused about the cameraUpVector. I read mikeschuld's explanation of it, and I thought I understood it. But then I remembered what mikeschuld wrote earlier about it working better if you leave it as (0,1,0) and changing it (0,-1,0) at times in order to prevent it from being upside down. It seems based on his explanation that this would not be the case. Also, my class seems to be having problems with the upVector. I tried using the same functions as the provided source code, but sometimes the camera flips upside down. I tried changing it according to how mikeschuld said in his post, and used this implementation:

if(_vRadians < -Math.PI/2 || _vRadians > Math.PI/2)
_cameraUpVector = new Vector3(0f, -1f, 0f);
else
_cameraUpVector = new Vector3(0f, 1f, 0f);

But is still flips upside down at times. Help would be greatly appreciated! Oh, I also have a question about this post:

"Actually, yes you are right. If you look above a few lines it is explained correctly there. just did a typo for the last few lines of it. It should be:

x = radius*cos(theta)
z = radius*sin(theta)

The rest of the article should be correct though.
Thanks for paying such close attention to my math ;)"

Does this require a change in the source code, or is that part implemented correctly?

 User Rating: 1006   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

This doesn't entirely apply to this version of the camera, but I will paste the code I use for my current upVector on my camera.


upVector = vRotation > Math.PI / 2 && vRotation < Math.PI / 2 * 3 ?
new Vector3(0, -1, 0) : new Vector3(0, 1, 0);

My current implementation doesn't use any negative angles

Trying to calculate the entire rotation of the real upVector is much more difficult, and it seems to handle just as well giving it only up or down as the the direction. This does not however let you "roll" the camera around its axis. To accomplish that, I will be implementing a Quaternion based camera which will eliminate all appearances of gimble lock.

 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

your camera really greate ,but if u like to help me in moving it ... i mean i need to make the camera moves while iam moving my mesh, plz help

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

thx really the camera is great but i need help to move this camera.i mean i need while my object move the camera move as u see in all games ex nefor speed we see the car althought its movement in the scene
thx for all

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I'm farily new to DirectX aswell, and i was experimenting with quaternions a couple of days ago.
I took the liberty of converting the camera class to work with these instead.
Basically, handle user input with MoveCamera and RotateCamera, and call GetViewTransformation in your game loop.
The camera always looks in the same direction, and has a constant up vector. When the user moves or rotates, the camera transformation matrix is affected. The view transform matrix is then created based on this.

#include <d3dx9.h>
#ifndef CAMERA_H
#define CAMERA_H

/*
	Quaternion based Camera by Haze
	haze42@gmail.com
*/
class Camera{
private:
	D3DXVECTOR3 mPosition;
	D3DXVECTOR3 mUp;
	D3DXVECTOR3 mDirection;
	D3DXMATRIXA16 mTransform;
public:
	Camera(D3DXVECTOR3 pos, FLOAT hRot, FLOAT vRot, FLOAT zRot=0){
		mPosition = pos;
		mUp = D3DXVECTOR3(0,1,0);
		mDirection = D3DXVECTOR3(0,0,-1);
		D3DXMatrixIdentity(&mTransform);
	}
	~Camera(){}
	
	//Move the camera forward/backward/sidestep
	VOID MoveCamera(FLOAT forward, FLOAT strafe=0){
		D3DXMATRIXA16 pTranslate;
		D3DXMatrixTranslation(&pTranslate,strafe,0,forward);
		D3DXMatrixMultiply(&mTransform,&mTransform,&pTranslate);
		//We create a translation matrix directly from the input fwd/back and strafe distance.
		//This is then added to the total camera tranformation matrix where rotation is added
		//giving us the correct direction

	}
	//Rotate the camera around horiz, vertical and z
	VOID RotateCamera(FLOAT h, FLOAT v, FLOAT z=0){
		D3DXQUATERNION qR;
		D3DXMATRIXA16 sRotate;
		D3DXMATRIXA16 sTransform;
		D3DXQuaternionRotationYawPitchRoll(&qR, h*(D3DX_PI/180.0f),
												v*(D3DX_PI/180.0f),
												z*(D3DX_PI/180.0f));
		D3DXMatrixAffineTransformation(&sRotate,1.0f,NULL,&qR,NULL);
		D3DXMatrixInverse(&sTransform,NULL,&sRotate);
		D3DXMatrixMultiply(	(D3DXMATRIX*)&mTransform,
							(D3DXMATRIX*)&mTransform,
							(D3DXMATRIX*)&sTransform);
	} 

	D3DXMATRIXA16 GetViewTransformation(){
		D3DXMATRIXA16 pView;
		if(FAILED(D3DXMatrixLookAtLH((D3DXMATRIX*)&pView,&mPosition,&mDirection,&mUp)))
			D3DXMatrixIdentity(&pView);
		D3DXMatrixMultiply(&pView,&pView,&mTransform);
		return pView;
	}
};



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

loved the article, simple and precise.. it helped me create my first camera!

thx

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: