Move SMaterial std::hash impl to its header
[minetest.git] / irr / src / CSceneCollisionManager.cpp
blob77549a7dcc2b2fbb7abd575cb0371025f2f2bce4
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
5 #include "CSceneCollisionManager.h"
6 #include "ICameraSceneNode.h"
7 #include "SViewFrustum.h"
9 #include "irrMath.h"
11 namespace irr
13 namespace scene
16 //! constructor
17 CSceneCollisionManager::CSceneCollisionManager(ISceneManager *smanager, video::IVideoDriver *driver) :
18 SceneManager(smanager), Driver(driver)
20 #ifdef _DEBUG
21 setDebugName("CSceneCollisionManager");
22 #endif
24 if (Driver)
25 Driver->grab();
28 //! destructor
29 CSceneCollisionManager::~CSceneCollisionManager()
31 if (Driver)
32 Driver->drop();
35 //! Returns a 3d ray which would go through the 2d screen coordinates.
36 core::line3d<f32> CSceneCollisionManager::getRayFromScreenCoordinates(
37 const core::position2d<s32> &pos, const ICameraSceneNode *camera)
39 core::line3d<f32> ln(0, 0, 0, 0, 0, 0);
41 if (!SceneManager)
42 return ln;
44 if (!camera)
45 camera = SceneManager->getActiveCamera();
47 if (!camera)
48 return ln;
50 const scene::SViewFrustum *f = camera->getViewFrustum();
52 core::vector3df farLeftUp = f->getFarLeftUp();
53 core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
54 core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;
56 const core::rect<s32> &viewPort = Driver->getViewPort();
57 core::dimension2d<u32> screenSize(viewPort.getWidth(), viewPort.getHeight());
59 f32 dx = pos.X / (f32)screenSize.Width;
60 f32 dy = pos.Y / (f32)screenSize.Height;
62 if (camera->isOrthogonal())
63 ln.start = f->cameraPosition + (lefttoright * (dx - 0.5f)) + (uptodown * (dy - 0.5f));
64 else
65 ln.start = f->cameraPosition;
67 ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);
69 return ln;
72 } // end namespace scene
73 } // end namespace irr