Skip to main content
gamedev.net
🔒 Locked

D3DXIntersect

Started by Innovatus 2 replies 950+ views
Original Post
Innovatus
Innovatus
D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL); Is there a way to get world space X,Y,Z cords at the point of intersection from this function. I am trying to move a Mesh to the point where a mouse click intersects my terrain. I want to move it to the X,Y,Z position where the ray intersects not just to the intersected face. If anyone knows a way to do this not use D3DXIntersect that is fine too. Inno
Inno
mdias
mdias
You''ll have to get the matrix of the object as you do when you
render the object and then calculate the point by yourself.
DX8 SDK tells how to calculate it, it''s something like it:

  
D3DXMATRIX Mat_World;
... //Get the transformation matrix of the object


// Calculate the new coordinates

x = (x*Mat_World._11) + (y*Mat_World._21) + (z*Mat_World._31) + Mat_World._41;
y = (x*Mat_World._12) + (y*Mat_World._22) + (z*Mat_World._32) + Mat_World._41;
z = (x*Mat_World._13) + (y*Mat_World._23) + (z*Mat_World._33) + Mat_World._43;



x , y and z are theposition of the object.

It''s kinda hard to use D3DXIntersect, it should test the transformed
position/rotation/scaling, not the original position/rotation/scaling

KaMiKaZe
Lupin
Lupin
this comes directly from my C++ DLL used for collision detection, I wrote this DLL to be used in an VB app.

DirTriCollision checks for collision between an point, an direction (both vectors are building up an endless ray) and an triangle.

RayTriCollision does nearly the same, but it only checks the collision between two points (start and END-point of the ray) and an triangle

this code uses also some standard vector math functions (not included)



  //This DLL is used for ray-triangle collision detection

#pragma warning (disable:4244) //disable the error about double-float conversions. irritating.


#include <math.h>
#include "main.h"


//Funktionen

_declspec( dllexport ) float _stdcall Vec3Dist(D3DVECTOR& vVect1, D3DVECTOR& vVect2)
{
float Distance = 0;
D3DVECTOR tdVect;

tdVect = vVect1 - vVect2;
Distance = Vec3Length(tdVect);
return Distance;
}

_declspec( dllexport ) D3DVECTOR _stdcall CalculateNormal(D3DVECTOR& vP0, D3DVECTOR& vP1, D3DVECTOR& vP2)
{
return Vec3Cross(vP1 - vP0, vP2 - vP0);
}

_declspec( dllexport ) short _stdcall GetSPoint(D3DVECTOR& vOut, D3DVECTOR& vP0, D3DVECTOR& vP1, D3DVECTOR& vP2, D3DVECTOR& vPv, D3DVECTOR& vUv)
{
D3DVECTOR ENorm;
D3DVECTOR vTmp;
float Eb = 0;
float Temp = 0;
float t = 0;
short Result = 0;

ENorm = CalculateNormal(vP0, vP1, vP2);
Eb = Vec3Dot(ENorm, vP0) - Vec3Dot(ENorm, vPv);

Temp = Vec3Dot(ENorm, vUv);

if(Temp != 0) {
t = Eb / Temp;
Result = 1;
} else {
Result = 0;
}

vTmp = vUv * t;
vOut = vPv + vTmp;

return Result;
}

_declspec( dllexport ) short _stdcall TriangleMatch(D3DVECTOR& P1, D3DVECTOR& P2, D3DVECTOR& P3, D3DVECTOR& Test_Point)
{
D3DVECTOR nE;
D3DVECTOR tP;
short tBool = 0;
D3DVECTOR spanTP;

nE = CalculateNormal(P1, P2, P3);
nE = Vec3Normalize(nE);

spanTP.x = Test_Point.x - P1.x;
spanTP.y = Test_Point.y - P1.y;
spanTP.z = Test_Point.z - P1.z;

tP.x = P3.x + nE.x;
tP.y = P3.y + nE.y;
tP.z = P3.z + nE.z;

tBool = GetSPoint(tP, P2, P3, tP, P1, spanTP);

if (Vec3Dist(P1, Test_Point) <= Vec3Dist(P1, tP) && Vec3Dist(Test_Point, tP) <= Vec3Dist(P1, tP) && Vec3Dist(P2, tP) <= Vec3Dist(P2, P3) && Vec3Dist(P3, tP) <= Vec3Dist(P2, P3)) {
return 1;
} else {
return 0;
}
}

_declspec( dllexport ) short _stdcall RayTriCollision(D3DVECTOR& vOut, D3DVECTOR& P0, D3DVECTOR& P1, D3DVECTOR& P2, D3DVECTOR& vFrom, D3DVECTOR& vTo)
{
D3DVECTOR tVec;
D3DVECTOR tVec2;
D3DVECTOR vTo2;

vTo2 = vFrom - vTo;

if(GetSPoint(tVec, P0, P1, P2, vFrom, vTo2) == 1) {
tVec2 = tVec - vFrom;
tVec2 = Vec3Normalize(tVec2);

vTo2 = Vec3Normalize(vTo);
if(Vec3Dot(tVec2, vTo2) > 0) {
if(TriangleMatch(P0, P1, P2, tVec) == 1) {
vOut = tVec;
return 1;
}
}
}
return 0;
}


_declspec( dllexport ) short _stdcall DirTriCollision(D3DVECTOR& vOut, D3DVECTOR& P0, D3DVECTOR& P1, D3DVECTOR& P2, D3DVECTOR& vFrom, D3DVECTOR& vDir)
{
D3DVECTOR tVec;
D3DVECTOR tVec2;
D3DVECTOR vDir2;

vDir2 = Vec3Normalize(vDir);

if(GetSPoint(tVec, P0, P1, P2, vFrom, vDir2) == 1) {
tVec2 = tVec - vFrom;
tVec2 = Vec3Normalize(tVec2);
if(Vec3Dot(tVec2, vDir2) > 0) {
if(TriangleMatch(P0, P1, P2, tVec) == 1) {
vOut = tVec;
return 1;
}
}
}
return 0;
}


[edited by - Lupin on December 18, 2002 9:54:50 AM]
Innovatus
Innovatus
I found the answer to this question

D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL);

Is there a way to get world space X,Y,Z cords at the point of intersection from this function. I am trying to move a Mesh to the point where a mouse click intersects my terrain. I want to move it to the X,Y,Z position where the ray intersects not just to the intersected face.

IntersectPoint.x = vPickRayOrig.x + fDist * vPickRayDir.x
IntersectPoint.y = vPickRayOrig.y + fDist * vPickRayDir.y
IntersectPoint.z = vPickRayOrig.z + fDist * vPickRayDir.z

remember to convert the ray to the worldspace of the object you are checking for intersection.

if anyone would like the full source on how to do this I''d be happy to provide it to them =)



Inno
Inno

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.