views:

177

answers:

2

Hey,

I am currently designing an RTS Game in XNA. So far I have a Terrain Model generated from a Heigthmap and some Units that are drawn on it.

The next step would be to give the units commands on where to move. To do that I have to calculate the exact position of the terrain behind the cursor when it's clicked.

For Units selection I simply generate a Ray pointing from the cursor away into the screen and select the first Unit intersected by that ray. Now that won't work for the terrain, because the whole thing is one model.

Can anyone help me?

Regards Nick

P.S: i just found this video on youtube which presents exactly the solution to my problem... unfortunately without any reference to the code. http://www.youtube.com/watch?v=czHW3oK8_oI

+1  A: 

I'm not familiar with XNA, but it sounds like your terrain model is a Mesh, and that you want to calculate the intersection of the ray with the mesh.

If so, there is a description of Ray-Mesh Intersection on the bottom half of this page. It decribes how to get the point of intersection and other data.

In general, googling XNA ray mesh intersection returns lots of hits. If that doesn't help you, then I'm obviously missing something.

brainjam
+1  A: 

Perhaps this sample from the Creators Club would be a good starting point:

http://creators.xna.com/en-US/sample/pickingtriangle

Picking Triangle Image

Andrew Russell