Pick

This function performs a pick operation on a single surface. The start and end pick positions should be in local space. If a radius is specified, it will also be in considered local space.

Syntax

Parameters

Returns

Returns true if anything is hit, otherwise false is returned.

Example

#include "Leadwerks.h"

using namespace Leadwerks;

int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
camera->Move(0, 0, -8);
Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);
camera->SetProjectionMode(2);
camera->SetZoom(50);

//Create a model
Model* model = Model::Sphere();
model->SetScale(3.6);

//Create a sphere to indicate where the pick hits
Model* picksphere = Model::Sphere();
picksphere->SetColor(1.0, 0.0, 0.0);
picksphere->SetScale(0.5);

//Get the model surface
Surface* surface = model->GetSurface(0);

//Start and end pick points
Vec3 p0 = Vec3(-10, 1, 0);
Vec3 p1 = Vec3(10, 1, 0);

//Typically you would transform the pick points into local space
p0 = Transform::Point(p0, NULL, model);
p1 = Transform::Point(p1, NULL, model);

PickInfo pickinfo;
if (surface->Pick(p0, p1, pickinfo, 0, true))
{
//Transform back to world coordinates
pickinfo.position = Transform::Point(pickinfo.position, model, NULL);
picksphere->SetPosition(pickinfo.position);
}
else
{
picksphere->Release();
}
while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;

Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync();
}
return 0;
}