I am implementing a game with an overhead perspective, and I would like the player's character to orient towards the mouse cursor. To accomplish this, I create a plane3d at the height of the character, get the ray from the camera to the mouse, and call getIntersectionWithLine to get the point I wish to orient towards.
However, when I run the code, for some reason the intersection it gives me is off by a few hundred units (about a half a screen). Something that doesn't make any sense is how I have to set the normal of my plane3d to -1 Y. I tried printing out the player's position and the intersection point with both +1 and -1, and only -1 yields the same Y value. When I use +1, not only is the Y wrong, but the X and Z values are even farther off.
I have struggled with this for some time now, and any help would be greatly appreciated.
Code: Select all
position2di aim; // mouse xy
scInput::Instance()->GetMouseXY(aim.X, aim.Y);
ISceneCollisionManager* scm = smgr->getSceneCollisionManager();
line3df ray = scm->getRayFromScreenCoordinates(aim);
plane3df horizontal; // plane horizontal to the character
horizontal.setPlane(vector3df(0.f, -1.f, 0.f), playerNode->getAbsolutePosition().Y);
vector3df pIntersect; // the point we want to orient towards
if(horizontal.getIntersectionWithLine(ray.start, ray.end, pIntersect))
{
pIntersect = pIntersect - playerNode->getPosition();
pIntersect.normalize();
f32 theta = 57.2958 * acos(pIntersect.dotProduct(vector3df(0.f, 0.f, 1.f)));
playerNode->setRotation(vector3df(0.f,theta,0.f));
}