Check if objects intersects between two points?

Started by
27 comments, last by perroned 13 years, 2 months ago
@Buckeye @nobodynews thankyou you both
I thot the parameters were like parameter 0, 1, 2, 3, 4 not 1, 2, 3, 4

So yes I fixed that :)

So I unprojected p1 as well after changing v.z to one, then did the subtraction like you said
So is this correct?

D3DXVECTOR3 v;
v.x = float(pd3dsdBackBuffer->Width /2);
v.y = -(float(pd3dsdBackBuffer->Height /2));
v.z = 0.0f;

const D3DXMATRIX* pmatProj = g_Camera.GetProjMatrix();
const D3DXMATRIX matView = *g_Camera.GetViewMatrix();
const D3DXMATRIX matWorld = *g_Camera.GetWorldMatrix();
D3DVIEWPORT9 tempmatViewPort;
g_pd3dDevice->GetViewport( &tempmatViewPort );
const D3DVIEWPORT9 matViewPort = tempmatViewPort;
D3DXVECTOR3 p0, p1;
D3DXVec3Unproject(&p0, &v, &matViewPort, pmatProj, &matView, &matWorld );// origin

v.z = 1.0f;
D3DXVec3Unproject(&p1, &v, &matViewPort, pmatProj, &matView, &matWorld );// direction

p1 -= p0;


Thank you all very much :)

Advertisement
Looks good. If you want to double check (and give yourself some headaches), you can use D3DXVec3Project() to convert p0 back to screen space and check that it matches ( width/2, height/2, 0 ).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


Looks good. If you want to double check (and give yourself some headaches), you can use D3DXVec3Project() to convert p0 back to screen space and check that it matches ( width/2, height/2, 0 ).



Well I projected like u said, and have a small headache :P

p0.x equals 633867
and
p0.y equals 356376

while
pd3dsdBackBuffer->Width equals 1366
and
pd3dsdBackBuffer->Height equals 768

And
1366/2 = 683
768/2 = 384

So i guess because they are floating points with numerous digits, is 633 == 683, and 384 == 356 close enough? Like should they be EXACTLY the same or is this normal? :)
While it's true that:
1366/2 = 683
768/2 = 384

683 certainly does not equal 633867. Did you forget a decimal point?

Your projected values immediately obtained from the unprojected values should be nearly identical. No, 633 is not nearly close enough to 683. If you get 683.001 or something similar, that's close.

You should be able to do something like:


D3DXVec3Unproject( &p0, &v, ...parameters...);
D3DXVECTOR3 vreturn;
D3DXVec3Project( &vreturn, &p0, ... SAME parameters..);


vreturn and v should be within fractions of a decimal point.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


While it's true that:
1366/2 = 683
768/2 = 384

683 certainly does not equal 633867. Did you forget a decimal point?

Your projected values immediately obtained from the unprojected values should be nearly identical. No, 633 is not nearly close enough to 683. If you get 683.001 or something similar, that's close.

You should be able to do something like:


D3DXVec3Unproject( &p0, &v, ...parameters...);
D3DXVECTOR3 vreturn;
D3DXVec3Project( &vreturn, &p0, ... SAME parameters..);


vreturn and v should be within fractions of a decimal point.


ok thankyou so I tested the code you wrote and yes they match exactly :)

So I just have one last question :P
So with this ray if I was going to test it against a bounding box would I just do it like this:
[color="#0000ff"][color="#0000ff"][color="#000000"](Assuming vMin [color="#000000"]and vMax are already filled out)
D3DXBoxBoundProbe( &vMin, &vMax, &p0, &p1 )

Thankyou once again for all you help. Much appreciated :)
If vMin and vMax are set correctly, D3DXBoxBoundProbe should work.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


If vMin and vMax are set correctly, D3DXBoxBoundProbe should work.



ok thank you sooo much :)
I have been searching all over and you finally helped me solve it :)
thanks alot :)

now i just need to find y it wont intersect with my bounding box ;P
It's likely that you don't use the object's world matrix to calculate the ray vectors. In your code above, you use the camera's world matrix. You need to use the world matrix of the object you want to intersect.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


It's likely that you don't use the object's world matrix to calculate the ray vectors. In your code above, you use the camera's world matrix. You need to use the world matrix of the object you want to intersect.



So rewrite everything again just using the world matrix? :)

This topic is closed to new replies.

Advertisement