Ray pick (again) ?

Started by
6 comments, last by Mythar 18 years, 5 months ago
Hi all, i ran into a new problem with my ray picker. Normal source :

void calcRay(int x,int y,D3DVECTOR &p1,D3DVECTOR &p2)
{
	float dx,dy;
	D3DMATRIX invMatrix,viewMatrix;

	dx=tanf(FOV*0.5f)*(x/WIDTH_DIV_2-1.0f)/ASPECT;
	dy=tanf(FOV*0.5f)*(1.0f-y/HEIGHT_DIV_2);
	lpDevice->GetTransform(D3DTRANSFORMSTATE_VIEW,&viewMatrix);
	D3DMath_MatrixInvert(invMatrix,viewMatrix);
	p1=D3DVECTOR(dx*NEAR,dy*NEAR,NEAR);
	p2=D3DVECTOR(dx*FAR,dy*FAR,FAR);
	D3DMath_VectorMatrixMultiply(p1,p1,invMatrix);
	D3DMath_VectorMatrixMultiply(p2,p2,invMatrix);
}

This works nicely for a "*LEFT HAND" system, but how would i change it, to work in a "*RIGHT HAND" system ? *LEFT HAND = D3DXMatrixLookAtLH + D3DXMatrixPerspectiveFovLH *RIGHT HAND = D3DXMatrixLookAtRH + D3DXMatrixPerspectiveFovRH
Advertisement

For a right handed coordinate system you need to negate the Z components of your direction vector.
Joshua Barczak3D Application Research GroupAMD
Thx that worked :)

btw, i just ran into another problem, what about "Ortho" views (both Left and Right Hand) ?
Any know how to do the Ray when using D3DMatrixOrthoLH, D3DMatrixOrthoRH, please ?

edit : hmm, guess I need to use D3DXVec3Unproject , only problem is ; i dont use D3DX, and I wont use it until MS includes it (25/26) in the retail ver. of DX.

Any have the source for D3DXVec3Unproject ? or another way of doing it ?

[Edited by - Mythar on October 24, 2005 8:06:09 AM]
Quote:Original post by Mythar
Any know how to do the Ray when using D3DMatrixOrthoLH, D3DMatrixOrthoRH, please ?


Umm, well unless I'm misunderstanding what you are trying to do, there is no need to ray pick objects drawn in ortho view: they are already in screen coordinates.

Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
huh? if I dont use ray pick, how would I, pick objects in a view like this ? :



Edit : above image made with D3DMatrixOrthoRH
It depends on how you setup your ortho view. Two popular ways are as follows:

Width = Height = 1.0f
or
Width = 800, Height = 600 (for an 800x600 screen)

Either way you do it, you have to scale the vertices of the objects you draw. Taking your sphere as an example, and using an ortho of 0.0 to 1.0, you would have the vertices such that the left tangent of the sphere would reside at around 0.4f and the right around 0.6f. If you check the sphere's bounding sphere radius off center against the scaled mouse position (scaled from screen coords to 0.0 to 1.0 coords), you should determine if the sphere was clicked on.

In other words, the mouse click exactly at the center of the screen would yield 0.5f, 0.5f in coordinates of 0.0 to 1.0 or would yield 400x300 in pixel coordinates. Depending on how you setup your ortho projection, you'll have to scale from one to the other, which is simple. Then just take your mouse point, turn it into a vector with the origin at the center of the sphere, then see if it's magnitude is larger than the sphere's radius. There's probably a simpler way too, that doesn't require a square root. You could square the radius and compare that to the vector's squared magnitude.
Chris ByersMicrosoft DirectX MVP - 2005
Quote:Original post by Supernat02

Either way you do it, you have to scale the vertices of the objects you draw.


That is not realy an option, it would take to long. Keep in mind that the Sphere is not a Sphere; but a collection of triangles.

There must be a way to calc. a ray for this ?

This topic is closed to new replies.

Advertisement