I developped a small games like "minecraft". and I'm stuck for a few days on the calculation of the direction of the cursor. Let me explain. I would like to target a cube with my cursor in the middle of the screen and make it disappear with a click. But I do not see any how to recover this information .. Can someone point me to a "sample / tutorial" or explanation please? Thank you very much.
My screenShoot picture game :
I take no credit for most of this--it's what I used in my 3D XNA game and it worked. The comments are fairly explanative. Basically it projects a
Ray
straight from your cursor position and checks to see if itIntersects()
the boundingsphere of any map objects (Unit
s in this case)Of course it widely depends on how you have your data organized. But if you have your polygon and meshes somewhere stored(and i'm not talking about vertex buffers!!), like in an octree, you can do something like this:
You create a ray from your camera settings and "fire" it through your scene. Usually a octree, or similar structures, can be extended to easily make a ray test to return the hit elements. Of course you could bruteforce iterate all your quads, but this will be very soon a performance bottleneck. From there you can use the result for what ever you want.
This whole process is usually called "picking" and this is a really nice tutorial about it.