Pick
This function performs a pick operation on all entities in a world that intersect the specified line.
Syntax
- bool Pick(const Vec3& p0, const Vec3& p1, PickInfo& pickinfo, float radius=0.0, bool closest=false, int collisiontype=0)
Parameters
- p0: the origin of the ray.
- p1: the terminal end of the ray.
- pickinfo: a pick info 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.
- collisiontype: if specified, the entity collision type will be tested against this, and only colliding entities will be tested.
Returns
Returns true if anything is hit, otherwise false is returned.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
SpotLight* light = NULL;
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);
//Create a model
Model* model1 = Model::Box();
model1->SetPosition(2, 0, 0);
Model* model2 = Model::Box();
model2->SetPosition(-2, 0, 0);
model2->SetPickMode(0);// Disable picking on the first hit entity
//Create a sphere to indicate where the pick hits
Model* picksphere = Model::Sphere();
picksphere->SetColor(1.0, 0.0, 0.0);
picksphere->SetPickMode(0);
float pickradius = 0.5;
picksphere->SetScale(pickradius*2.0);
world->Update();
PickInfo pickinfo;
if (world->Pick(-10, 0.75, 0, 10, 0.75, 0, pickinfo, pickradius, true))
{
picksphere->SetPosition(pickinfo.position);
//Make sure the pick mode equals Entity::PolygonPick
Debug::Assert(pickinfo.entity->GetPickMode() == Entity::PolygonPick);
}
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;
}