Sign in to follow this  

Mouse to texture in DX9 (C++)

This topic is 3631 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

I am trying to allow the player to click on certain textures to cause certain actions. However, I am having trouble on figuring out which texture is being clicked upon. I can get mouse coords fine, however I cannot easily tell which texture is at a given mouse coord. If it helps, I am trying to work in a targeting system where a player can target an object. All help is appreciated =)

Share this post


Link to post
Share on other sites
Please give a better explanation of your problem. AFAIK, each triangle can have one texture only(or lots of textures blended together). Then, check what triangle is below the cursor and check what texture(s) pertain to this one. Take a look at this;

Share this post


Link to post
Share on other sites
Ok, sorry for the bad explanation. After skimming over the link, it could be a bit of what I'm trying to do, but I'll rephrase my problem first.

Basically, I have a grid of tiles flat, and then I have some characters projected at an angle to give them a little bit of realism to the 2d textures.

I want to be able to detect clicks on the characters so I can process the information and the player can interact with the characters.



[ ][ ][ ][ ][ ][ ][ ]
[ ][X][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]


Let's say you click on the X character (lets just model it as a square for now), the mouse coordinates will be like, let's say, 100,100 - but the location on my screen will be something different, all relative to where I am in the map, but thats fine, I can adjust relative to the center of my display.

However, my camera is at angle, so finding where they are at is much different, at the bottom of my screen I might get only 15 squares in, but at the top, I can see 30 squares wide.

Now I figured I could just find each little spot relative to my perspective, but that will take a very long time and I'd prefer not to do that. Any help as to how to solve this problem?

Share this post


Link to post
Share on other sites
As MJP said, what you are looking for is picking.

Here is what I wrote a while ago. This gives you world coordinates (WorldCoord) if you have device coordinates (Cursor). It's a little late so I don't really have time to draw any figures but if you have a Vector Algebra background, you will probably understand. If you have any questions, feel free to ask.


Function DeviceToWorldCoordinates:

VectorIN(Cursor.x,Cursor.y, Camera.NearPlane);

VectorNear,VectorFar;

D3DXVec3Unproject(&VectorNear,&VectorIN,...);

VectorIN = Camera.FarPlane;

D3DXVec3Unproject(&VectorFar,&VectorIN,...);

VectorDirection = VectorFar - VectorNear;

t = (-(VectorFar .z) / VectorDirection .z);

WorldCoord.x = VectorFar.x + t * VectorDirection.x;
WorldCoord.y = VectorFar.y + t * VectorDirection.y;
WorldCoord.z = 0;


PS: I for some reason assumed that you are using DX but I believe there is a similar (to D3DXVec3Unproject) function in OpenGL.

Share this post


Link to post
Share on other sites

This topic is 3631 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this