2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CSceneCollisionManager.h"
|
|
|
|
#include "ICameraSceneNode.h"
|
|
|
|
#include "SViewFrustum.h"
|
|
|
|
|
|
|
|
#include "os.h"
|
|
|
|
#include "irrMath.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
2024-03-20 19:35:52 +01:00
|
|
|
CSceneCollisionManager::CSceneCollisionManager(ISceneManager *smanager, video::IVideoDriver *driver) :
|
|
|
|
SceneManager(smanager), Driver(driver)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef _DEBUG
|
2023-10-03 20:37:00 +02:00
|
|
|
setDebugName("CSceneCollisionManager");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (Driver)
|
|
|
|
Driver->grab();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! destructor
|
|
|
|
CSceneCollisionManager::~CSceneCollisionManager()
|
|
|
|
{
|
|
|
|
if (Driver)
|
|
|
|
Driver->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns a 3d ray which would go through the 2d screen coordinates.
|
|
|
|
core::line3d<f32> CSceneCollisionManager::getRayFromScreenCoordinates(
|
2024-03-20 19:35:52 +01:00
|
|
|
const core::position2d<s32> &pos, const ICameraSceneNode *camera)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
core::line3d<f32> ln(0, 0, 0, 0, 0, 0);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (!SceneManager)
|
|
|
|
return ln;
|
|
|
|
|
|
|
|
if (!camera)
|
|
|
|
camera = SceneManager->getActiveCamera();
|
|
|
|
|
|
|
|
if (!camera)
|
|
|
|
return ln;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const scene::SViewFrustum *f = camera->getViewFrustum();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
core::vector3df farLeftUp = f->getFarLeftUp();
|
|
|
|
core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
|
|
|
|
core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const core::rect<s32> &viewPort = Driver->getViewPort();
|
2023-10-03 20:37:00 +02:00
|
|
|
core::dimension2d<u32> screenSize(viewPort.getWidth(), viewPort.getHeight());
|
|
|
|
|
|
|
|
f32 dx = pos.X / (f32)screenSize.Width;
|
|
|
|
f32 dy = pos.Y / (f32)screenSize.Height;
|
|
|
|
|
|
|
|
if (camera->isOrthogonal())
|
2024-03-20 19:35:52 +01:00
|
|
|
ln.start = f->cameraPosition + (lefttoright * (dx - 0.5f)) + (uptodown * (dy - 0.5f));
|
2023-10-03 20:37:00 +02:00
|
|
|
else
|
|
|
|
ln.start = f->cameraPosition;
|
|
|
|
|
|
|
|
ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);
|
|
|
|
|
|
|
|
return ln;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|