camera wont get current pos. of obj. to follow

Started by
3 comments, last by justcallmedrago 16 years, 3 months ago
I took a whack at making a link between my camera and the object I want it to follow. I made a pointer that can be set to an OBJECT struct that I want the camera to follow.
struct CAMERA{
	D3DXVECTOR3 position;
	D3DXVECTOR3 lookAt;
	D3DXVECTOR3 cameraUp;
	float rotationAngle;
	float angleFromVertical;
	float distance;
	D3DXMATRIX viewMatrix;
	D3DXMATRIX projectionMatrix;
	DWORD setting;
	OBJECT* linkToObject; // The link that doesn't work
	void FillViewMatrix();
	void FillProjectionMatrix();
	void Update();
	void Orbit(float rotationAngle, float angleFromVertical, float distance);
	void Follow(OBJECT* objectToFollow, float distance);
	void Strafe(CAMERA &camera, float distance, DWORD direction);
	CAMERA();
	CAMERA(D3DXVECTOR3 posVector, D3DXVECTOR3 lookVector, D3DXVECTOR3 upVector);
};

void CAMERA::Follow(OBJECT* objectToFollow, float distance)
{
	this->linkToObject = objectToFollow; //SETS THE LINK (not really)
	this->distance = distance;
	this->setting = CAM_FOLLOW;
	this->Orbit(0.0f, 30.0f, distance);
}
void CAMERA::Update()
{
	if(this->setting = CAM_FOLLOW)
		this->Orbit(this->rotationAngle, this->angleFromVertical, this->distance);
	this->FillViewMatrix();
}
void CAMERA::Orbit(float rotationAngle, float angleFromVertical, float distance)
{
	if(rotationAngle >= 360.0f)
		rotationAngle -= 360.0f;
	if(rotationAngle <= -360.0f)
		rotationAngle += 360.0f;
	if(angleFromVertical < 0.1f)
		angleFromVertical = 0.1f;
	if(angleFromVertical > 179.9f)
		angleFromVertical = 179.9f;
	this->rotationAngle = rotationAngle;
	this->angleFromVertical = angleFromVertical;
	this->lookAt = this->linkToObject->position; //Example of link use
	this->position.x = this->linkToObject->position.x + distance * 
sin(D3DXToRadian(rotationAngle)) * sin(D3DXToRadian(angleFromVertical));
	this->position.y = this->linkToObject->position.y + distance * 
cos(D3DXToRadian(angleFromVertical));
	this->position.z = this->linkToObject->position.z + distance * 
cos(D3DXToRadian(rotationAngle)) * sin(D3DXToRadian(angleFromVertical));
}

Of course, the linking doesn't work. I'm thinking all that happens when follow() is called, a copy of the object pointed to is created. For example, I set the camera to follow an object that starts at (0,0,0). Even if I move that object around, the camera doesn't budge from (0,0,0). :( I have tried to make a reference to the object, but I get the error "CAMERA::linkToObject must be initialized in constructor base/member initializer list" What can I do to get the object's current position?
-----------------------------std::string is the way to go.
Advertisement
Are you calling IDirect3DDevice9::SetTransform(D3DTS_VIEW, &viewMatrix) every update?
.
D3D_Device->BeginScene();	mainCamera.Update();    D3D_Device->SetTransform(D3DTS_VIEW, &mainCamera.viewMatrix);    // set the view transform    D3D_Device->SetTransform(D3DTS_PROJECTION, &mainCamera.projectionMatrix);    // set the projection//...


Update():
void CAMERA::Update(){	if(this->setting = CAM_FOLLOW)		this->Orbit(this->rotationAngle, this->angleFromVertical, this->distance);	this->FillViewMatrix();}


FillViewMatrix():
void CAMERA::FillViewMatrix(){    D3DXMatrixLookAtLH(&(this->viewMatrix),			&(this->position),    // the camera position			&(this->lookAt),    // the look-at position			&(this->cameraUp));    // the up direction}


Yes. Sorry I meant "the camera only rotates and looks at (0,0,0)" instead of "doesn't budge". The camera rotates and orbits just fine, except at the wrong location! :P
-----------------------------std::string is the way to go.
It seems you're not translating the target/at. Thats why its always at 0 0 0. Or is this the line where you do it?:

this->lookAt = this->linkToObject->position;

If the position of your object is changing and you're calling this every frame, the lookAt must change too. THe code seems to be correct...I would try some debugging, try to see the true values of these variables...
.
Nevermind I got it.

[Edited by - justcallmedrago on January 12, 2008 9:29:31 AM]
-----------------------------std::string is the way to go.

This topic is closed to new replies.

Advertisement