Jump to content
  • Advertisement
Sign in to follow this  
kVandaele

(FIXED) Objects not translated

This topic is 5456 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

When i start the 3d engine, all objects have a different direction, but they're all spawned at the origin, and they don't move... The drawing code works fine, or at least it was before when i had 4 objects (not in a vector) and had them turn like clockwork (using timeGetTime as rotation).
// code called in main program
// m_pFlightShips is a vector of pointers, pointing to a CFlightShip object
		for(DWORD i=0; i<m_pFlightShips.size(); i++){
			m_pFlightShips.at(i)->UpdatePos(m_dwTimePassed);
			m_pFlightShips.at(i)->GetMatrix(&matObject);
			m_pGfx->GetDevice()->SetTransform(D3DTS_WORLD, &matObject);
			m_pShipMeshes.at(m_pFlightShips.at(i)->GetType())->Render();
		}


void CFlightShip::GetMatrix(D3DXMATRIX* mat){
	// Advanced 3D GAME Programming with DirectX 9.0
	// page 166 - 167 'black box'
	//... except, NOT!
// Note: I don't quite know what above comments mean (even if i did make them myself)

	D3DXMATRIX matRot1;
	D3DXMATRIX matRot2;
	D3DXMATRIX matMove;
	D3DXMatrixIdentity(mat);
	D3DXMatrixTranslation(&matMove, m_Dir.x, m_Dir.y, m_Dir.z);

	D3DXMatrixRotationX(&matRot1, atan2f(m_Dir.y, m_Dir.z));
	D3DXMatrixRotationY(&matRot2, atan2f(m_Dir.z, m_Dir.x));

	D3DXMatrixMultiply(mat, &matRot1, &matRot2);

	D3DXMatrixRotationAxis(&matRot1, &m_Dir, atan2f(m_Dir.y, m_Dir.x));

	D3DXMatrixMultiply(mat, &matRot1, mat);
//	D3DXMatrixMultiply(mat, &matMove, mat);
//	D3DXMatrixMultiply(mat, mat, &matRot1);
//	D3DXMatrixMultiply(mat, mat, &matMove);
// Note: i've been struggling with this for a while, so i think two of the 4 don't need to be commented
}


void CFlightShip::UpdatePos(DWORD dwTimeElapsed){
//	New position = old position + (direction * distance moved)
	m_Pos = m_Pos + m_Dir * ((float)dwTimeElapsed/1000.0f * m_fVel);
}


[Edited by - kVandaele on October 15, 2004 12:57:30 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
D3DXMatrixTranslation(&matMove, m_Dir.x, m_Dir.y, m_Dir.z);

Should that not be m_Pos? [grin]

EDIT: and your update looks borked to me... I'm used to using D3D functions for adding vectors (you are using vectors, right?) but if you can do it without them, cheers!

Share this post


Link to post
Share on other sites
THAT's the bug i've been looking for for a week???
Anyway, that did fix it. Something's wrong in my randomisation of direction and position, but that's most likely because i didn't remember that it needs negative values as well (duh).

As for my use of just the '+' operator and not a D3D function on the D3DXVECTOR3 in UpdatePos:


// Source: DirectX 8.1 Documention, C++ segment
typedef struct D3DXVECTOR3 : public D3DVECTOR {
public:
D3DXVECTOR3() {};
D3DXVECTOR3( CONST FLOAT * );
D3DXVECTOR3( CONST D3DVECTOR& );
D3DXVECTOR3( FLOAT x, FLOAT y, FLOAT z );

// casting
operator FLOAT* ();
operator CONST FLOAT* () const;

// assignment operators
D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& );
D3DXVECTOR3& operator -= ( CONST D3DXVECTOR3& );
D3DXVECTOR3& operator *= ( FLOAT );
D3DXVECTOR3& operator /= ( FLOAT );

// unary operators
D3DXVECTOR3 operator + () const;
D3DXVECTOR3 operator - () const;

// binary operators
D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;
D3DXVECTOR3 operator - ( CONST D3DXVECTOR3& ) const;
D3DXVECTOR3 operator * ( FLOAT ) const;
D3DXVECTOR3 operator / ( FLOAT ) const;

friend D3DXVECTOR3 operator * ( FLOAT, CONST struct D3DXVECTOR3& );

BOOL operator == ( CONST D3DXVECTOR3& ) const;
BOOL operator != ( CONST D3DXVECTOR3& ) const;

} D3DXVECTOR3, *LPD3DXVECTOR3;

// My UpdatePos again for easy referencing:
void CFlightShip::UpdatePos(DWORD dwTimeElapsed){
// New position = old position + (direction * distance moved)
m_Pos = m_Pos + m_Dir * ((float)dwTimeElapsed/1000.0f * m_fVel);
}



I'm no expert so perhaps i misunderstood, but i see a + operator overloaded for a vector (vector + vector), which is m_Pos + m_Dir. I also see a * operator overloaded for a constant, which is m_Dir * (float). I hope that explains it and simplifies things on your future endeavours :)

Oh ya, did i mention? Thanks!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!