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
- bool Pick(const Vec3& p0, const Vec3& p1, Pick& pick, float radius = 0.0, bool closest=false)
Parameters
- p0: the origin of the ray.
- p1: the terminal end of the ray.
- pick: a pick object to contain pick information.
- radius: the radius of the ray. If the radius is greater than 0.0, a slower swept sphere collision will be performed.
- closest: if set to true, the closest intersection will be found, otherwise the operation will return as soon as a single intersection is found.
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;
}