1
// Copyright (C) 2002-2011 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
#ifndef __C_SCENE_COLLISION_MANAGER_H_INCLUDED__
6
#define __C_SCENE_COLLISION_MANAGER_H_INCLUDED__
8
#include "ISceneCollisionManager.h"
9
#include "ISceneManager.h"
10
#include "IVideoDriver.h"
17
//! The Scene Collision Manager provides methods for performing collision tests and picking on scene nodes.
18
class CSceneCollisionManager : public ISceneCollisionManager
23
CSceneCollisionManager(ISceneManager* smanager, video::IVideoDriver* driver);
26
virtual ~CSceneCollisionManager();
28
//! Returns the scene node, which is currently visible at the given
29
//! screen coordinates, viewed from the currently active camera.
30
virtual ISceneNode* getSceneNodeFromScreenCoordinatesBB(const core::position2d<s32>& pos,
31
s32 idBitMask=0, bool bNoDebugObjects=false, ISceneNode* root=0);
33
//! Returns the nearest scene node which collides with a 3d ray and
34
//! whose id matches a bitmask.
35
virtual ISceneNode* getSceneNodeFromRayBB(const core::line3d<f32>& ray,
36
s32 idBitMask=0, bool bNoDebugObjects=false,
39
//! Returns the scene node, at which the overgiven camera is looking at and
40
//! which id matches the bitmask.
41
virtual ISceneNode* getSceneNodeFromCameraBB(ICameraSceneNode* camera,
42
s32 idBitMask=0, bool bNoDebugObjects = false);
44
//! Finds the collision point of a line and lots of triangles, if there is one.
45
virtual bool getCollisionPoint(const core::line3d<f32>& ray,
46
ITriangleSelector* selector, core::vector3df& outCollisionPoint,
47
core::triangle3df& outTriangle,
48
ISceneNode* & outNode);
50
//! Collides a moving ellipsoid with a 3d world with gravity and returns
51
//! the resulting new position of the ellipsoid.
52
virtual core::vector3df getCollisionResultPosition(
53
ITriangleSelector* selector,
54
const core::vector3df &ellipsoidPosition,
55
const core::vector3df& ellipsoidRadius,
56
const core::vector3df& ellipsoidDirectionAndSpeed,
57
core::triangle3df& triout,
58
core::vector3df& hitPosition,
62
const core::vector3df& gravityDirectionAndSpeed);
64
//! Returns a 3d ray which would go through the 2d screen coodinates.
65
virtual core::line3d<f32> getRayFromScreenCoordinates(
66
const core::position2d<s32> & pos, ICameraSceneNode* camera = 0);
68
//! Calculates 2d screen position from a 3d position.
69
virtual core::position2d<s32> getScreenCoordinatesFrom3DPosition(
70
const core::vector3df & pos, ICameraSceneNode* camera=0, bool useViewPort=false);
72
//! Gets the scene node and nearest collision point for a ray based on
73
//! the nodes' id bitmasks, bounding boxes and triangle selectors.
74
virtual ISceneNode* getSceneNodeAndCollisionPointFromRay(
76
core::vector3df & outCollisionPoint,
77
core::triangle3df & outTriangle,
79
ISceneNode * collisionRootNode = 0,
80
bool noDebugObjects = false);
85
//! recursive method for going through all scene nodes
86
void getPickedNodeBB(ISceneNode* root, core::line3df& ray, s32 bits,
88
f32& outbestdistance, ISceneNode*& outbestnode);
90
//! recursive method for going through all scene nodes
91
void getPickedNodeFromBBAndSelector(ISceneNode * root,
95
f32 & outBestDistanceSquared,
96
ISceneNode * & outBestNode,
97
core::vector3df & outBestCollisionPoint,
98
core::triangle3df & outBestTriangle);
101
struct SCollisionData
103
core::vector3df eRadius;
105
core::vector3df R3Velocity;
106
core::vector3df R3Position;
108
core::vector3df velocity;
109
core::vector3df normalizedVelocity;
110
core::vector3df basePoint;
114
core::vector3df intersectionPoint;
116
core::triangle3df intersectionTriangle;
122
ITriangleSelector* selector;
125
//! Tests the current collision data against an individual triangle.
127
\param colData: the collision data.
128
\param triangle: the triangle to test against.
129
\return true if the triangle is hit (and is the closest hit), false otherwise */
130
bool testTriangleIntersection(SCollisionData* colData,
131
const core::triangle3df& triangle);
133
//! recursive method for doing collision response
134
core::vector3df collideEllipsoidWithWorld(ITriangleSelector* selector,
135
const core::vector3df &position,
136
const core::vector3df& radius, const core::vector3df& velocity,
138
const core::vector3df& gravity, core::triangle3df& triout,
139
core::vector3df& hitPosition,
141
ISceneNode*& outNode);
143
core::vector3df collideWithWorld(s32 recursionDepth, SCollisionData &colData,
144
core::vector3df pos, core::vector3df vel);
146
inline bool getLowestRoot(f32 a, f32 b, f32 c, f32 maxR, f32* root);
148
ISceneManager* SceneManager;
149
video::IVideoDriver* Driver;
150
core::array<core::triangle3df> Triangles; // triangle buffer
154
} // end namespace scene
155
} // end namespace irr