• 12
• 14
• 13
• 10
• 11

# Raycasting in a simple 2d game

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

## Recommended Posts

Would ray casting be ok for a simple 2d game to target specific objects/monsters? I have heard nothing but suggestions to look into Pick, but it seems a bit overwhelming to figure out whats happening (and I cant find a solid example with more comments to explain) and thus before I dive into that, I wanted to look into ray casting. The reason I ask is I heard its slow or outdated, but my game is not graphics heavy (like I said, its 2d without many effects, no shaders, nothing fancy). My plan is to use D3DXIntersectTri() and model my sprites with 2 triangles, but I'm not sure if that'd be the best way. The example I'm reading about uses meshes, but like I said, its only a 2d game, so I'm trying to model by 2d sprites. D3DXVec3Unproject() is another function ive looked into (namely for the pick example) but I have not been able to get anything close to that work. Thanks in advance

##### Share on other sites
Picking is certainly more simple than raycasting. At the least, you can use the most basic form of picking: considering your game objects have a bounding box, check to see that a specific point (x,y) lies within the boundaries of the bounding box of any game object.

##### Share on other sites
Well, I guess I should have been more detailed. My game uses 2d textures but it is in 3d world, i.e., the camera is at a 45 degree angle and the sprites are rotated at a 45 degree angle. The terrain is rendered flat, thus it gives the appearance that the world has depth (trees block the player when the tree is in front of the player).

So that means that the very top tile I draw is much smaller than the closest tile. It also means that the sprites are of varying size depending on how far they are.

Thank you for your response =)

##### Share on other sites
Hi,

Use what works for you. I would suggest to model your sprites as 2 triangles and compute your picking ray.

You can look at the pick sample in your DirectX SDK at:
(SDK root)\Samples\C++\Direct3D\Pick

An example of picking in DX can also be found at
http://www.mvps.org/directx/articles/rayproj.htm

Ghostly yours,

Red.

##### Share on other sites
Alright, well someone suggested this in an old thread I had but it was retired, I shall post the code here:

D3DXMATRIX matProj1, matView1, matWorld1;	D3DVIEWPORT9 vp;	renderer.d3ddev->GetViewport(&vp);	renderer.d3ddev->GetTransform(D3DTS_PROJECTION, &matProj1);	renderer.d3ddev->GetTransform(D3DTS_VIEW, &matView1);	renderer.d3ddev->GetTransform(D3DTS_WORLD, &matWorld1);		D3DXVECTOR3 VectorIN(mouseCoords.x, mouseCoords.y, NEAR_VIEW_PLANE);	D3DXVECTOR3 VectorNear,VectorFar;	D3DXVec3Unproject(&VectorNear,&VectorIN, &vp, &matProj1, &matView1, &matWorld1);	VectorIN.z = FAR_VIEW_PLANE;	D3DXVec3Unproject(&VectorFar,&VectorIN, &vp, &matProj1, &matView1, &matWorld1);	D3DXVECTOR3 VectorDirection = VectorFar - VectorNear;	float t = (-(VectorFar.z) / VectorDirection.z);	D3DXVECTOR3 WorldCoord;	WorldCoord.x = VectorFar.x + t * VectorDirection.x;	WorldCoord.y = VectorFar.y + t * VectorDirection.y;	WorldCoord.z = 0;

Now after doing this, I display world coords, and its not what I expect at all, though I'm not sure if what I expect simply is wrong in itself. For instance, when I move position in the game, the output is the same, and I think it should change if it was finding the world coord.

Any help would be appreciated