Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a picking example #264

Open
WindowsNT opened this issue Jun 6, 2021 · 0 comments
Open

Add a picking example #264

WindowsNT opened this issue Jun 6, 2021 · 0 comments

Comments

@WindowsNT
Copy link

WindowsNT commented Jun 6, 2021

It would help to have a picking method.

I'm trying to make my own, it doesn't work fully, what if the object is translated.


std::shared_ptr<item> Pick(float mouseX,float mouseY)
{
     
    // Right Hand conversion
    float pointX = (((2.0f * (float)mouseX) / (float)wi) - 1.0f) * -1.0f;
    float pointY = (((2.0f * (float)mouseY) / (float)he) - 1.0f);

    Vector3 origin = Vector3(pointX, pointY, 0);
    Vector3 farx = Vector3(pointX, pointY, 1);

    for (auto& m : models)
    {
        DirectXTK::BoundingSphere b = m->get_bounding_sphere();  // uses DirectXTK
        SimpleMath::Matrix worldMatrix = m->world;  // how to use this?

        auto m1 = m_view * m_proj;
        Matrix m2;
        m1.Invert(m2);

        // Now transform the ray origin and the ray direction from view space to world space.
        auto rayOrigin = XMVector3TransformCoord(origin, m2);
        auto rayend = XMVector3TransformCoord(farx, m2);

        // Normalize the ray direction.
        auto rayDirection = XMVector3Normalize(rayend - rayOrigin);

        float radius = 0;
        bool intersect = b.Intersects(rayOrigin, rayDirection, radius);

        if (intersect == true)
            return m;

        }
    return nullptr;
}
@walbourn walbourn self-assigned this Aug 18, 2021
@walbourn walbourn removed their assignment Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants