Ray casting

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Ray casting

Postby Rebornxeno on Tue Jun 18, 2013 5:03 pm

Yo yo wadup elysian shadowzzzz

What I want is to be able to cast a ray from the world-space position of any pixel on my screen into the orthogonal(i think) direction of which my view matrix is pointing.
I have a left handed perspective matrix, a left handed view matrix. When I upload my matrices to my shader, I transpose them, I'm not exactly sure why, but that's just what I do, and not understanding this part might be part of my problem. So I take the pixel coordinate selected by my mouse and put it into screen space coordinates, in the -1 to 1 range. I take the inverse of my transposed view and transposed perspective matrices. I multiply my ss(screen space) coords by the inverse perspective first, then divide by w. Then I multiply by the inverse view, and divide by w(w is usually 1 here, so nothing is changed). According to my scientific calculations, I should have a point in world-space that corresponds to my selected pixel coordinate, with a depth away from the screen depending on the z value I selected when picking my pixel coords. I draw a line from exactly the middle of my view (world space coordinate, same depth as the coordinate I calculated) to the position of my calculated coordinate. It is close to my mouse, and anywhere I look, it is close to my mouse,and moves with my mouse, but it isn't close enough.

Why? I think it might have to do with the point I calculated, when drawn, it is drawn in perspective, and thus is made smaller depending on the distance from my screen, and so it won't be 1:1 with where my selected pixel is, though I'm not sure how to test it, I think I'll have to make an orthogonal perspective function in my shader and draw it with this. Though even if I get it extremely close to my viewing plane, it doesn't look like it is close enough to my mouse for this to be the case. It isn't crazy far away, but it isn't close enough to being able to click on something near it's edge and have it detect.

This single thing has kept me from progressing on all the games I want to make. I have come so far, my engine and all the tools I've made are all just waiting on this one aspect that I just have not been able to figure out for MONTHS.(actually it's been like a year) So, I'm finally coming here to beg for help. PLEASE OMG HELP ME

Just tried orthographic perspective, and it is EXACTLY THE SAME. The distance from the point to the mouse is about 1 centimeter or half of 1 centimeter, always right below the mouse. I need this to be exactly at the mouse!

Okay, so I figured out that it was lower than my mouse because the height value calculated in the window border. Now that that's gone, it's closer to my mouse but still isn't right. And sometimes my value goes further and closer to my selected coordinates, which it shouldn't be doing.

Holy fucking shit. I just figured out the goddamn issue. When my window is being created, this function is called:
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
Adjusting only one reference to the rectangle of my screen, changing that rect (very important rect) from the rectangle my screen-to-world uses and my projection matrix. And that threw off everything. Now it's almost exactly at my mouse, though it could be better.

Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 12:12 pm

Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests