Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


D3DXIntersect() not succeeding


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 george7378   Members   -  Reputation: 1291

Like
0Likes
Like

Posted 27 January 2013 - 07:04 PM

Hi everyone,

 

I'm trying to sort out some basic system which allows my camera to follow the height of a mesh (the mesh pointer itself is a public member of a class, of which meshTorus is an instance) a bit like terrain, using the D3DXIntersect() function. I'm defining a ray to start at the location of my camera (held in &campos) and point directly downwards (effectively allowing me to detect my vertical height above the chosen mesh). This is sent to a float called terrainHeight:

 

D3DXIntersect(meshTorus.meshPointer, &campos, &D3DXVECTOR3(0, -1, 0), NULL, NULL, NULL, NULL, &terrainHeight, NULL, NULL);

 

The only problem is that when I use it, the function doesn't succeed (when I run a check, it doesn't return D3D_OK). I can't think why this is - the mesh certainly exists because I render just before the function is called, and it's there.

 

I suspect that this is something pretty simple that I just don't know about! I should also say that I am running D3DXIntersect() in the render loop, just after rendering the mesh to be tested.

 

Thanks!

 



Sponsor:

#2 Mussi   Crossbones+   -  Reputation: 2103

Like
0Likes
Like

Posted 27 January 2013 - 07:26 PM

george7378, on 28 Jan 2013 - 02:03, said:
The only problem is that when I use it, the function doesn't succeed (when I run a check, it doesn't return D3D_OK).

Okay, what does it return then?

I don't think the 4th parameter is optional, but I'm not sure. Another note taken from the MDSN comment section: This function will return E_INVALIDCALL if mesh flags have D3DXMESH_IB_WRITEONLY or D3DXMESH_VB_WRITEONLY.

Also, you don't want determine the camera's position in or after the render stage. You'll want to do this before you render anything, otherwise you'll be rendering from incorrect camera positions.

#3 george7378   Members   -  Reputation: 1291

Like
0Likes
Like

Posted 28 January 2013 - 04:27 AM

I changed my code to this:

 

HRESULT hr = D3DXIntersect(meshTorus.meshPointer, &campos, &D3DXVECTOR3(0, -1, 0), NULL, NULL, NULL, NULL, &terrainHeight, NULL, NULL);
    _com_error err(hr); LPCTSTR errMsg = err.ErrorMessage();
    MessageBox(NULL, errMsg, "error", MB_OK);

 

...and when I run the program, I get this in my message box:

 

errintersect.jpg

 

...however, when I add a pointer for the 4th parameter, I get the message 'this function completed successfully' in my messagebox. I guess that's solved it!

 

Also, I always update my camera position before the actual BeginScene() is called. I guess this is OK?

 

Thanks for your help!



#4 belfegor   Crossbones+   -  Reputation: 2722

Like
0Likes
Like

Posted 28 January 2013 - 05:15 AM

When you use some DX api function, or from any other library that you might include into your project, first what should you do is to look for documentation about that function (if any) and see if some of its paramters can be NULL, if it doesn't say then it is more likely that it will fail if you pass NULL.

 

Also, I always update my camera position before the actual BeginScene() is called. I guess this is OK?

 

In between Begin and EndScene you must put code that is related to drawing, usually DrawXXX calls, setting shaders, render targets... Everything else you put where you think that is making sense.

 

_com_error err(hr); LPCTSTR errMsg = err.ErrorMessage();
    MessageBox(NULL, errMsg, "error", MB_OK);

 

I don't think that can give proper info about DX error, its better to put breakpoint just after D3DXIntersect function and copy value of hr and paste it in "DXErr.exe" utility from DX SDK.



#5 george7378   Members   -  Reputation: 1291

Like
0Likes
Like

Posted 28 January 2013 - 08:47 AM

OK, I'm picking up some useful stuff here, thanks. I am having another slight problem with my D3DXIntersect function though - I have applied a translation matrix to the mesh I'm interested in so that it appears lower down, however, the function still seems to think the mesh is in the same place as it was before I applied the translation (i.e. when I loaded it in). Is there a simple way to make D3DXIntersect take transformations in the world matrix into account?

 

Thanks!



#6 belfegor   Crossbones+   -  Reputation: 2722

Like
0Likes
Like

Posted 28 January 2013 - 08:58 AM

Try this: http://stackoverflow.com/questions/11274336/d3dxintersect-ray-in-wrong-coords



#7 george7378   Members   -  Reputation: 1291

Like
0Likes
Like

Posted 28 January 2013 - 11:29 AM

Ah, it looks like that's what I'm doing wrong. While I'm trying to sort out the workings of this function, I translated my floor up so that the mesh sits on it without having any transformations applied (my floor was at -4 before, so it is probably better to put it at 0 anyway), and I think I've got it working.

 

At the moment, this is my code:

 

if (D3DXIntersect(meshTorus.meshPointer, &campos, &D3DXVECTOR3(0, -1, 0), &meshHit, NULL, NULL, NULL, &terrainHeight, NULL, NULL)==D3D_OK);
{if (meshHit){campos.y += (0.5 - terrainHeight);} //0.5 is the height of my camera above the flat ground
else {campos.y = 0.5; terrainHeight = 0;}}

 

This works fine in that my camera follows the height of the object beautifully. However, I was wondering if anyone can think of a better way of elevating the camera than the one I have now? My current method for determining the height of the camera just seems a bit 'ugly' for some reason, but I can't think of a different way of doing it (given that I only know the distance to the ray's intersection with the mesh).

Also, my mesh has an underside too, so should I be worrying that there will be more than one intersection of the ray with the mesh?

 

Now I'm going to go and figure out the best way to transform the ray for the translated mesh...



#8 Mussi   Crossbones+   -  Reputation: 2103

Like
0Likes
Like

Posted 29 January 2013 - 07:38 AM

You could translate your world matrix down by 0.5, but I'm not sure if that's better.


Also, my mesh has an underside too, so should I be worrying that there will be more than one intersection of the ray with the mesh?

D3DXIntersect returns the shortest distance. Assuming that's what you're interested in, you don't have to worry about that.

Edited by Mussi, 29 January 2013 - 07:39 AM.


#9 george7378   Members   -  Reputation: 1291

Like
0Likes
Like

Posted 29 January 2013 - 05:37 PM

OK - I've been testing this method some more and it seems to work for more complicated island meshes too. It's also good because it only follows the height of the terrain below you (i.e. you can't suddenly mount this platform by walking under it - you have to walk up the ramp):

 

Direct3D first person camera 2013-01-29 23-30-20-68.png

 

Direct3D first person camera 2013-01-29 23-30-55-32.png






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS