int Java_zte_irrlib_scene_Scene_nativeGetTouchedSceneNode( JNIEnv *env, jobject defaultObj, jint x, jint y, jint root) { ISceneNode* rootNode = smgr->getSceneNodeFromId(root); ICameraSceneNode* camera = smgr->getActiveCamera(); if (!rootNode) { WARN_NODE_NOT_FOUND(root, GetTouchedSceneNode); return -1; } ISceneCollisionManager *collMgr = smgr->getSceneCollisionManager(); core::line3d<f32> ray = collMgr->getRayFromScreenCoordinates( irr::core::position2di(x, y), camera); core::vector3df intersection; core::triangle3df hitTriangle; scene::ISceneNode * selectedSceneNode = collMgr->getSceneNodeAndCollisionPointFromRay( ray, intersection, //intersection position hitTriangle, //hit triangle 0, //idBitMask: 0 to test all nodes rootNode //root node to search from ); if(selectedSceneNode) return selectedSceneNode->getID(); else return 0; }
/// @param x screen x-coordinate for active camera /// @param y screen y-coordinate for active camera /// @return Approximate 3d position of the click Vector3f SimContext::GetClickedPosition(const int32_t& x, const int32_t& y) { Pos2i pos(x,y); ISceneCollisionManager* collider = mIrr.getSceneManager()->getSceneCollisionManager(); // get ray Line3f ray = collider->getRayFromScreenCoordinates(pos); LOG_F_DEBUG("collision", "screen coordinates: " << pos << ", ray: " << ray.start << " - " << ray.end << ", length: " << ray.getLength()); Vector3f collision_point; Triangle3f collision_triangle; ISceneNode* collision = collider->getSceneNodeAndCollisionPointFromRay(ray, collision_point, collision_triangle); if (collision) { SimEntityPtr ent = mpSimulation->FindBySceneObjectId(collision->getID()); LOG_F_DEBUG("collision", "screen coordinates: " << pos << " colliding with: " << ent << " at: " << collision_point); return ConvertIrrlichtToNeroPosition(collision_point); } else { LOG_F_WARNING("collision", "screen coordinates: " << pos << " did not collide with any geometry!"); return ConvertIrrlichtToNeroPosition(ray.end); } }
void SelectionSystem::update ( float timestep ) { EventReceiver::SMouseState mouseState = *EventReceiver::getMouseState(); // Calculate click point on terrain ISceneCollisionManager* colmgr = Game::game.getRendMgr()->getSceneManager()->getSceneCollisionManager(); line3df ray = colmgr->getRayFromScreenCoordinates(EventReceiver::getMouseState()->position); vector3df point; triangle3df triangle; ISceneNode* node; int hitID = -1; if ((hitID = colmgr->getSceneNodeFromRayBB(ray)->getID()) > -1) { //std::cout << "mouse over" << std::endl; } if (mouseState.leftPressed) { if (!dragging) { dragging = true; clickPos = vector2d<int>(mouseState.position.X, mouseState.position.Y); mousePos = vector2d<int>(mouseState.position.X, mouseState.position.Y); } else { mousePos = vector2d<int>(mouseState.position.X, mouseState.position.Y); } } else { if (dragging) { selectObjects(); dragging = false; int hitID = -1; if ((hitID = EventReceiver::getHoverObject()) > -1) { if (Game::game.getObjMgr()->getObjectComponent<SelectableComponent>(hitID, "SelectableComponent") != nullptr) { Game::game.getObjMgr()->getObjectComponent<SelectableComponent>(hitID, "SelectableComponent")->selected = true; } } } } }
int main(void){ MyEventReceiver receiver; //Device, driver et graphe de scene irr::IrrlichtDevice* device = irr::createDevice(irr::video::EDT_OPENGL, irr::core::dimension2d<irr::u32>(800, 600), 32, false, false, false, &receiver); irr::video::IVideoDriver* driver = device->getVideoDriver(); /*irr::video::SMaterial material; material.FrontfaceCulling = true; driver->setMaterial(material);*/ irr::scene::ISceneManager *sceneManager = device->getSceneManager(); //On rend invisible le curseur //device->getCursorControl()->setVisible(false); RubicCube *cube = new RubicCube(sceneManager); irr::scene::IMeshSceneNode *c_centre = sceneManager->addCubeSceneNode(BLOC_S, 0, 33, irr::core::vector3df(0, 0, 0)); c_centre->setMaterialType(irr::video::EMT_SOLID); c_centre->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\blue.jpg")); irr::scene::IMeshSceneNode *c_up = sceneManager->addCubeSceneNode(BLOC_S, 0, 34, irr::core::vector3df(-(BLOC_S+BLOC_SPC), 0, 0)); c_up->setMaterialType(irr::video::EMT_SOLID); c_up->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_down = sceneManager->addCubeSceneNode(BLOC_S, 0, 35, irr::core::vector3df(BLOC_S+BLOC_SPC, 0, 0)); c_down->setMaterialType(irr::video::EMT_SOLID); c_down->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_north = sceneManager->addCubeSceneNode(BLOC_S, 0, 36, irr::core::vector3df(0, BLOC_S+BLOC_SPC, 0)); c_north->setMaterialType(irr::video::EMT_SOLID); c_north->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_south = sceneManager->addCubeSceneNode(BLOC_S, 0, 37, irr::core::vector3df(0, -(BLOC_S+BLOC_SPC), 0)); c_south->setMaterialType(irr::video::EMT_SOLID); c_south->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_east = sceneManager->addCubeSceneNode(BLOC_S, 0, 38, irr::core::vector3df(0, 0, BLOC_S+BLOC_SPC)); c_east->setMaterialType(irr::video::EMT_SOLID); c_east->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_west = sceneManager->addCubeSceneNode(BLOC_S, 0, 39, irr::core::vector3df(0, 0, -(BLOC_S+BLOC_SPC))); c_west->setMaterialType(irr::video::EMT_SOLID); c_west->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\red.jpg")); irr::scene::IMeshSceneNode *c_111 = sceneManager->addCubeSceneNode(BLOC_S, c_down, 40, irr::core::vector3df(0, -(BLOC_S+BLOC_SPC), -(BLOC_S+BLOC_SPC))); c_111->setMaterialType(irr::video::EMT_SOLID); c_111->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\blue.jpg")); irr::scene::IMeshSceneNode *c_121 = sceneManager->addCubeSceneNode(BLOC_S, c_down, 41, irr::core::vector3df(0, -(BLOC_S+BLOC_SPC), 0)); c_121->setMaterialType(irr::video::EMT_SOLID); c_121->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\blue.jpg")); irr::scene::IMeshSceneNode *c_131 = sceneManager->addCubeSceneNode(BLOC_S, c_down, 42, irr::core::vector3df(0, -(BLOC_S+BLOC_SPC), BLOC_S+BLOC_SPC)); c_131->setMaterialType(irr::video::EMT_SOLID); c_131->setMaterialTexture(0, driver->getTexture("E:\\Codage\\Rubirrks\\Ressources\\blue.jpg")); /*irr::scene::IMesh *cubeMesh = sceneManager->getMesh("E:\\Codage\\Rubirrks\\Ressources\\cube.irrmesh"); irr::scene::IMeshSceneNode *c_test = sceneManager->addMeshSceneNode(cubeMesh, 0, 99, irr::core::vector3df(0, 100, 100)); IMesh *cubedMesh = sceneManager->getMesh("E:\\Codage\\Rubirrks\\cube.irrmesh"); IMeshSceneNode *meshtest = sceneManager->addOctTreeSceneNode(cubedMesh, 0, 888); meshtest->setPosition(vector3df(0, 200, 0)); */ //sceneManager->setAmbientLight(irr::video::SColorf(1.0, 1.0, 1.0, 0.0)); irr::scene::ICameraSceneNode *camera; camera = sceneManager->addCameraSceneNodeMaya(0, 100.0f, 200.0f, 2000.0f, 77); //camera = sceneManager->addCameraSceneNode(cube->getRubicElt(1, 1, 1)->anode, vector3df(100, 0, 0), vector3df(0, 0, 50)); ISceneCollisionManager *colisionManager = sceneManager->getSceneCollisionManager(); //ISceneNode *node = cube->getRubicElt(2, 2, 2)->node; //IAnimatedMeshSceneNode *node = cube->getRubicElt(2, 2, 2)->anode; //scene::ITriangleSelector* selector = 0; //selector = sceneManager->createTriangleSelector(node); //selector = sceneManager->createTriangleSelectorFromBoundingBox(node); // node->setTriangleSelector(selector); //selector->drop(); //La chaine pour le titre de la fenetre irr::core::vector3df posCam; core::position2d<s32> posCursor = device->getCursorControl()->getPosition(); /*** essay gestion colision mouse cube ***/ irr::core::line3d<float> collRay; collRay = colisionManager->getRayFromScreenCoordinates(posCursor, sceneManager->getActiveCamera()); // get the collision core::vector3df collPoint; core::triangle3df collTriangle; IAnimatedMeshSceneNode *node = 0; const scene::ISceneNode* pNode = 0; //pNode = colisionManager->getSceneNodeAndCollisionPointFromRay(collRay, collPoint, collTriangle); /****************************************/ wchar_t titre[100]; bool hasCollision = false; int i = 0; //La boucle de rendu while(device->run()){ posCam = camera->getPosition(); posCursor = device->getCursorControl()->getPosition(); swprintf(titre, 100, L"FPS : %d (mode debug) | PosCam : " "X : %f Y : %f Z : %f, m.X : %ld m.Y : %ld", driver->getFPS(), posCam.X, posCam.Y, posCam.Z, posCursor.X, posCursor.Y); device->setWindowCaption(titre); collRay = colisionManager->getRayFromScreenCoordinates(posCursor, sceneManager->getActiveCamera()); for(int i = 0; i < 3; i++){ //if(hasCollision) break; for(int j = 0; j < 3; j++){ //if(hasCollision) break; for(int k = 0; k < 3; k++){ //if(hasCollision) break; node = cube->getRubicElt(i, j, k)->anode; hasCollision = colisionManager->getCollisionPoint(collRay, node->getTriangleSelector(), collPoint, collTriangle, pNode); if(hasCollision && pNode->isTrulyVisible() && !sceneManager->isCulled(pNode)){ //fprintf(stdout, "ID : %ld\n", pNode->getID()); fprintf(stdout, "collPoint.X : %f, collPoint.Y : %f, collPoint.Z : %f\n,", collPoint.X, collPoint.Y, collPoint.Z); //hasCollision = false; //pNode->setMaterialFlag(video::EMF_LIGHTING, receiver.GetMouseState().LeftButtonDown); cube->getRubicElt(i, j, k)->anode->setMaterialFlag(video::EMF_WIREFRAME, receiver.GetMouseState().RightButtonDown); //cube->getRubicElt(i, j, k)->anode->setMaterialFlag(video::EMF_LIGHTING, receiver.GetMouseState().RightButtonDown); //cube->rotate(cube->getRubicElt(1, 1, 1), vector3df(0, 0, 0)); } } } } //hasCollision = false; //collRay = colisionManager->getRayFromScreenCoordinates(posCursor, sceneManager->getActiveCamera()); //oui = colisionManager->getCollisionPoint(collRay, node->getTriangleSelector(), collPoint, collTriangle, pNode); //if(oui){ // //printf("YYYYYYYEEEEEEAAAAAAHHHHHH\n\n"); // fprintf(stdout, "ID : %ld\n", pNode->getID()); // oui = false; //} //printf("posCur.X : %ld, posCur.Y : %ld,, posNode.X : %ld, posNode.Y : %ld\n", posCursor.X, posCursor.Y, posNode.X, posNode.Y); driver->beginScene(true, true, irr::video::SColor(0, 50, 70, 50)); //c_down->setRotation(irr::core::vector3df(i*2.f, 0, 0)); i++; sceneManager->drawAll(); driver->endScene (); device->sleep(20); } device->drop(); return 0; }