0

I want to create a dynamic sphere in OSG which would be created(moved) by the mouse left click at that location (center) and with a dynamic radius of the mouse pointer current location's distance to the center....

I understand that for this reason I need to create an osgGA::GUIEventHandler Object and implement the virtual handle function but I miss other details like finding the sphere object it self from the scene and changing its attributes.

I also tried to change A picking example but failed to detect the sphere in the nodePath or creating a new sphere

datenwolf
  • 159,371
  • 13
  • 185
  • 298
Alireza Soori
  • 645
  • 9
  • 25

1 Answers1

1

I would probably start by creating a custom class derived from one of the osgGA Manipulator classes.

You'll want to override the handle() method with something like this:

bool CustomManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    using namespace osgGA;

    if (ea.getEventType()==GUIEventAdapter::FRAME)
    {
        if (_thrown) aa.requestRedraw();
    }
    else if (ea.getEventType()==GUIEventAdapter::PUSH)
    {
        // check if your sphere is picked using LineSegmentIntersector
        // (like in the picking example) and set a flag
    }
    else if (ea.getEventType()==GUIEventAdapter::DRAG)
    {
        // update the position and radius of your sphere if the flag was set
    }
    else if (ea.getEventType()==GUIEventAdapter::RELEASE)
    {
        // release the sphere, unset the flag
    }   
    return false;
}

Then remember to use setCameraManipulator() on your viewer to add this instead of the default TrackballManipulator.

mikewoz
  • 146
  • 2
  • 9
  • 1
    I just re-read the question and realized that you want to "create" the sphere upon mouse push. In that case, forget about the LineSegmentIntersector part and just reveal your sphere at that moment. I would create it in advance and use a nodemask to hide it until then. You may also want to use ea.getModKeyMask() to only perform your action if some keyboard button (eg, SHIFT) is held down. – mikewoz Nov 01 '12 at 19:27
  • so its not possible to create the sphere upon click or add it to the scene's node path or remove one from node path? or change it's radius? – Alireza Soori Nov 02 '12 at 20:39