Page 1 of 1

Problem with getIntersectionWithLine

Posted: Wed Mar 18, 2009 1:21 am
by Sleddog
I have a problem using the plane3d getIntersectionWithLine function I think. Here is what I am trying to do.

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));
    }

Posted: Wed Mar 18, 2009 5:14 am
by vitek
You're using getIntersectionWithLine() incorrectly. The first parameter is the line start, but the second parameter is the line direction (not the line end point). You should use getIntersectionWithLimitedLine() if you want to pass the start and end of the line.

Travis

Posted: Thu Mar 19, 2009 12:10 am
by Sleddog
That did it! I knew I didn't understand something about the function...

Anyways, I ran into another problem with the rotation only pointing in one direction, but I was able to fix it with the following code.

Code: Select all

f32 theta;
if(pIntersect.X >= 0.f)
    theta = 57.2958 * acos(pIntersect.dotProduct(vector3df(0.0f, 0.0f, 1.0f)));
else
    theta = -57.2958 * acos(pIntersect.dotProduct(vector3df(0.0f, 0.0f, 1.0f)));
Again, thanks so much!