Jump to content

Using World::Pick to get distance


EirikNesje
 Share

Go to solution Solved by Josh,

Recommended Posts

Hi

I would like to get the distance from an entity to a scene/map loaded. Intend to use the position of the entity and do a call to a vector on the same x and z values but lower to get the height above the map. Have tested with requesting the first entity but it is always in a distance of -1 (have set the start vector below the  mesh to avoid getting the entity I'm using as a start vector for the ray cast).

Are there any examples on using a list of entities hit by the ray cast or will not the ray cast register the scene as a hit (not an entity) ?

Have also used player physics and it worked well and GetAirborne did much of the same as I want to achieve but not using PHYSICS_PLAYER. (just as an entity with mass).

BR

Eirik

Link to comment
Share on other sites

Not sure exactly what your tying to do.  Are you trying to get the terrain elevation under your physics entity?

3 hours ago, EirikNesje said:

Are there any examples on using a list of entities hit by the ray cast or will not the ray cast register the scene as a hit (not an entity) ?

I don't know of any but you can loop through the entities and find the closest one by getting it's distance to the entity in question.

Link to comment
Share on other sites

  • Solution

The distance member is used internally by the engine and results in a value from 0 to 1 if the ray test hits anything. You should check if the pickinfo.success  member is true or false. It sounds like the ray is not currently intersecting anything.

The World::Pick method can be used to determine visibility between two points, if you don't care which object is hit and you just want to see if anything is in the way. If you set the closest parameter to true then the ray will pick the closest object that is hit.

The distance between the pickinfo.position Vec3 and the ray origin is your distance.

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

Hi

 

Did some more testing and now it is working as I wanted. Had to use :

 auto pick = world->Pick(sphere->GetPosition(), Vec3(sphere->GetPosition().x, lowest, sphere->GetPosition().z), 0.0f, true);

It was the radius parameter I got wrong. Set it to expected max value but it looks like it works with 0.0f. Instead of using the pick.entity.Position it worked perfect using the pick.position (which looks to be the location of the casted ray entering the entity found. 

The whole test program looks like this :

#include <string>
#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

#define SPHERE_SIZE 0.5f
#define SPHERE_MOVE_FORCE 10.0f
#define SPHERE_JUMP_FORCE 500.0f

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    //Create a world
    auto world = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a camera
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 7, 2);

    // Load map
    auto scene = LoadScene(world, "Maps/new.map");

    //Create sphere
    auto sphere = CreateSphere(world, SPHERE_SIZE);
    sphere->SetColor(1, 0, 0);
    sphere->SetPosition(2, 10, 2);
    sphere->SetMass(1);

    // Get z-position to put camera on and minimal y value of scene
    float closest = 999.0f;
    float lowest = -999.0f;
    for (auto current : scene->entities) {
        auto aabb = current->GetBounds();
        if (aabb.min.z < closest) closest = aabb.min.z;
        if (aabb.min.y < lowest) lowest = aabb.min.z;
    }
    
    float distanceToEntity = 0.0f;
    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        // check if sphere is in the air
        bool airborne = false;

        auto pick = world->Pick(sphere->GetPosition(), Vec3(sphere->GetPosition().x, lowest, sphere->GetPosition().z), 0.0f, true);
        if (pick.success) {
            distanceToEntity = sphere->GetPosition().y - pick.position.y;
        }
        if (distanceToEntity > (SPHERE_SIZE + 0.1f)) {
            airborne = true;
        }
        
        // Adding torque can be done while in the air causing a spin effect on contact :-)
        // Add torque to the Z-axis (moving along the x-axis)
        if (window->KeyDown(KEY_A)) {
            sphere->AddTorque(Vec3(0.0f, 0.0f, SPHERE_MOVE_FORCE));
        }
        if (window->KeyDown(KEY_D)) {
            sphere->AddTorque(Vec3(0.0f, 0.0f, -SPHERE_MOVE_FORCE));
        }
        // Add torque to the X-axis (moving along the z-axis)
        if (window->KeyDown(KEY_S)) {
            sphere->AddTorque(Vec3(-SPHERE_MOVE_FORCE, 0.0f, 0.0f));
        }
        if (window->KeyDown(KEY_W)) {
            sphere->AddTorque(Vec3(SPHERE_MOVE_FORCE, 0.0f, 0.0f));
        }

        if (!airborne) {
            if (window->KeyHit(KEY_SPACE)) {
                sphere->AddForce(Vec3(0.0f, SPHERE_JUMP_FORCE, 0.0f));
            }
        }

        world->Update();
        camera->SetPosition(sphere->GetPosition().x, 7.0f, closest);
        camera->Point(sphere);

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

Using a map (new.map) that looks like this:
image.thumb.png.9e9ebf2e2ad782ae2ce75adb5aec9ca5.png

Rolling up the ramp had some issues when using the entity's position. Using the PickInfo's position worked well.

BR

Eirik

 

  • Like 2
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...