setPosition from getPosition

OGL

21-05-2009 21:10:07

Hi,
I made a few cars and now I would like to attach a camera to one of them. I do this by:
mCamera->setPosition(car_snSubaru->getPosition());
car_snSubaru->attachObject(mCamera);

but the camera does not appear there where the vehicle stands.

something similar takes place when I would like to add a vehicle in place where currently is a camera. I do this by:
car_snSubaru->setPosition(mCamera->getPosition());
but again, the coordinates of the car does not correspond to those in which the camera happens to be. The vehicle is inserted at the corner of the board.

Thx for any suggestions

zillenjack

22-05-2009 04:02:36

Do you want to achieve the third-person feature?

OGL

22-05-2009 12:02:31

Yes, that's what I want to do...

zillenjack

24-05-2009 04:59:46

This is my code:

Variable Description:
D3DXVECTOR3 position; //Object's position
D3DXVECTOR3 offset;//Camera to object distance
D3DXVECTOR3 lookat;//Observation direction
float yaw;//Y Rotation
float pitch;//X Rotation

when frameStared

Vector3 m_CameraLookAt;
Vector3 m_CameraPosition;
m_camera->GetPositionAndLookAt(&m_CameraPosition,&m_CameraLookAt);
mCamera->setPosition(m_CameraPosition);
mCamera->lookAt(m_CameraLookAt);


CameraManager.h
#pragma once

#include <d3d9.h>
#include <d3dx9math.h>
#include <Ogre.h>

using namespace Ogre;

class CCameraManager
{
public:
CCameraManager(void);
virtual ~CCameraManager(void);
D3DXVECTOR3 position;
D3DXVECTOR3 offset;
D3DXVECTOR3 lookat;
float yaw;
float pitch;
void Forward(float speed);
void Rotation(float speed);
void Pitch(float speed);
void Strafe(float speed);
void zoom(float speed);
void GetPositionAndLookAt(OUT D3DXVECTOR3* _position,OUT D3DXVECTOR3* _lookat);
void GetPositionAndLookAt(OUT Vector3* _position,OUT Vector3* _lookat);
};

CameraManager.cpp
#include "StdAfx.h"
#include "CameraManager.h"

CCameraManager::CCameraManager(void)
{
yaw=0;
pitch=0;
position=D3DXVECTOR3(0,0,0);
offset=D3DXVECTOR3(0,0,-100);
lookat=D3DXVECTOR3(0,0,100);
}

CCameraManager::~CCameraManager(void)
{
}

void CCameraManager::Forward(float speed)
{
D3DXMATRIX rotation;
D3DXMatrixRotationY(&rotation,yaw);
D3DXVECTOR3 vec;
D3DXVec3TransformCoord(&vec,&D3DXVECTOR3(0,0,1),&rotation);
position += vec * speed;
}

void CCameraManager::Rotation(float speed)
{
yaw += speed;
}

void CCameraManager::Strafe(float speed)
{

}

void CCameraManager::zoom(float speed)
{

}

void CCameraManager::GetPositionAndLookAt(OUT D3DXVECTOR3* _position,OUT D3DXVECTOR3* _lookat)
{
D3DXVECTOR3 _o,_l;

D3DXMATRIX rotation;
D3DXMatrixRotationY(&rotation,yaw);
D3DXVECTOR3 axis;
D3DXVec3TransformCoord(&axis,&D3DXVECTOR3(1,0,0),&rotation);
D3DXMATRIX pitchMatrix;
D3DXMatrixRotationAxis(&pitchMatrix,&axis,pitch);

D3DXVec3TransformCoord(&_l,&lookat,&rotation);
D3DXVec3TransformCoord(&_l,&_l,&pitchMatrix);
D3DXVec3TransformCoord(&_o,&offset,&rotation);
D3DXVec3TransformCoord(&_o,&_o,&pitchMatrix);

D3DXVECTOR3 o=_o + position;
D3DXVECTOR3 l=_l + position;
_position->x=o.x;
_position->y=o.y;
_position->z=o.z;
_lookat->x=l.x;
_lookat->y=l.y;
_lookat->z=l.z;
}

void CCameraManager::GetPositionAndLookAt( OUT Vector3* _position,OUT Vector3* _lookat )
{
D3DXVECTOR3 _o,_l;

D3DXMATRIX rotation;
D3DXMatrixRotationY(&rotation,yaw);
D3DXVECTOR3 axis;
D3DXVec3TransformCoord(&axis,&D3DXVECTOR3(1,0,0),&rotation);
D3DXMATRIX pitchMatrix;
D3DXMatrixRotationAxis(&pitchMatrix,&axis,pitch);

D3DXVec3TransformCoord(&_l,&lookat,&rotation);
D3DXVec3TransformCoord(&_l,&_l,&pitchMatrix);
D3DXVec3TransformCoord(&_o,&offset,&rotation);
D3DXVec3TransformCoord(&_o,&_o,&pitchMatrix);

D3DXVECTOR3 o=_o + position;
D3DXVECTOR3 l=_l + position;
_position->x=o.x;
_position->y=o.y;
_position->z=o.z;
_lookat->x=l.x;
_lookat->y=l.y;
_lookat->z=l.z;
}
void CCameraManager::Pitch( float speed )
{
pitch+=speed;
}