Jump to content
  • Advertisement
Sign in to follow this  
Adams555

Place object on top of mesh

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm kind of new to DirectX. If I had a mesh that was a landscape, would it be possible to figure out where to place an object (say, a character) so that it is always on top of the mesh? (Assuming the mesh has hills, etc.) I cant seem to find any information on how to do this. Any help would be appreciated, thanks.

Share this post


Link to post
Share on other sites
Advertisement
Well, yes, but it's probably be better to use a heightmap (grid of vertices) as a landscape. That way you can access the heightmap to find the y-value by interpolating between the vertices that make up the quad the object is on.

For meshes, it might be a little more difficult because you don't have direct access to the vertex positions. But I'm sure it can be done, using vertex buffers and such.

But for landscapes, I highly recommend heightmaps. Very useful things [cool]

Share this post


Link to post
Share on other sites
Try D3DXIntersect(). Though it is a heavy waste if your terrain mesh is large (consider cutting it up and using quadtrees).

Share this post


Link to post
Share on other sites
Once you've placed the mesh, how do you KEEP it on the terrain? How do you avoid walking into things? How do you make sure the camera stays about 1.6 meters above the ground?

The answer is that you need a "collision detection system" (sometimes known as an interference detector). If you google for this, you'll find lots of hits -- I like the one that comes with the ODE physics library; you can use only the collision part if you want.

Once you have a collision detection system, finding the height along a specific ray is easy: just cast a ray into your collision system and see what you hit. If your ground has Y up, and the coordinates you're interested in are (X,Z) then cast a ray from (X,10000,Z) downwards for 20000 units, and you'll intersect the ground at the ground height -- if there is any ground at (X,Z) of course :-)

Share this post


Link to post
Share on other sites
An important note following Mushu's suggestion is that you need to keep track of your ground information in system memory. If you know the height of every vertex in your terrain (as they are stored in system memory), then you can interpolate between any three heights and place your object there. Also you would probably want to store some information about that object in system memory, such as the bottom of it relative to its origin. Then you'll know how far off the ground to place the object. If you have a somewhat tesselated terrain, you can probably save the interpolations and just place the object on the nearest vertex point (at its height) or in your map editor force a kind of "Snap-to" to occur when placing objects on the terrain. Also to optimize, you can export the objects from your mesh editor with their bottoms at the model origin, so you don't have to perform an extra translation to place it on the terrain (though you'd not be saving much by doing that).

Chris

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!