Sign in to follow this  
cathode26

I am having trouble with intersecting a ray and sprite

Recommended Posts

Hi everyone, I am trying to do collision detection. I am: clicking on the screen returning the point that I clicked on turning the point into a ray turning 4 points into a plane, the 4 points of a sprite seeing if the ray intersects with the plane I have never done this before and I have been looking on the net for how to do it. Something is going wrong. If anyone can point me to a correct example or if they see something wrong can you please help me. The idea of how to do it came from this example http://answers.google.com/answers/threadview?id=18979 Thanks in advance to anyone that sees what I am doing wrong. -Vincent int mousex; //your current mouse positions int mousey; //Calculate a picking ray from the mouse positions //I am using directX //i need to get the projection matrix D3DXMATRIX proj; g_d3ddevice->GetTransform(D3DTS_PROJECTION, &proj); //I need the viewport D3DVIEWPORT9 vp; g_d3ddevice->GetViewport(&vp); //this will get me my points p.x = (((2.0f*x) / vp.Width) - 1.0f) / proj(0,0); p.y = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1,1); p.z = 1.0f; //Ray is a struct containing 2, 3d vectors, the origin o and the direction d Ray myRay; myRay.o = D3DXVECTOR3(g_nScreenWidth/2.0f, 384.0f, -350.0f); myRay.d = D3DXVECTOR3(p.x, p.y, p.z); //I need to transform the ray from view space to world space to do that I need //to get the inverse of the view transformation matrix D3DXMATRIX view; g_d3ddevice->GetTransform(D3DTS_VIEW, &view); //inverse of the view matrix D3DXMATRIX vI; //viewInverse D3DXMatrixInverse(&vI, 0, &view) Ray tRay; //temp ray tRay.d.x = myRay->d.x*vI->_11 + myRay->d.y*vI->_21 + myRay->d.z*vI->_31; tRay.d.y = myRay->d.x*vI->_12 + myRay->d.y*vI->_22 + myRay->d.z*vI->_32; tRay.d.z = myRay->d.x*vI->_13 + myRay->d.y*vI->_23 + myRay->d.z*vI->_33; myRay->o.x = vI->_41; myRay->o.y = vI->_42; myRay->o.z = vI->_43; myRay->d.x = tRay.d.x; myRay->d.y = tRay.d.y; myRay->d.z = tRay.d.z; //Now my ray needs to be transformed from world to object space D3DXMATRIX world; device->GetTransform(D3DTS_WORLD, &world); D3DXMATRIX matWorldInverse; D3DXMatrixInverse(&matWorldInverse,NULL,&world); D3DXVECTOR3 rayObjOrigin,rayObjDirection; D3DXVec3TransformCoord(&myRay.o,&myRay.o,&matWorldInverse); D3DXVec3TransformNormal(&myRay.d,&myRay.d,&matWorldInverse); D3DXVec3Normalize(&myRay.d,&myRay.d); //So now my ray is finished //I am trying to click on barney, barney is a sprite //Created using this function /* ID3DXSprite::Draw Adds a sprite to the list of batched sprites. HRESULT Draw( LPDIRECT3DTEXTURE9 pTexture, CONST RECT * pSrcRect, CONST D3DXVECTOR3 * pCenter, CONST D3DXVECTOR3 * pPosition, D3DCOLOR Color ); */ //barney is a 256x256 image so I gave him a bounding square //The position he is originally given in the draw function is this // x = 256 /2.0f; // y = 256; // z = 500; // and I think that these points are the top left corner of the image // so I define these vectors to represent the image D3DXVECTOR3 barneyP1 = D3DXVECTOR3(128, 256, 500); D3DXVECTOR3 barneyP2 = D3DXVECTOR3(384, 256, 500); D3DXVECTOR3 barneyP3 = D3DXVECTOR3(384, 0, 500); D3DXVECTOR3 barneyP4 = D3DXVECTOR3(128, 0, 500); //defining these points in a clock wise order as a bounding rectangle //for barney //Now I am going to turn these vectors into a plane //I saw an example saying to use cramer's rule float A = barneyP1.y * (barneyP2.z - barneyP3.z) + barneyP2.y * (barneyP3.z - barneyP1.z) + barneyP3.y * (barneyP1.z - barneyP2.z); float B = barneyP1.z * (barneyP2.x - barneyP3.x) + barneyP2.z * (barneyP3.x - barneyP1.x) + barneyP3.z * (barneyP1.x - barneyP2.x); float C = barneyP1.x * (barneyP2.y - barneyP3.y) + barneyP2.x * (barneyP3.y - barneyP1.y) + barneyP3.x * (barneyP1.y - barneyP2.y); float D = -barneyP1.x * (barneyP2.y * barneyP3.z - barneyP3.y * barneyP2.z) - barneyP2.x * (barneyP3.y * barneyP1.z - barneyP1.y * barneyP3.z) - barneyP3.x * (barneyP1.y * barneyP2.z - barneyP2.y * barneyP1.z); //then create the plane and substitute the ray into plane and solve for t //o is for origin //d is for direction float t = -(A * myRay.o.x + B*myRay.o.y + C*myRay.o.z + D) / (A*myRay.d.x + B*myRay.d.y + C*myRay.d.z); //then get your intersection point D3DXVECTOR3 intersectionPoint = D3DXVECTOR3((myRay.o.x) + (myRay.d.x * t), (myRay.o.y) + (myRay.d.y * t), (myRay.o.z) + (myRay.d.z * t)); //Now we need to trim the x and y axis on the plane and test for intersection //we can do this using //create a vector from A to the Intersection //create a vector from C to the Intersection D3DXVECTOR3 V3 = intersectionPoint - barneyP1; D3DXVECTOR3 V4 = intersectionPoint - barneyP3; //a vector from A to B //a vector from C to D D3DXVECTOR3 V1 = barneyP2 - barneyP1; D3DXVECTOR3 V2 = barneyP4 - barneyP3; //Normalize those vectors D3DXVec3Normalize(&V1,&V1); D3DXVec3Normalize(&V2,&V2); D3DXVec3Normalize(&V3,&V3); D3DXVec3Normalize(&V4,&V4); //dot product them float a = D3DXVec3Dot(&V1,&V4); float b = D3DXVec3Dot(&V3,&V5); if(a>0 && b>0) return true //for intersection to be true

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this