Skip to main content
gamedev.net
🔒 Locked

(REALLY) Wierd Raycasting Problem

Started by rjahrman Aug 6, 2002 at 12:56 PM 7 replies 600+ views
Original Post
rjahrman
rjahrman
I'm new to 3D, and I'm trying to use raycasting in my game to see if the mouse is over the sun. However, when I have the cursor over the sun when the program starts, onsun is false. As I move towards the sun, it stays false until the moment I pass the sun. (Basically, it thinks I'm facing the sun when I'm facing the exact opposite direction.) Here's my code:
	D3DXVECTOR3 ForMouse;
	ForMouse = MGM_MakeRay(0.78f,4/3,g_Mouse.GetMousePos().x,g_Mouse.GetMousePos().y,100);

	float myc[3] = {0,0,0};
	float myo[3];
	g_Camera.GetPosition(myo[0],myo[1],myo[2]);
	float myd[3] = {ForMouse.x,ForMouse.y,ForMouse.z};
	float junk[3];

	if ( SphereRayCollision(myc,10,myo,myd,junk) ) {
		onsun = true;
	} else {
		onsun = false;
	}
    
And here is the MGM_MakeRay function:
D3DXVECTOR3 MGM_MakeRay(float FOV,float AspectRatio,WORD MouseX,WORD MouseY,float range)
{

	D3DXVECTOR3 LineEnd,CameraSpacePos;
	float NMouseX,NMouseY,det;
	D3DXMATRIX matView;

	NMouseX=1.0 - 2.0*MouseX/640;
	NMouseY=1.0 - 2.0*MouseY/480;

	//is FOV in radians
	CameraSpacePos.y=(NMouseY*tan(FOV/2.0));
	CameraSpacePos.x=((-NMouseX*AspectRatio)*tan(FOV/2.0));

	LineEnd.x=range*CameraSpacePos.x;
	LineEnd.y=range*CameraSpacePos.y;
	LineEnd.z=range;

	g_pDevice->GetTransform(D3DTS_VIEW,(D3DMATRIX *)&matView);
	D3DXMatrixInverse(&matView,&det,&matView);
	D3DXVec3TransformCoord(&LineEnd,&LineEnd,&matView);

	return LineEnd;

}
    
And here is the definition of SphereRayCollision: (point is the point of intersection it finds)
bool SphereRayCollision(float center[3], float radius,
                        float origin[3], float direction[3],
                        float point[3]);
    
Thanks (I'm using D3D 8 if it matters). EDIT: Got rid of those lines (see next post). [edited by - rjahrman on August 6, 2002 3:03:10 PM] [edited by - rjahrman on August 6, 2002 3:50:26 PM]
AxoDosS
AxoDosS
Hi!

Have you tested calculating whit the opposite direction?
Maybe you should not create a negative vector, which is what you do?

These lines should be deleted? Try it!

myd[0] = myd[0] * -1;
myd[1] = myd[1] * -1;
myd[2] = myd[2] * -1;

A little optimization tip!
myd[0] = -myd[0];
myd[1] = -myd[1];
myd[2] = -myd[2];
rjahrman
rjahrman
Woops, that line wasn''t supposed to be there. It originally wasn''t there, but that''s one thing I tried. The same thing happens. It also doesn''t work if both are negative.
AxoDosS
AxoDosS
Hi!

Hmm another thing can be that the z value should be -z
myo[2] = -myo[2]

Play around whit the code!
rjahrman
rjahrman
Your "optimization" doesn''t do the same thing. If it is already negative, your way will make it stay negative. My way will make it positive. Which way do I want?
AxoDosS
AxoDosS
Hi!

what is "myd" defined as?
myd[2] = -myd[2] should work on a normal C++ compiler?
It works for me.

[edited by - axodoss on August 6, 2002 3:50:29 PM]
rjahrman
rjahrman
Oh, now I see (I''m so "slow" sometimes ). And no, that didn''t do a thing (I also tried switching myd and myo in the SphereRayCollision call, but that made it so it never became true).
rjahrman
rjahrman
Argggghhhhhhh! What''s wrong?
Photon
Photon
You''re not taking into account the camera direction anywhere.
You''re only taking its position and calculating a ray that assumes the camera has a specific orientation.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.