How 2 use D3DXIntersect?

Started by
18 comments, last by ParityCheck 20 years, 4 months ago
The D3D_MYVERTEX2 isn''t a datatype but just a defined combo of flags
#define D3D_MYVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)


the thing is that I don''t seem to get any hresult value at all the code just seems to ''blow up'' within the statement
D3DXComputeBoundingSphere( pVertices, lpMesh->GetNumVertices(), D3DXGetFVFVertexSize(lpMesh->GetFVF()), pCenter, pRadius);


what could be the problem with this guys I really appreciate the help
Keep coding..
Advertisement
Are you running with the Debug Runtime installed? What does the debug spew say?

Also, make sure that pCenter and pRadius are really valid pointers that indeed point to something. Since you never allocate any space for these variables in your constructor, then I''m not sure they''re actually valid. Your CMesh class definition should be something like this:
class CMesh{public:  CMesh();  virtual ~CMesh();  // more stuff....protected:  D3DXVECTOR3 vCenter;  FLOAT fRadius;  // more stuff....} 

Then your call to D3DXComputeBoundingSphere would look like this:
if( FAILED( D3DXComputeBoundingSphere( pVertices, lpMesh->GetNumVertices(), D3DXGetFVFVertexSize(lpMesh->GetFVF()), &vCenter, &fRadius ) ) )  MessageBox( NULL, "D3DXComputeBoundingSphere messed up", "Error", MB_OK ); 

I think that the problem is that you never allocated space to store the center and radius of your bounding sphere.

Do yourself a favor and use the Debug Runtimes. They''re completely invaluable for debugging code!

Hope this helps,
neneboricua
I''ve got the debug runtime installed, but I''m not quite sure

how it works. I''ve set all the options mentioned, but my

program seems to exit as usual with out displaying any sort

of message as to what went wrong(assuming that the debug

runtime does that) . My app is running in full screen, but

I don''t think that should be a problem.

I''m initialisng my pointers pCenter and pRadius to Null, is

that possibly cousing trouble.

I mean like my program doesn''t even go pass the

D3DXComputeBoundingShere code.. even enclosing it within an

if statement doesn''t help, it doens''t even go within the if

bloc. This is getting spooky guys, any ideas, I really need to get this working

Keep coding..
Run your app in a window so you can see the debug messages. The Debug Runtimes write messages to Visual Studio''s debugger, so you either need to run your app in a window, or on a seperate monitor so you can step through the code.

It''s a good thing to initialize your the center and radius to NULL, but you have to make sure that you''ve allocated space for them somewhere. This kind of thing would normally happen in your constructor I don''t see the code there. If you''re using pointers as your variables instead of just taking the address of a variable, you should have something like this before you call D3DXComputeBoundingSphere:

pCenter = new D3DXVECTOR3();
pRadius = new FLOAT;

Then you should deallocate this memory before the object is destroyed:

delete pCenter;
delete pRadius;

Allocation should happen in your constructor, and deletion should happen in your destructor.

But for what you''re doing, it would probably be better to just have a D3DXVECTOR3 variable for the center and a FLOAT for the radius instead of having to allocate space dynamically.

neneboricua
This is spooky, I just made a few changes such that instead of coputing my bounding sphere of the mesh from a function, I just hard coded it into the constructor and now its computing the bounding sphere?? This doesn''t seem to make any sense as I just copied the code from the function into the constructor and bid the function bye bye and now the code is running ok.

I still have to set the collision detection thing as thats not working for now. But This baffles me as to how could a few lines of code cease to work in a function when they work pretty ok in the constructor???

My detect collision is still a mess tthough, just a question where do I implement the detect collision function. In the game render loop or the mesh or do I have to create an arbitrary class that handles all this? Any suggestion would be highly appreciated as this is one of my first attempts to programming in DX
Keep coding..
I got the applictaion to compute the boudning sphere of my meshes and it seems to be doing that ok. The problem I am facing

now is detecting the collision. I am kinda confused as to where to implement the detect collision function entioned early on.

I am creating two meshes dynamically using arbitrary classes.

I have like a mesh class that
- creates the mesh and computes bounding sphere

I have 2 meshBuilder classes that actually create an individual instance of the mesh class respectively and cause movements

to their respective created meshes

The first meshbuilder just creates a mesh class object and leave it stationery.

The other meshbuilder class creates another instacne of the mesh class and moves the created mesh towards the first mesh.


Note the meshbuilders contain reference to an instance of the mesh class

I have 2 problems though

when I calculate the bounding spheres they get the vCenter and fRadisu values set fine. When I try to access them to compare the intersection they appear as zero which results in my program shutting down when the detect function is called. I think I''m mixing up with pointers of so.


The other question is where do I implement the detect collision function
- at the moment I''m thinking of calling the detectcollision function fro my main program render function. It seems crude but

at the moment I have a bit of stuff goin on in my render function.
What would be your suggestions guys I really appreciate all the help.
Keep coding..
quote:Original post by neneboricua19
D3DXIntersect isn''t used for collision detection between meshes. It''s used for ray casting and ray tracing.


Not true. Ray/Triangle-intersections are used quite a lot for testing tri/tri intersections. Each edge of a polygon is used as a ray, to see if they intersect the triangle.
Like I said I got my bounding spheres computed but now I

seem to be having trouble getting to compute the collision

detection. As mentioned I''m retrieveing the bounding sphere

center and radius via an intermediate class which mreates my

mesh, but somehwere along the way I lose my values?? I mean

they get calculated fine but on retrieval they reach the

calling class as zeros?? I think I must have messed up my

pointers or so here is the code for retruning the values of

the sphere center and float of my actual mesh class:

Mesh::	ReturnBoundingSphere(D3DXVECTOR3* m_pCenter, float* m_pRadius){	m_pCenter = &pCenter	m_pRadius = &pRadius};	//No problems here


here is the code of my intermediate class that gets the

values from the mesh and passes to the calling function

MeshBuilder::GetMeshBoundingSphere(D3DXVECTOR3* pCenter, float* pRadius){	Mesh1->ReturnBoundingSphere(pCenter, pRadius);};//Here the values returned are ZERO !!!


And here is my calling function i.e the detect collision

function, it calls to retrieve the values, and checks for

intersection. The problem is that the values returned are

zero so thats why I gues the program crashes.

DetectCollision(){	MeshBuilder1->GetMeshBoundingSphere(pCenter1, pRadius1);	MeshBuilder2->GetMeshBoundingSphere(pCenter2, pRadius2);		float fdistance = D3DXVec3Length(pBotCenter);		D3DXVECTOR3 vTemp;	HRESULT hr;	D3DXVec3Subtract(&vTemp, pCenter2, pCenter1);	{//		WriteLog("D3DXVec3Subtract()",hr);		//return false;	}	float distance = D3DXVec3Length(&vTemp);	if(distance<= *pRadius1+*pRadius2)		return true;	else 		return false;};


What could be the problem cos I know that at the Mesh calss

the radii and centers are being computed just fine. Upon

retrieval something is going wrong

ANy help would be greatly appreciated guys
Keep coding..
as you asked how 2 use D3DXIntersect

to use it you need a mesh loaded in d3d. And a ray. a ray has two properties. 1) the point from where a ray is fired called origion of ray and 2) direction in which the ray is fired.

if the ray fired collided with a mesh D3DXIntersect will set BOOL *pHit to true.
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
quote:Original post by ParityCheck

Mesh::ReturnBoundingSphere(D3DXVECTOR3* m_pCenter, float*
m_pRadius)
{
m_pCenter = &pCenter
m_pRadius = &pRadius

}; //No problems here
<br><BLOCKQUOTE><SPAN CLASS=smallfont>quote:<hr HEIGHT=1 noshade><br>This is just bad. There is almost never a need to return a pointer to member variables of a class. This is just bad programming and could lead to some SERIOUS problems if your code is going to be used by others. If you''re having problems with the pointers getting lost, then use references. They''re a little easier to keep straight in your head.<br><br>Also, from your variables, I can''t tell if pCenter is a pointer to a D3DXVECTOR3 variable or not. If it is indeed a pointer, then why are you taking it''s address (&pCenter) ? This gives you the address of a pointer, which in most cases is probably not what you intended.<br><br>All this pointer stuff is unnecessary and error prone. Please, do your code like this:<br> </pre> <!--STARTSCRIPT--><pre class="source"><br><font color=blue>class</font> Mesh<br>{<br><font color=blue>public</font>:<br> Mesh();<br> <font color=blue>virtual</font> ~Mesh();<br> FLOAT GetBoundingSphereRadius() const<br> {<br> <font color=blue>return</font> m_fRadius;<br> }<br><br> const D3DXVECTOR3& GetBoundingSphereCenter() const<br> {<br> <font color=blue>return</font> m_vCenter;<br> }<br><br> <font color=gray>// other stuff....<br></font><br><br><font color=blue>protected</font>:<br> D3DXVECTOR3 m_vCenter;<br> FLOAT m_fRadius;<br> <font color=gray>// more stuff...<br></font><br>};<br><br><font color=gray>//////////////////<br></font><br><br>DetectCollision()<br>{<br> const D3DXVECTOR3 &vCenter1 = MeshBuilder1-&gt;GetBoundingSphereCenter();<br> FLOAT fRadius1 = MeshBuilder1-&gt;GetBoundingSphereRadius();<br><br> const D3DXVECTOR3 &vCenter2 = MeshBuilder2-&gt;GetBoundingSphereCenter();<br> FLOAT fRadius2 = MeshBuilder2-&gt;GetBoundingSphereRadius();<br><br> D3DXVECTOR3 vTemp;<br> D3DXVec3Subtract(&vTemp, &vCenter2, &vCenter2);<br> FLOAT distance = D3DXVec3Length(&vTemp);<br> <font color=blue>if</font>(distance &lt;= fRadius1+fRadius2)<br> <font color=blue>return</font> <font color=blue>true</font>;<br> <font color=blue>else</font> <br> <font color=blue>return</font> <font color=blue>false</font>;<br>};<br></pre><!--ENDSCRIPT--><br><br>If you have more questions &#111;n this, please start a new thread with a good subject. I doubt many people &#111;n the board have kept up with this thread since it''s gone sooooo far off topic. That way, you''ll get other people''s responses as well.<br><br>neneboricua

This topic is closed to new replies.

Advertisement