Pick
This function performs a pick from camera screen coordinates. The current buffer or context will be used for picking operations.
Syntax
- bool Pick(int x, int y, PickInfo& pick, float radius=0.0, bool closest=false, int collisiontype=0)
Parameters
- x: the x component of the screen coordinate to pick.
- y: the y component of the screen coordinate to pick.
- pickinfo: a PickInfo structure to hold information.
- radius: a radius can be specified to perform a swept sphere collision test.
- closest: if set to true the closest intersection will be found, otherwise the routine will return as soon as one object is picked.
- collisiontype: if specified, the entity collision type will be tested against this, and only colliding entities will be tested.
Returns
Returns true if anything was picked, 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);
float pickradius = 0.5;
//Create a model
float spherescale = 3.6;
Model* model = Model::Sphere();
model->SetPickMode(Entity::SpherePick);
model->SetPickRadius(spherescale / 2.0);
model->SetScale(model->GetPickRadius()*2.0);
//Create a sphere to indicate where the pick hits
Entity* picksphere = Model::Sphere();
picksphere->SetColor(1.0, 0.0, 0.0);
picksphere->SetPickMode(0);
picksphere->SetScale(pickradius*2.0);
picksphere->Hide();
while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) break;
if (window->MouseDown(1))
{
picksphere->Hide();
PickInfo pickinfo;
Vec3 p = window->GetMousePosition();
if (camera->Pick(p.x, p.y, pickinfo, pickradius, true))
{
picksphere->Show();
picksphere->SetPosition(pickinfo.position);
}
}
Leadwerks::Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync();
}
}