Jump to content
  • Advertisement
Sign in to follow this  
justcallmedrago

camera wont get current pos. of obj. to follow

This topic is 3815 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 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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!