Jump to content

World::Pick() with radius greater than zero


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

I don't know if this is a bug or not actually.  You can see here that the green sphere bounces around a little when it rides over the blue cube and it starts to do the same toward the end of the red.  And should the picked position be offset from the cube like that?  I'm guessing it should be and the offset is the radius value?

Can we get an Ultra Engine Bug Report forum too?  I could only create this topic in Leadwerks Bug Reports.

Picking.gif.71838e21a2f6b407eef9bd379fe52f23.gif

#include "UltraEngine.h"

using namespace UltraEngine;

bool PickFilter(std::shared_ptr<Entity> entity, std::shared_ptr<Object> extra) {
    if (entity->GetCollider() == nullptr) { return false; }
    return true;
}

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 2, -3);
    camera->SetRotation(25, 0, 0);
    camera->AddPostEffect(LoadPostEffect("Shaders\\PostEffects\\FXAA.json"));

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 45, 0);
    light->SetColor(5);

    auto floor = CreatePlane(world, 100, 100);
    floor->Move(0, -1, 0);

    auto b1 = CreateBox(world, 2.0f);
    b1->SetPosition(-3.0f, 0.0f, 0.0f);
    b1->SetColor(1, 0, 0);

    auto b2 = CreateBox(world, 2.0f);
    b2->SetColor(0.0f, 0.0f, 1.0f);
    b2->SetPosition(3.0f, 0.0f, 2.0f);
    b2->SetRotation(0.0f, 45.0f, 0.0f);

    auto pivot = CreatePivot(world);
    
    auto rod_scale = 5.0f;
    auto rod = CreateCylinder(world, 0.05f);
    rod->SetCollider(nullptr);
    rod->SetParent(pivot);
    rod->SetRotation(90.0f, 0.0f, 0.0f);
    rod->SetPosition(0.0f, 0.0f, rod_scale / 2.0f);
    rod->SetScale(1.0f, rod_scale, 1.0f);

    auto sphere = CreateSphere(world, 0.25f);
    sphere->SetCollider(nullptr);
    sphere->SetParent(pivot);
    sphere->SetColor(0, 1, 0);
    sphere->SetPosition(0.0f, 0.0f, rod_scale);

    auto spin_speed = 0.5f;
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        pivot->Turn(0.0f, spin_speed, 0.0f);

        auto target_pos = Vec3(0.0f, 0.0f, rod_scale);
        target_pos = TransformPoint(target_pos, Mat4(), pivot->GetMatrix(true).Inverse());

        auto pick_info = world->Pick(pivot->GetPosition(true), target_pos, 0.5f, false, PickFilter, nullptr);
        if (pick_info.success) {
            sphere->SetPosition(pick_info.position, true);
        }
        else {
            sphere->SetPosition(target_pos, true);
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}
Link to comment
Share on other sites

  • Solution

Close that drop down and the Continue button will be there, I think. You can switch to the category forum view mode on the main forum index page if you wish.

There are two things going on here. One is that the sphere is using a different radius than the pick, so it looks wrong, but the radius is correct. The other is that the pick does seem to have some trouble on the edges of polygons, but you have the "closest" parameter set to false, so it isn't always picking the closest polygon, just the first one it encounters.

Try this:

#include "UltraEngine.h"

using namespace UltraEngine;

bool PickFilter(std::shared_ptr<Entity> entity, std::shared_ptr<Object> extra) {
    if (entity->GetCollider() == nullptr) { return false; }
    return true;
}

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 2, -3);
    camera->SetRotation(25, 0, 0);
    camera->AddPostEffect(LoadPostEffect("Shaders\\PostEffects\\FXAA.json"));

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 45, 0);
    light->SetColor(5);

    auto floor = CreatePlane(world, 100, 100);
    floor->Move(0, -1, 0);

    auto b1 = CreateBox(world, 2.0f);
    b1->SetPosition(-3.0f, 0.0f, 0.0f);
    b1->SetColor(1, 0, 0);

    auto b2 = CreateBox(world, 2.0f);
    b2->SetColor(0.0f, 0.0f, 1.0f);
    b2->SetPosition(3.0f, 0.0f, 2.0f);
    b2->SetRotation(0.0f, 45.0f, 0.0f);

    auto pivot = CreatePivot(world);

    auto rod_scale = 5.0f;
    auto rod = CreateCylinder(world, 0.05f);
    rod->SetCollider(nullptr);
    rod->SetParent(pivot);
    rod->SetRotation(90.0f, 0.0f, 0.0f);
    rod->SetPosition(0.0f, 0.0f, rod_scale / 2.0f);
    rod->SetScale(1.0f, rod_scale, 1.0f);

    auto sphere = CreateSphere(world, 0.25f);
    sphere->SetCollider(nullptr);
    sphere->SetParent(pivot);
    sphere->SetColor(0, 1, 0);
    sphere->SetPosition(0.0f, 0.0f, rod_scale);

    camera->SetWireframe(true);

    auto spin_speed = 0.5f;
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        pivot->Turn(0.0f, spin_speed, 0.0f);

        auto target_pos = Vec3(0.0f, 0.0f, rod_scale);
        target_pos = TransformPoint(target_pos, Mat4(), pivot->GetMatrix(true).Inverse());

        auto pick_info = world->Pick(pivot->GetPosition(true), target_pos, 0.25f, true, PickFilter, nullptr);
        if (pick_info.success) {
            sphere->SetPosition(pick_info.position, true);
        }
        else {
            sphere->SetPosition(target_pos, true);
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

57 minutes ago, Josh said:

Close that drop down and the Continue button will be there, I think. You can switch to the category forum view mode on the main forum index page if you wish.

Ah yes, it is.  Thanks.

Example works too.  Didn't think about the "closest" parameter.

13 minutes ago, Josh said:

I like your example so much I added it to the docs:
https://www.ultraengine.com/documentation?page=World_Pick

I am honoured. ☺️

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...