Jump to content
  • Advertisement
Sign in to follow this  
sofakng

How can I draw a ray? (ie. why is my simple code not working?)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm following the mouse picking tutorial but instead of trying to intersect objects with the ray I'm just trying to draw the ray itself. (I know that a ray is infinite but I'm trying to draw from it's starting position to 1000 units away from it) Here is how I'm creating my ray:
Vector3 nearSource = new Vector3(Mouse.GetState().X, Mouse.GetState().Y, camera.nearPlane); 
Vector3 farSource = new Vector3(Mouse.GetState().X, Mouse.GetState().Y, camera.farPlane); 
 
Vector3 nearPoint = Game.GraphicsDevice.Viewport.Unproject(nearSource, camera.projection, camera.view, Matrix.Identity); 
Vector3 farPoint = Game.GraphicsDevice.Viewport.Unproject(farSource, camera.projection, camera.view, Matrix.Identity); 
 
// find the direction vector that goes from the nearPoint to the farPoint 
// and normalize it.... 
Vector3 direction = farPoint - nearPoint; 
direction.Normalize(); 
 
// and then create a new ray using nearPoint as the source. 
lastRay = new Ray(nearPoint, farPoint); 
 
VertexPositionColor[] v = new VertexPositionColor[2]; 
v[0] = new VertexPositionColor(lastRay.Position, Color.Blue); 
v[1] = new VertexPositionColor(lastRay.Position * (lastRay.Direction * 1000), Color.Red); 
 
vb = new VertexBuffer(Game.GraphicsDevice, VertexPositionColor.SizeInBytes * 2, BufferUsage.WriteOnly); 
vb.SetData(v); 
...and here is how I'm drawing it:
if (vb != null) 
{ 
   GraphicsDevice.RenderState.DepthBufferEnable = true; 
   GraphicsDevice.RenderState.AlphaBlendEnable = false; 
 
   GraphicsDevice.VertexDeclaration = new VertexDeclaration(Game.GraphicsDevice, VertexPositionColor.VertexElements); 
 
   GraphicsDevice.Vertices[0].SetSource(vb, 0, VertexPositionColor.SizeInBytes); 
 
   effect.World = Matrix.Identity; 
   effect.View = camera.view; 
   effect.Projection = camera.projection; 
 
   effect.Begin(); 
   effect.EnableDefaultLighting(); 
 
   foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
   { 
      pass.Begin(); 
      GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 1); 
      pass.End(); 
   } 
 
   effect.End(); 
} 
Am I doing something wrong? I'm just trying to see the mouse-picking line before I use it...

Share this post


Link to post
Share on other sites
Advertisement
Did you try rendering a line with known values for the two points to make sure that works first?

I've found that trying to visualize a picking ray every frame is hard sometimes, and it doesn't really show up - you can only really see it if the camera is moving perpendicular to the ray (strafing). Though I was always casting from the center of the screen, I recall it hardly showing up even if I was using an arbitrary screen position.

Share this post


Link to post
Share on other sites
I guess you know that drawing a ray from the camera directly into the scene won't be visible?
You are looking at the end of it which would be 1pixel if you are lucky. Try setting the root of the ray somewhere else - Increase every component (X,Y,Z) by a few units for example.

Share this post


Link to post
Share on other sites
sometimes if you attached to texture while rendering without texture coordinates it fail to render correctly, try settexture to 0.

Share this post


Link to post
Share on other sites
Thanks for all of the replies.

I've found half of my problem: The nearSource Z coordinate must be 0 and the farSource Z coordinate must be 1. I was using my actual near and far plane values (1, and 1000).

Now my line shows up!

However, it seems to be drawn a little off-center from where I click.

Are my source and destination vertices correct for the line?

ray.Position (for the starting point), and ray.Position * (ray.Direction * 100) for the destination? (eg. I just want to draw the actual ray for about 100 units of length)

Share this post


Link to post
Share on other sites
Ok, I've found my problem but I'm really confused what is causing it.

After checking MouseState.MiddleButton (or any button) the X, Y mouse coordinates change!

For example:

MouseState ms = Mouse.MouseState()

if (ms.MiddleButton == ButtonState.Pressed)
{
// Do something
}

// ms.X and ms.Y might be (0, 0) at this point, but if you check Mouse.GetState().X/Y you might see (203, 481) or something extremely different.

This was causing my mouse pick ray to have incorrect coordinates...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!