moving objects with the mouse ray cast


photo

Recommended Posts

 

Hello! I have such a question as to use the mouse to manipulate objects, as well as bones so that you can control, for example, a manipulator.
Link to post
I understand it using World :: getIntersection. But you need to somehow convert the coordinates of the mouse. Any example?
Link to post

Hello @zomaru!

To retrieve an object under mouse cursor, one needs to cast a ray from the camera and find the first intersected object. You may find these articles helpful on this question:

In brief, use Player::getDirectionFromScreen() and World::getIntersection() for that like in the example:

Spoiler


NodePtr AppWorldLogic::getNodeUnderCursor()
{
	ivec2 mouse = Input::getMouseCoord();
	Vec3 p0 = player->getWorldPosition();
	Vec3 p1 = p0 + Vec3(player->getDirectionFromScreen(mouse.x, mouse.y)) * 100;
	WorldIntersectionPtr intersection = WorldIntersection::create();
	NodePtr nd = World::getIntersection(p0, p1, ~0, intersection);
	return nd;
}

 

So, you'll need a custom logic for moving objects (e.g. screen-space offset by using mouse delta).

For your convenience, there are ready-to-use widgets for node manipulation, they are:

Here's an example on how to use such a widget:

Spoiler


NodePtr selected_node;
PlayerPtr player;
GuiPtr gui;
WidgetManipulatorPtr wm;

int AppWorldLogic::init()
{
  	selected_node = World::getNodeByName("my_node");
	player = Game::getPlayer();
	gui = Gui::get();
  
	wm = WidgetManipulatorTranslator::create(gui);
	wm->setTransform(selected_node->getWorldTransform());
	wm->setProjection(player->getProjection());
	wm->addCallback(Gui::CHANGED, MakeCallback(this, &AppWorldLogic::move));
	gui->addChild(wm);
  
  	return 1;
}

int AppWorldLogic::update()
{
	wm->setModelview(player->getCamera()->getModelview());
	return 1;
}

void AppWorldLogic::move()
{
	selected_node->setWorldTransform(wm->getTransform());
}

 

widget_manipulator.gif

Please find the attached files for complete sample sources.

AppWorldLogic.h AppWorldLogic.cpp

  • Like 3
Link to post

Большое спасибо! У вас очень хорошая тех поддержка и продукт.

  • Thanks 1
Link to post