# D3DXIntersect and world transformation

This topic is 4639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, sorry for my terrible english at first... I want to use D3DXIntersect function to check for collisions between my mesh and ray. But D3DXIntersect does not take any world transformation matrix, so, if I understand correctly, I must transform my input RayPos and RayDir vectors by world transformation matrix of my mesh. So far, so good, transformation of RayPos is no problem (just inverted translation part of transformation matrix + actual RayPos) But what about rotation of my mesh? I have found somewhere that I should transform RayDir by world matrix of the tested mesh using D3DXVec3TransformNormal function, but this look strange for me because I should rotate mesh vertices not direction of RayDir... Or maybe I'm wrong and this solution is good? Thanks for any help...

##### Share on other sites
Hey domin,

i've just got some intersection code working in my app, the way i go about it is by transforming the ray( both position and direction ) by the inverse of the object's world matrix. That way they're both in the same space( at least this is my understanding of it ).

here is my code, just in case you want a look, i only use bounding sphere collision thou, but shouldn't be hard to change to mesh collision
bool CMenuNode::RayIntersect( SRay p_pRay ) {	BoundingSphere TempBS = m_pModel->GetBoundingSphere();	// Bring the ray into this node's object space	D3DXMATRIX worldInverse;	D3DXMatrixInverse( &worldInverse, 0, &m_mCombinedWorld ); // i have a hierarchy system, so just chenge m_mCombinedWorld with your world matrix	D3DXVec3TransformCoord( &p_pRay.m_vRayPosition, &p_pRay.m_vRayPosition, &worldInverse );	D3DXVec3TransformNormal( &p_pRay.m_vRayDirection, &p_pRay.m_vRayDirection, &worldInverse );	D3DXVec3Normalize( &p_pRay.m_vRayDirection, &p_pRay.m_vRayDirection );//------------------------------------------------------------------------------// Basically here down is bounding sphere collision//------------------------------------------------------------------------------	D3DXVECTOR3 vTemp = p_pRay.m_vRayPosition - TempBS.m_vCenter;	float b = 2.0f * D3DXVec3Dot( &p_pRay.m_vRayDirection, &vTemp );	float c = D3DXVec3Dot( &vTemp, &vTemp ) - ( TempBS.m_fRadius * TempBS.m_fRadius );	// find the discriminant	float fDiscriminant = ( b * b ) - ( 4.0f * c );	// test for imaginary number	if( fDiscriminant < 0.0f ) {		return false;	}	fDiscriminant = sqrtf( fDiscriminant );	float s0 = ( -b + fDiscriminant ) / 2.0f;	float s1 = ( -b - fDiscriminant ) / 2.0f;	// if a solution is >= 0 , then we have an intersection	if( s0 >= 0.0f || s1 >= 0.0f ) {		return true;	}	return false;}

hope this helps mate....

##### Share on other sites
Thans for your reply NovaCaine - I do it in a similar way - just asking to be sure that this mehod is correct.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999589
×