Пример #1
0
 virtual void keyPressed(const KeyEventUnrecPtr e)
 {
     if(e->getKey() == KeyEvent::KEY_Q && e->getModifiers() & KeyEvent::KEY_MODIFIER_COMMAND)
     {
         TutorialWindow->closeWindow();
     }
     switch(e->getKey())
     {
         case KeyEvent::KEY_S:
             {
                 buildSphere();
             }
             break;
         case KeyEvent::KEY_B:
             {
                 buildBox();
             }
             break;
         case KeyEvent::KEY_T:
             {
                 buildTriMesh();
             }
             break;
         case KeyEvent::KEY_D:
             {
                 if(PhysDrawableNode->getTravMask())
                 {
                     PhysDrawableNode->setTravMask(TypeTraits<UInt32>::getMin());
                 }
                 else
                 {
                     PhysDrawableNode->setTravMask(TypeTraits<UInt32>::getMax());
                 }
             }
             break;
     }
 }
Пример #2
0
// Initialize GLUT & OpenSG and set up the rootNode
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // Set up Window
    TutorialWindow = createNativeWindow();
    TutorialWindow->initWindow();

    TutorialWindow->setDisplayCallback(display);
    TutorialWindow->setReshapeCallback(reshape);

    TutorialKeyListener TheKeyListener;
    TutorialWindow->addKeyListener(&TheKeyListener);
    TutorialMouseListener TheTutorialMouseListener;
    TutorialMouseMotionListener TheTutorialMouseMotionListener;
    TutorialWindow->addMouseListener(&TheTutorialMouseListener);
    TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener);


    // Create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;


    // Tell the Manager what to manage
    mgr->setWindow(TutorialWindow);

    //Make Base Geometry Node
    TriGeometryBase = makeTorus(0.5, 1.0, 24, 24);

    //Make Main Scene Node
    NodeRefPtr scene = makeCoredNode<Group>();
    setName(scene, "scene");
    rootNode = Node::create();
    setName(rootNode, "rootNode");
    ComponentTransformRefPtr Trans;
    Trans = ComponentTransform::create();
    rootNode->setCore(Trans);

    // add the torus as a child
    rootNode->addChild(scene);

    //Make The Physics Characteristics Node
    PhysicsCharacteristicsDrawableRefPtr PhysDrawable = PhysicsCharacteristicsDrawable::create();
    PhysDrawable->setRoot(rootNode);

    PhysDrawableNode = Node::create();
    PhysDrawableNode->setCore(PhysDrawable);
    PhysDrawableNode->setTravMask(TypeTraits<UInt32>::getMin());

    rootNode->addChild(PhysDrawableNode);


    //Setup Physics Scene
    physicsWorld = PhysicsWorld::create();
    physicsWorld->setWorldContactSurfaceLayer(0.005);
    physicsWorld->setAutoDisableFlag(1);
    physicsWorld->setAutoDisableTime(0.75);
    physicsWorld->setWorldContactMaxCorrectingVel(100.0);
    physicsWorld->setGravity(Vec3f(0.0, 0.0, -9.81));

    physicsSpace = PhysicsHashSpace::create();

    physHandler = PhysicsHandler::create();
    physHandler->setWorld(physicsWorld);
    physHandler->pushToSpaces(physicsSpace);
    physHandler->setUpdateNode(rootNode);

    physHandler->attachUpdateProducer(TutorialWindow->editEventProducer());


    /************************************************************************/
    /* create spaces, geoms and bodys                                       */
    /************************************************************************/
    //create a group for our space
    GroupRefPtr spaceGroup;
    spaceGroupNode = makeCoredNode<Group>(&spaceGroup);
    //create the ground plane
    GeometryRefPtr plane;
    NodeRefPtr planeNode = makeBox(30.0, 30.0, 1.0, 1, 1, 1);
    plane = dynamic_cast<Geometry*>(planeNode->getCore());
    //and its Material
    SimpleMaterialRefPtr plane_mat = SimpleMaterial::create();
    plane_mat->setAmbient(Color3f(0.7,0.7,0.7));
    plane_mat->setDiffuse(Color3f(0.9,0.6,1.0));

    plane->setMaterial(plane_mat);



    //create Physical Attachments
    PhysicsBoxGeomRefPtr planeGeom = PhysicsBoxGeom::create();
    planeGeom->setLengths(Vec3f(30.0, 30.0, 1.0));
    //add geoms to space for collision
    planeGeom->setSpace(physicsSpace);


    //add Attachments to nodes...
    spaceGroupNode->addAttachment(physicsSpace);
    spaceGroupNode->addAttachment(physHandler);    
    spaceGroupNode->addAttachment(physicsWorld);
    spaceGroupNode->addChild(planeNode);


    planeNode->addAttachment(planeGeom);


    scene->addChild(spaceGroupNode);


    //Create Statistics Foreground
    SimpleStatisticsForegroundRefPtr PhysicsStatForeground = SimpleStatisticsForeground::create();
    PhysicsStatForeground->setSize(25);
    PhysicsStatForeground->setColor(Color4f(0,1,0,0.7));
    PhysicsStatForeground->addElement(PhysicsHandler::statPhysicsTime, 
                                      "Physics time: %.3f s");
    PhysicsStatForeground->addElement(PhysicsHandler::statCollisionTime, 
                                      "Collision time: %.3f s");
    PhysicsStatForeground->addElement(PhysicsHandler::statSimulationTime, 
                                      "Simulation time: %.3f s");
    PhysicsStatForeground->addElement(PhysicsHandler::statNCollisions, 
                                      "%d collisions");
    PhysicsStatForeground->addElement(PhysicsHandler::statNCollisionTests, 
                                      "%d collision tests");
    PhysicsStatForeground->addElement(PhysicsHandler::statNPhysicsSteps, 
                                      "%d simulation steps per frame");
    PhysicsStatForeground->setVerticalAlign(SimpleStatisticsForeground::Center);


    SimpleStatisticsForegroundRefPtr RenderStatForeground = SimpleStatisticsForeground::create();
    RenderStatForeground->setSize(25);
    RenderStatForeground->setColor(Color4f(0,1,0,0.7));
    RenderStatForeground->addElement(RenderAction::statDrawTime, "Draw FPS: %r.3f");
    RenderStatForeground->addElement(RenderAction::statNGeometries, 
                                     "%d Nodes drawn");





    // tell the manager what to manage
    mgr->setRoot  (rootNode);

    mgr->getWindow()->getPort(0)->addForeground(PhysicsStatForeground);
    mgr->getWindow()->getPort(0)->addForeground(RenderStatForeground);

    physHandler->setStatistics(PhysicsStatForeground->editCollector());
    mgr->getRenderAction()->setStatCollector(RenderStatForeground->editCollector());

    // show the whole rootNode
    mgr->showAll();

    Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
    Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
    TutorialWindow->openWindow(WinPos,
                               WinSize,
                               "01SimplePhysics");

    //Enter main Loop
    TutorialWindow->mainLoop();

    osgExit();

    return 0;
}