Mesh Always Facing Cursor

Started by
2 comments, last by __Daedalus__ 21 years ago
I have a mesh .X file "floor" as a level. It is just a plane primitive stretching 100' by 50'. I also have a player mesh that runs about on the floor using the WSAD keys. At the moment I am not rotating the player mesh. It's Y world coordinate does not change - just the X and Z ones. How do I get it so that this player mesh is always facing the mouse cursor? The player mesh essentially needs to rotate on the Y axis. So how would I designate part of the player mesh as being the "front" (attach a normal vector??) and then how would I continually rotate it to always look at the mouse. I am assuming the XY mouse coordinates would need to be transformed to 3D world coordinates. I have looked at the Pick SDK example but couldn't gleam anything practical out of it ... How do I figure out what I should rotate the mesh to? Thanks for any info .. R [edited by - RayWilson on April 1, 2003 8:36:47 AM]
Advertisement
are you having trouble with the picking part, or the overall concept?

overall, here''s the steps i''d think you''d need to do:

o "unproject" the mouse point into the 3D world to get a line
o find the intersection of that line with the floor mesh
o subtract the hero''s x/z coord (using the intersecting point''s y) from the intersecting point to get a vector from the hero to the pick point
o dot product that vector with the hero model''s initial directional vector (usually +x for my models but depends upon which direction the "face" is initially pointing) to get the cosine of the angle that the hero needs to be rotated to
o get the angle from the cosine and set the hero''s y-rotation angle to that
o re-build the hero''s world matrix by concatenating the y-rotation matrix (built from the y-rotation angle) with the hero''s translation matrix and any other matrices you may be using - use this as the world matrix for rendering purposes

of course this will "instantly" re-orient the hero to face the pick point. animating the rotation would depend on if you have an animation sequence for your mesh or not. either way you''d probably want to use a quaternion to smooth things out.
Here''s a recent thread covering something similar. That''ll get you started with finding the point on the ground the mouse is pointed at (in conjunction with the pick sdk sample).

- Matt


Thanks so much guys - this is informational gold! I think I have finally transformed the mouse coords in to world coords! That''s a great start!

This topic is closed to new replies.

Advertisement