# [MDX] Help - Creating a Ray From Mouse Coords

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

## Recommended Posts

edit: Changed title to show help needed Other pertinent information on software being used: Visual C++ 2003 .Net 1.1 April 2006 DirectX SDK I think I have found why my picking tests are failing (as per forum message now on page 2 of this forum). I have a Primitive Class that allows me to create any primitive by passing Position, Normal, Colors etc and then draws them on the screen. I have a Plane class which uses it to generate x by y grids on the xz plane. This and other primitives draws fine. But while testing picking of a particular grid on the plane I was finding that it was either not finding an intersection point or finding the wrong one. So I thought I would create a line primitive to display the ray being generated. Only to find that the ray is not being generated from the mouse's position but from another point on the screen which is different as the screen is rotated. So I suspect my Ray calculation (based on various examples on the web) must be wrong. Here is the piece of code I am using for generating the ray. This code is executed as soon as I click the mouse button. The viewport is set when the device is initialised. The camera holds the projection and view transformations and the plane holding the world transformation. I am suspecting that my Ray Line Drawing should not include a normal ? Or should it but be set to a different value to the one I have there ? Can anyone point out where I have gone wrong here ? As for additional information that may make a difference the Far value of the camera projection in the code was set to 100000.0f or some such high figure. Should this ray far point be similarly set ? I have tried having the Ray start at RayPos and end at RayDir with similar lack of success. If you need anymore information just ask away as it may give me a clue to what I am missing. Thanks for any assistance and for taking the time to read this. Here's hoping someone can help me figure this out.
	Viewport vp;
Matrix world;
Matrix view;
Matrix projection;

vp = m_dxbase->Device->Viewport;
world = m_dxbase->Device->Transform->World;
projection = m_dxbase->Device->Transform->Projection;
view = m_dxbase->Device->Transform->View;

Vector3 RayPos;
RayPos = Vector3();
RayPos.X = (float)pArgs->X;
RayPos.Y = (float)pArgs->Y;

Vector3 Near;
Vector3 Far;

Near = Vector3::Unproject(Vector3(RayPos.X,RayPos.Y,0),__box(vp),world,view,projection);
Far = Vector3::Unproject(Vector3(RayPos.X,RayPos.Y,1),__box(vp),world,view,projection);

Vector3 RayDir;
RayDir = Vector3::Subtract(Far,Near);

m_ray->SetPosition(0,RayDir);
m_ray->SetNormal(0,Vector3(0.0f,1.0f,0.0f));
m_ray->SetColor(0,Drawing::Color::Red.ToArgb());
m_ray->SetPosition(1,RayPos);
m_ray->SetNormal(1,Vector3(0.0f,1.0f,0.0f));
m_ray->SetColor(1,Drawing::Color::Red.ToArgb());
DrawRay = true;


[Edited by - Xrystal on May 31, 2006 12:15:48 PM]

##### Share on other sites
Maybe our C# picking tutorial might be of use here. You question isn't completely clear to me though. Exactly what is the problem, is the ray not drawn correctly? If so, some drawing code would help :)

##### Share on other sites
Thanks for the reply. I'll take a look at that Picking link you gave and see if I can locate my problem.

As to the Ray Drawing here's the Ray Creation and Drawing steps.

Ray initialised in Form Initialisation Routine.

m_ray = __gc new DXPrimitive(m_dxbase->Device,0,2,DXBase::CustomVertexType::NormalisedColored);

The DXPrimitive Constructor being Used

UsingBuffers = false;m_device = device;m_cvt = cvt;m_vertexCount = vertexCount;m_indexCount = indexCount;if (indexCount > 0) m_indices = new Int32[indexCount];if (m_cvt == DXBase::CustomVertexType::NormalisedTextured) m_PNTVerts = __gc new CustomVertex::PositionNormalTextured[vertexCount];if (m_cvt == DXBase::CustomVertexType::NormalisedColored) m_PNCVerts = __gc new CustomVertex::PositionNormalColored[vertexCount];

Drawing the Ray in the Form OnPaint routine

if (DrawRay) m_ray->Draw(PrimitiveType::LineList,1);

The DXPrimitive Draw Routine

if (UsingBuffers){	m_device->SetStreamSource(0, m_vb, 0);	if (m_indexCount > 0)	{		m_device->DrawIndexedPrimitives(primitiveType,0,0,vertexCount,0,primitiveCount);	}	else	{		m_device->DrawPrimitives(primitiveType,0,primitiveCount);	}}else{	if (m_indexCount > 0)	{		m_device->DrawIndexedUserPrimitives(primitiveType,0,vertexCount,primitiveCount,(Object*)m_indices, false,vertices);	}	else	{		m_device->DrawUserPrimitives(primitiveType,primitiveCount,vertices);	}}

A copy of the display showing the ray drawn can be seen here (http://prog.swangen.co.uk/images/testray.jpg).

As you can see the ray is being drawn as per information being given to it and assuming the other items I draw via DXPrimitives (the Black and Red dots) draw fine the problem with the ray is the values being calculated.

Hope that helps you understand the problem but in the mean time I will check your link out.

##### Share on other sites
Ah yes, I did read through your code and some of it is in my code apart from the picking code as I am not using a mesh but a plane made of vertices and dimension testing which I have just added and it made a slight difference but the ray is still not travelling to or from the mouse cursor position.

Thanks for the assistance so far. It might have to wait until I get back home and have access to my math books and see what they tell me.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×