irrlicht/source/Irrlicht/CSceneCollisionManager.cpp

957 lines
29 KiB
C++
Raw Normal View History

// Copyright (C) 2002-2011 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 "ISceneNode.h"
#include "ICameraSceneNode.h"
#include "ITriangleSelector.h"
#include "SViewFrustum.h"
#include "os.h"
#include "irrMath.h"
namespace irr
{
namespace scene
{
//! constructor
CSceneCollisionManager::CSceneCollisionManager(ISceneManager* smanager, video::IVideoDriver* driver)
: SceneManager(smanager), Driver(driver)
{
#ifdef _DEBUG
setDebugName("CSceneCollisionManager");
#endif
if (Driver)
Driver->grab();
}
//! destructor
CSceneCollisionManager::~CSceneCollisionManager()
{
if (Driver)
Driver->drop();
}
//! Returns the scene node, which is currently visible at the given
//! screen coordinates, viewed from the currently active camera.
ISceneNode* CSceneCollisionManager::getSceneNodeFromScreenCoordinatesBB(
const core::position2d<s32>& pos, s32 idBitMask, bool noDebugObjects, scene::ISceneNode* root)
{
const core::line3d<f32> ln = getRayFromScreenCoordinates(pos, 0);
if ( ln.start == ln.end )
return 0;
return getSceneNodeFromRayBB(ln, idBitMask, noDebugObjects, root);
}
//! Returns the nearest scene node which collides with a 3d ray and
//! which id matches a bitmask.
ISceneNode* CSceneCollisionManager::getSceneNodeFromRayBB(
const core::line3d<f32>& ray,
s32 idBitMask, bool noDebugObjects, scene::ISceneNode* root)
{
ISceneNode* best = 0;
f32 dist = FLT_MAX;
core::line3d<f32> truncatableRay(ray);
getPickedNodeBB((root==0)?SceneManager->getRootSceneNode():root, truncatableRay,
idBitMask, noDebugObjects, dist, best);
return best;
}
//! recursive method for going through all scene nodes
void CSceneCollisionManager::getPickedNodeBB(ISceneNode* root,
core::line3df& ray, s32 bits, bool noDebugObjects,
f32& outbestdistance, ISceneNode*& outbestnode)
{
const ISceneNodeList& children = root->getChildren();
const core::vector3df rayVector = ray.getVector().normalize();
ISceneNodeList::ConstIterator it = children.begin();
for (; it != children.end(); ++it)
{
ISceneNode* current = *it;
if (current->isVisible())
{
if((noDebugObjects ? !current->isDebugObject() : true) &&
(bits==0 || (bits != 0 && (current->getID() & bits))))
{
// get world to object space transform
core::matrix4 worldToObject;
if (!current->getAbsoluteTransformation().getInverse(worldToObject))
continue;
// transform vector from world space to object space
core::line3df objectRay(ray);
worldToObject.transformVect(objectRay.start);
worldToObject.transformVect(objectRay.end);
const core::aabbox3df & objectBox = current->getBoundingBox();
// Do the initial intersection test in object space, since the
// object space box test is more accurate.
if(objectBox.isPointInside(objectRay.start))
{
// use fast bbox intersection to find distance to hitpoint
// algorithm from Kay et al., code from gamedev.net
const core::vector3df dir = (objectRay.end-objectRay.start).normalize();
const core::vector3df minDist = (objectBox.MinEdge - objectRay.start)/dir;
const core::vector3df maxDist = (objectBox.MaxEdge - objectRay.start)/dir;
const core::vector3df realMin(core::min_(minDist.X, maxDist.X),core::min_(minDist.Y, maxDist.Y),core::min_(minDist.Z, maxDist.Z));
const core::vector3df realMax(core::max_(minDist.X, maxDist.X),core::max_(minDist.Y, maxDist.Y),core::max_(minDist.Z, maxDist.Z));
const f32 minmax = core::min_(realMax.X, realMax.Y, realMax.Z);
// nearest distance to intersection
const f32 maxmin = core::max_(realMin.X, realMin.Y, realMin.Z);
const f32 toIntersectionSq = (maxmin>0?maxmin*maxmin:minmax*minmax);
if (toIntersectionSq < outbestdistance)
{
outbestdistance = toIntersectionSq;
outbestnode = current;
// And we can truncate the ray to stop us hitting further nodes.
ray.end = ray.start + (rayVector * sqrtf(toIntersectionSq));
}
}
else
if (objectBox.intersectsWithLine(objectRay))
{
// Now transform into world space, since we need to use world space
// scales and distances.
core::aabbox3df worldBox(objectBox);
current->getAbsoluteTransformation().transformBox(worldBox);
core::vector3df edges[8];
worldBox.getEdges(edges);
/* We need to check against each of 6 faces, composed of these corners:
/3--------/7
/ | / |
/ | / |
1---------5 |
| 2- - -| -6
| / | /
|/ | /
0---------4/
Note that we define them as opposite pairs of faces.
*/
static const s32 faceEdges[6][3] =
{
{ 0, 1, 5 }, // Front
{ 6, 7, 3 }, // Back
{ 2, 3, 1 }, // Left
{ 4, 5, 7 }, // Right
{ 1, 3, 7 }, // Top
{ 2, 0, 4 } // Bottom
};
core::vector3df intersection;
core::plane3df facePlane;
f32 bestDistToBoxBorder = FLT_MAX;
f32 bestToIntersectionSq = FLT_MAX;
for(s32 face = 0; face < 6; ++face)
{
facePlane.setPlane(edges[faceEdges[face][0]],
edges[faceEdges[face][1]],
edges[faceEdges[face][2]]);
// Only consider lines that might be entering through this face, since we
// already know that the start point is outside the box.
if(facePlane.classifyPointRelation(ray.start) != core::ISREL3D_FRONT)
continue;
// Don't bother using a limited ray, since we already know that it should be long
// enough to intersect with the box.
if(facePlane.getIntersectionWithLine(ray.start, rayVector, intersection))
{
const f32 toIntersectionSq = ray.start.getDistanceFromSQ(intersection);
if(toIntersectionSq < outbestdistance)
{
// We have to check that the intersection with this plane is actually
// on the box, so need to go back to object space again.
worldToObject.transformVect(intersection);
// find the closest point on the box borders. Have to do this as exact checks will fail due to floating point problems.
f32 distToBorder = core::max_ ( core::min_ (core::abs_(objectBox.MinEdge.X-intersection.X), core::abs_(objectBox.MaxEdge.X-intersection.X)),
core::min_ (core::abs_(objectBox.MinEdge.Y-intersection.Y), core::abs_(objectBox.MaxEdge.Y-intersection.Y)),
core::min_ (core::abs_(objectBox.MinEdge.Z-intersection.Z), core::abs_(objectBox.MaxEdge.Z-intersection.Z)) );
if ( distToBorder < bestDistToBoxBorder )
{
bestDistToBoxBorder = distToBorder;
bestToIntersectionSq = toIntersectionSq;
}
}
}
// If the ray could be entering through the first face of a pair, then it can't
// also be entering through the opposite face, and so we can skip that face.
if (!(face & 0x01))
++face;
}
if ( bestDistToBoxBorder < FLT_MAX )
{
outbestdistance = bestToIntersectionSq;
outbestnode = current;
// If we got a hit, we can now truncate the ray to stop us hitting further nodes.
ray.end = ray.start + (rayVector * sqrtf(outbestdistance));
}
}
}
// Only check the children if this node is visible.
getPickedNodeBB(current, ray, bits, noDebugObjects, outbestdistance, outbestnode);
}
}
}
ISceneNode* CSceneCollisionManager::getSceneNodeAndCollisionPointFromRay(
core::line3df ray,
core::vector3df & outCollisionPoint,
core::triangle3df & outTriangle,
s32 idBitMask,
ISceneNode * collisionRootNode,
bool noDebugObjects)
{
ISceneNode* bestNode = 0;
f32 bestDistanceSquared = FLT_MAX;
if(0 == collisionRootNode)
collisionRootNode = SceneManager->getRootSceneNode();
// We don't try to do anything too clever, like sorting the candidate
// nodes by distance to bounding-box. In the example below, we could do the
// triangle collision check with node A first, but we'd have to check node B
// anyway, as the actual collision point could be (and is) closer than the
// collision point in node A.
//
// ray end
// |
// AAAAAAAAAA
// A |
// A | B
// A | B
// A BBBBB
// A |
// A |
// |
// |
// ray start
//
// We therefore have to do a full BB and triangle collision on every scene
// node in order to find the nearest collision point, so sorting them by
// bounding box would be pointless.
getPickedNodeFromBBAndSelector(collisionRootNode, ray, idBitMask,
noDebugObjects, bestDistanceSquared, bestNode,
outCollisionPoint, outTriangle);
return bestNode;
}
void CSceneCollisionManager::getPickedNodeFromBBAndSelector(
ISceneNode * root,
core::line3df & ray,
s32 bits,
bool noDebugObjects,
f32 & outBestDistanceSquared,
ISceneNode * & outBestNode,
core::vector3df & outBestCollisionPoint,
core::triangle3df & outBestTriangle)
{
const ISceneNodeList& children = root->getChildren();
ISceneNodeList::ConstIterator it = children.begin();
for (; it != children.end(); ++it)
{
ISceneNode* current = *it;
ITriangleSelector * selector = current->getTriangleSelector();
if (selector && current->isVisible() &&
(noDebugObjects ? !current->isDebugObject() : true) &&
(bits==0 || (bits != 0 && (current->getID() & bits))))
{
// get world to object space transform
core::matrix4 mat;
if (!current->getAbsoluteTransformation().getInverse(mat))
continue;
// transform vector from world space to object space
core::line3df line(ray);
mat.transformVect(line.start);
mat.transformVect(line.end);
const core::aabbox3df& box = current->getBoundingBox();
core::vector3df candidateCollisionPoint;
core::triangle3df candidateTriangle;
// do intersection test in object space
ISceneNode * hitNode = 0;
if (box.intersectsWithLine(line) &&
getCollisionPoint(ray, selector, candidateCollisionPoint, candidateTriangle, hitNode))
{
const f32 distanceSquared = (candidateCollisionPoint - ray.start).getLengthSQ();
if(distanceSquared < outBestDistanceSquared)
{
outBestDistanceSquared = distanceSquared;
outBestNode = current;
outBestCollisionPoint = candidateCollisionPoint;
outBestTriangle = candidateTriangle;
const core::vector3df rayVector = ray.getVector().normalize();
ray.end = ray.start + (rayVector * sqrtf(distanceSquared));
}
}
}
getPickedNodeFromBBAndSelector(current, ray, bits, noDebugObjects,
outBestDistanceSquared, outBestNode,
outBestCollisionPoint, outBestTriangle);
}
}
//! Returns the scene node, at which the overgiven camera is looking at and
//! which id matches the bitmask.
ISceneNode* CSceneCollisionManager::getSceneNodeFromCameraBB(
ICameraSceneNode* camera, s32 idBitMask, bool noDebugObjects)
{
if (!camera)
return 0;
const core::vector3df start = camera->getAbsolutePosition();
core::vector3df end = camera->getTarget();
end = start + ((end - start).normalize() * camera->getFarValue());
return getSceneNodeFromRayBB(core::line3d<f32>(start, end), idBitMask, noDebugObjects);
}
//! Finds the collision point of a line and lots of triangles, if there is one.
bool CSceneCollisionManager::getCollisionPoint(const core::line3d<f32>& ray,
ITriangleSelector* selector, core::vector3df& outIntersection,
core::triangle3df& outTriangle,
ISceneNode*& outNode)
{
if (!selector)
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return false;
}
s32 totalcnt = selector->getTriangleCount();
Triangles.set_used(totalcnt);
s32 cnt = 0;
selector->getTriangles(Triangles.pointer(), totalcnt, cnt, ray);
const core::vector3df linevect = ray.getVector().normalize();
core::vector3df intersection;
f32 nearest = FLT_MAX;
bool found = false;
const f32 raylength = ray.getLengthSQ();
const f32 minX = core::min_(ray.start.X, ray.end.X);
const f32 maxX = core::max_(ray.start.X, ray.end.X);
const f32 minY = core::min_(ray.start.Y, ray.end.Y);
const f32 maxY = core::max_(ray.start.Y, ray.end.Y);
const f32 minZ = core::min_(ray.start.Z, ray.end.Z);
const f32 maxZ = core::max_(ray.start.Z, ray.end.Z);
for (s32 i=0; i<cnt; ++i)
{
const core::triangle3df & triangle = Triangles[i];
if(minX > triangle.pointA.X && minX > triangle.pointB.X && minX > triangle.pointC.X)
continue;
if(maxX < triangle.pointA.X && maxX < triangle.pointB.X && maxX < triangle.pointC.X)
continue;
if(minY > triangle.pointA.Y && minY > triangle.pointB.Y && minY > triangle.pointC.Y)
continue;
if(maxY < triangle.pointA.Y && maxY < triangle.pointB.Y && maxY < triangle.pointC.Y)
continue;
if(minZ > triangle.pointA.Z && minZ > triangle.pointB.Z && minZ > triangle.pointC.Z)
continue;
if(maxZ < triangle.pointA.Z && maxZ < triangle.pointB.Z && maxZ < triangle.pointC.Z)
continue;
if (triangle.getIntersectionWithLine(ray.start, linevect, intersection))
{
const f32 tmp = intersection.getDistanceFromSQ(ray.start);
const f32 tmp2 = intersection.getDistanceFromSQ(ray.end);
if (tmp < raylength && tmp2 < raylength && tmp < nearest)
{
nearest = tmp;
outTriangle = triangle;
outIntersection = intersection;
outNode = selector->getSceneNodeForTriangle(i);
found = true;
}
}
}
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return found;
}
//! Collides a moving ellipsoid with a 3d world with gravity and returns
//! the resulting new position of the ellipsoid.
core::vector3df CSceneCollisionManager::getCollisionResultPosition(
ITriangleSelector* selector,
const core::vector3df &position, const core::vector3df& radius,
const core::vector3df& direction,
core::triangle3df& triout,
core::vector3df& hitPosition,
bool& outFalling,
ISceneNode*& outNode,
f32 slidingSpeed,
const core::vector3df& gravity)
{
return collideEllipsoidWithWorld(selector, position,
radius, direction, slidingSpeed, gravity, triout, hitPosition, outFalling, outNode);
}
bool CSceneCollisionManager::testTriangleIntersection(SCollisionData* colData,
const core::triangle3df& triangle)
{
const core::plane3d<f32> trianglePlane = triangle.getPlane();
// only check front facing polygons
if ( !trianglePlane.isFrontFacing(colData->normalizedVelocity) )
return false;
// get interval of plane intersection
f32 t1, t0;
bool embeddedInPlane = false;
// calculate signed distance from sphere position to triangle plane
f32 signedDistToTrianglePlane = trianglePlane.getDistanceTo(
colData->basePoint);
f32 normalDotVelocity =
trianglePlane.Normal.dotProduct(colData->velocity);
if ( core::iszero ( normalDotVelocity ) )
{
// sphere is traveling parallel to plane
if (fabs(signedDistToTrianglePlane) >= 1.0f)
return false; // no collision possible
else
{
// sphere is embedded in plane
embeddedInPlane = true;
t0 = 0.0;
t1 = 1.0;
}
}
else
{
normalDotVelocity = core::reciprocal ( normalDotVelocity );
// N.D is not 0. Calculate intersection interval
t0 = (-1.f - signedDistToTrianglePlane) * normalDotVelocity;
t1 = (1.f - signedDistToTrianglePlane) * normalDotVelocity;
// Swap so t0 < t1
if (t0 > t1) { f32 tmp = t1; t1 = t0; t0 = tmp; }
// check if at least one value is within the range
if (t0 > 1.0f || t1 < 0.0f)
return false; // both t values are outside 1 and 0, no collision possible
// clamp to 0 and 1
t0 = core::clamp ( t0, 0.f, 1.f );
t1 = core::clamp ( t1, 0.f, 1.f );
}
// at this point we have t0 and t1, if there is any intersection, it
// is between this interval
core::vector3df collisionPoint;
bool foundCollision = false;
f32 t = 1.0f;
// first check the easy case: Collision within the triangle;
// if this happens, it must be at t0 and this is when the sphere
// rests on the front side of the triangle plane. This can only happen
// if the sphere is not embedded in the triangle plane.
if (!embeddedInPlane)
{
core::vector3df planeIntersectionPoint =
(colData->basePoint - trianglePlane.Normal)
+ (colData->velocity * t0);
Changes in version 1.6, TA - FileSystem 2.0 SUPER MASTER MAJOR API CHANGE !!! The FileSystem is know build internally like for e.q the texture-, and the meshloaders. There exists a known list of ArchiveLoader, which know how to produce a Archive. The Loaders and the Archive can be attached/detached on runtime. The FileNames are now stored as core::string<c16>. where c16 is toggled between char/wchar with the #define flag _IRR_WCHAR_FILESYSTEM, to supported unicode backends (default:off) I replaced all (const c8* filename) to string references. Basically the FileSystem is divided into two regions. Native and Virtual. Native means using the backend OS. Virtual means only use currently attach IArchives. Browsing each FileSystem has it's own workdirectory and it's own methods to - create a FileTree - add/remove files & directory ( to be done ) Hint: store a savegame in a zip archive... basic browsing for all archives is implemented. Example 21. Quake3Explorer shows this TODO: - a file filter should be implemented. - The IArchive should have a function to create a filetree for now CFileList is used. Class Hiarchy: IArchiveLoader: is able to produce a IFileArchive - ZipLoader - PakLoader - MountPointReader ( formaly known as CUnzipReader ) IFileArchive: -ZipArchive -PakArchive -MountPoint (known as FolderFile) IFileSystem - addArchiveLoader - changed implementation of isALoadableFileExtension in all loaders to have consistent behavior - added a parameter to IFileList * createFileList setFileListSystem allows to query files in any of the game archives standard behavior listtype = SYSTEM ( default) - CLimitReadFile added multiple file random-access support. solved problems with mixed compressed & uncompressed files in a zip TODO: - Big Big Testing!! - Linux Version ( minor ) - remove all double loader interfaces where only the filename differs (IReadFile/const char *filename). This blows up the the interface - many loaders use their own private filesearching we should rework this - there are a lot of helper function ( getAbsolutePath, getFileDir ) which should be adapted to the virtual filesystem - IrrlichtDevice added: virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ) = 0; virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue ) = 0; and calculating methods to DeviceStub. implemented in Win32, TODO: other Devices - irrlicht.h changed exported irrlicht.dll routines createDevice, createDeviceEx, IdentityMatrix to extern "C" name mangling. for easier dynamically loading the irrlicht library and different versions - ParticleSystem removed the private (old?,wrong?) interface from the ParticleEffectors to match the parent class irr::io::IAttributeExchangingObject::deserializeAttributes TODO: please test if the serialization works! - Generic - vector3d<T>& normalize() #if 0 f32 length = (f32)(X*X + Y*Y + Z*Z); if (core::equals(length, 0.f)) return *this; length = core::reciprocal_squareroot ( (f32)length ); #else const T length = core::reciprocal_squareroot ( (X*X + Y*Y + Z*Z) ); #endif Weak checking on zero?!?! just to avoid a sqrt?. mhm, maybe not;-) added reciprocal_squareroot for f64 - dimension2d added operator dimension2d<T>& operator=(const dimension2d<U>& other) to cast between different types - vector2d bugfix: vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Width; return *this; } to vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; } - C3DMeshLoader renamed chunks const u16 to a enum removing "variable declared but never used warning" - added a global const identity Material changed all references *((video::SMaterial*)0) to point to IdentityMaterial removed warning: "a NULL reference is not allowed" - modified IRRLICHT_MATH to not support reciprocal stuff but to use faster float-to-int conversion. gcc troubles may they are. i'm using intel-compiler..;-) - core::matrix4 USE_MATRIX_TEST i tried to optimize the identity-check ( in means of performance) i didn't succeed so well, so i made a define for the matrix isIdentity -check for now it's sometimes faster to always calculate versus identity-check but if there are a lot of scenenodes/ particles one can profit from the fast_inverse matrix, when no scaling is used. further approvement could be done on inverse for just tranlastion! ( many static scenenodes are not rotated, they are just placed somewhere in the world) one thing to take in account is that sizeof(matrix) is 64 byte and with the additional bool/u32 makes it 66 byte which is not really cache-friendly.. - added buildRotateFromTo Builds a matrix that rotates from one vector to another - irr::array. changed allocating routine in push_back okt, 2008. it's only allowed to alloc one element, if default constructor has to be called. removes existing crashes. ( MD3 Mesh ) and possible others ones. A new list template should be made. one with constructor/destructor calls ( safe_array ) and one without. like the array since the beginning of irrlicht. currently the array/string is extremly slow.. also a hint for the user has to be done, so that a struct T of array<T> must have a copy constructor of type T ( const T&other ). i needed hours to track that down... added a new method setAllocStrategy, safe ( used + 1 ), double ( used * 2 + 1) better default strategies will be implemented - removed binary_search_const i added it quite a long time ago, but it doesnt make real sense a call to a sort method should happen always. i just wanted to safe a few cycles.. - added binary_search_multi searches for a multi-set ( more than 1 entry in the sorted array) returns start and end-index - changed some identity matrix settings to use core::IdentityMatrix - added deletePathFromFilename to generic string functions in coreutil.h and removed from CZipReader and CPakReader - s32 deserializeAttributes used instead of virtual void deserializeAttributes in ParticleSystem ( wrong virtual was used) - strings & Locale - started to add locale support - added verify to string - added some helper functions - XBOX i have access to a XBOX development machine now. I started to compile for the XBOX. Question: Who did the previous implementation?. There is no XBOX-Device inhere. maybe it's forbidden because of using the offical Microsoft XDK. I will implement a native or sdl device based on opendk. irrlicht compiles without errors on the xbox but can't be used. TODO: - native XBOX Device - Windows Mobile reworked a little. added the mobile example to the windows solution for cross development. added maximal 128x128 texture size for windows mobile ( memory issues ) - Collision Speed Up The Collision Speed Up greatly improves with many small static child-nodes - added COctTreeTriangleSelector::getTriangles for 3dline from user Piraaate - modified createOctTreeTriangleSelector and createTriangleSelector to allow node == 0, to be added to a meta selector - CSceneNodeAnimatorCollisionResponse has the same problem as CSceneNodeAnimatorFPS on first update: Problem. you start setting the map. (setWorld). First update cames 4000 ms later. The Animator applies the missing force... big problem... changed to react on first update like camera. - add Variable FirstUpdate. if set to true ( on all changes ) then position, lasttime, and falling are initialized -added #define OCTTREE_USE_HARDWARE in Octree.h if defined octtree uses internally a derived scene::MeshBuffer which has the possibility to use the Hardware Vertex Buffer for static vertices and dirty indices;-) if defined OCTTREE_USE_HARDWARE octree uses internally a derived scene::CMeshBuffer so it's not just a replacement inside the octree. It also in the OctTreeSceneNode. #if defined (OCTTREE_USE_HARDWARE) driver->drawMeshBuffer ( &LightMapMeshes[i] ); #else driver->drawIndexedTriangleList( &LightMapMeshes[i].Vertices[0], LightMapMeshes[i].Vertices.size(), d[i].Indices, d[i].CurrentSize / 3); #endif #define OCTTREE_PARENTTEST is also used. It's skip testing on fully outside and takes everything on fully inside - virtual void ISceneNode::updateAbsolutePosition() - changed inline CMatrix4<T> CMatrix4<T>::operator*(const CMatrix4<T>& m2) const all two matrices have to be checked by isIdentity() to let the isIdentity work always -changed inline bool CMatrix4<T>::isIdentity() const on full identityCheck-> to look first on Translation, because this is the most challenging element which will likely not to be identity.. - virtual core::matrix4 getRelativeTransformation() const Hiarchy on Identity-Check 1) ->getRelativeTransform -> 9 floating point checks to be passed as Identity 2) ->isIdentity () -> 16 floating point checks to be passed as Identity - inline void CMatrix4<T>::transformBoxEx(core::aabbox3d<f32>& box) const added isIdentity() check - changed CSceneNodeAnimatorCollisionResponse - added CSceneNodeAnimatorCollisionResponse::setGravity needed to set the differents Forces for the Animator. for eq. water.. - added CSceneNodeAnimatorCollisionResponse::setAnimateTarget - added CSceneNodeAnimatorCollisionResponse::getAnimateTarget - changed CSceneNodeAnimatorCollisionResponse::animateNode to react on FirstUpdate - changad Gravity to - TODO: set Gravity to Physically frame independent values.. current response uses an frame depdended acceleration vector. ~9.81 m/s^2 was achieved at around 50 fps with a setting of -0.03 may effect existing application.. - SceneNodes - CSkyDomeSceneNode moved radius ( default 1000 ) to constructor added Normals added DebugInfo added Material.ZBuffer, added SceneMaanager - CVolumeLightSceneNode: changed default blending OneTextureBlendgl_src_color gl_src_alpha to EMT_TRANSPARENT_ADD_COLOR ( gl_src_color gl_one ) which gives the same effect on non-transparent-materials. Following the unspoken guide-line, lowest effect as default - added LensFlareSceneNode (from forum user gammaray, modified to work ) showing in example special fx - changed SceneNode Skydome f64 to f32, - AnimatedMesh -Debug Data: mesh normals didn't rotate with the scenenode fixed ( matrix-multiplication order) - Camera SceneNode setPosition Camera now finally allow to change position and target and updates all effected animators.. a call to OnAnimate ( ) lastime < time or OnAnimate ( 0 ) will reset the camera and fr. the collision animator to a new position - Device: added the current mousebutton state to the Mouse Event so i need to get the current mouse state from the OS -a dded to CIrrDeviceWin32 TODO: - Linux and SDL Device - GUI - CGUIFont: - added virtual void setInvisibleCharacters( const wchar_t *s ) = 0; define which characters should not be drawn ( send to driver) by the font. for example " " would not draw any space which is usually blank in most fonts and saves rendering of ususally full blank alpha-sprites. This saves a lot of rendering... default: setInvisibleCharacters ( L" " ); - added MultiLine rendering should avoid to us CStaticText breaking text in future - CGUIListBox - changed Scrollbar LargeStepSize to ItemHeight which easy enables to scroll line by line - CGUIScrollBar bug: Create a Window and inside a listbox with a scrollbar or a windowed irrlicht application Click & hold Scrollbar Slider. move outside it's region. Release Mouse. Go Back to Scrollbar.. it's moving always... it's generally missing the event PRESSED_MOVED, which leads to problem when an element is dragging, has a focus, or position loose and gets focus back again. ( think of a drunken mouse sliding left&right during tracking ) so added the mouse Input Buttonstates on every mouse event IrrDeviceWin32: added event.MouseInput.ButtonStates = wParam & ( MK_LBUTTON | MK_RBUTTON | MK_MBUTTON ); TODO: Linux & SDL so now i can do this case irr::EMIE_MOUSE_MOVED: if ( !event.MouseInput.isLeftPressed () ) { Dragging = false; } - bug: Scrollbar notifyListBox notify when the scrollbar is clicked. - changed timed event in draw to OnPostRender Why the hell is a gui element firing a timed event in a draw routine!!!!!. This should be corrected for all gui-elements. - added GUI Image List from Reinhard Ostermeier, modified to work added GUI Tree View from Reinhard Ostermeier, modified to work shown in the Quake3MapShader Example TODO: Spritebanks - FileOpenDialog changed the static text for the filename to an edit box. - changed the interface for addEditBox to match with addStaticText - changed the interface for addSpinBox to match with addEditBox - added MouseWheel to Spinbox - changed CGUITable CLICK_AREA from 3 to 12 to enable clicking on the visible marker - CGUISpritebank removed some crashes with empty Sprite banks - IGUIScrollBar added SetMin before min was always 0 changed ScrollWheel Direction on horizontal to move right on wheel up, left on wheel down - IComboBox -added ItemData - removed IsVisbile check in IGUIElement::draw - Image Loaders - added TGA file type 2 ( grayscale uncompressed ) - added TGA file type (1) 8 Bit indexed color uncompressed ColorConverter: - added convert_B8G8R8toA8R8G8B8 - added convert_B8G8R8A8toA8R8G8B8 - Media Files - added missing shaders and textures to map-20kdm2. Taken from free implementation - ball.wav. adjusted DC-Offset, amplified to -4dB, trim cross-zero - impact.wav clip-restoration, trim cross-zero - added gun.md2, gun.pcx to media-files copyright issues!. i don't know from where this file came from... i hope this is not from original quake2.. - added new irrlicht logo irrlicht3.png i've taken the new layout. i should ask niko to use it. - added Skydome picture to media files (skydome2.jpg) half/sphere - OctTree -added #define OCTTREE_PARENTTEST ( default: disabled ) used to leave-out children test if the parent passed a complete frustum. plus: leaves out children test minus: all edges have to be checked - added MesBuffer Hardware Hint Vertex to octtree - CQuake3ShaderSceneNode: - removed function releaseMesh Shader doesn't copy the original mesh anymore ( saving memory ) so therefore this (for others often misleading ) function was removed - changed constructor to take a (shared) destination meshbuffer for rendering reducing vertex-memory to a half - don't copy the original vertices anymore - added deformvertexes autosprite - added deformvertexes move - added support for RTCW and Raven BSPs ( qmap2 ) - added polygonoffset (TODO: not perfect) - added added nomipmaps - added rgbgen const - added alphagen - added MesBuffer Hardware Hint Vertex/Index to Quake3: static geometry, dynamic indices - added Quake3Explorer examples - added wave noise - added tcmod transform - added whiteimage - added collision to Quake3Explorer - renamed SMD3QuaterionTag* to SMD3QuaternionTag* ( typo ) - updated quake3:blendfunc - added crouch to Quake3Explorer (modifying the ellipsiodRadius of the camera animator ) added crouch to CSceneNodeAnimatorCameraFPS still problems with stand up and collision - Quake3MapLoader modified memory allocation for faster loading - Quake3LoadParam added Parameter to the Mesh-Loader - added The still existing missing caulking of curved surfaces. using round in the coordinates doesn't solve the problem. but for the demo bsp mesh it solves the problem... (luck) so for now it's switchable. TJUNCTION_SOLVER_ROUND default:off - BurningVideo - pushed BurningsVideo to 0.40 - added blendfunc gl_one_minus_dst_alpha gl_one - added blendfunc gl_dst_color gl_zero - added blendfunc gl_dst_color src_alpha - modified AlphaChannel_Ref renderer to support alpha test lessequal - addded 32 Bit Index Buffer - added sourceRect/destRect check to 2D-Blitter ( slower, but resolves crash ) - added setTextureCreationFlag video::ETCF_ALLOW_NON_POWER_2 Burning checks this flag and when set, it bypasses the power2 size check, which is necessary on 3D but can be avoided on 2D. used on fonts automatically. - added Support for Destination Alpha - OpenGL - Fixed a bug in COpenGLExtensenionHandler where a glint was downcasted to u8!!!!!! MaxTextureSize=static_cast<u32>(num); - TODO: COpenGLMaterialRenderer_ONETEXTURE_BLEND to work as expected - Direct3D8 - compile and links again - added 32 Bit Index Buffer - D3DSAMP_MIPMAPLODBIAS doesnt compile!. it is d3d9 i think. - compile for XBOX - Direc3D9 - fixed crash on RTT Textures DepthBuffer freed twice. added deleteAllTextures to destuctor - NullDriver - removeallTextures. added setMaterial ( SMaterial() ) to clean pointers for freed textures git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2147 dfc29bdd-3216-0410-991c-e03cc46cb475
2009-01-27 07:53:53 -08:00
if (triangle.isPointInside(planeIntersectionPoint))
{
foundCollision = true;
t = t0;
collisionPoint = planeIntersectionPoint;
}
}
// if we havent found a collision already we will have to sweep
// the sphere against points and edges of the triangle. Note: A
// collision inside the triangle will always happen before a
// vertex or edge collision.
if (!foundCollision)
{
core::vector3df velocity = colData->velocity;
core::vector3df base = colData->basePoint;
f32 velocitySqaredLength = velocity.getLengthSQ();
f32 a,b,c;
f32 newT;
// for each edge or vertex a quadratic equation has to be solved:
// a*t^2 + b*t + c = 0. We calculate a,b, and c for each test.
// check against points
a = velocitySqaredLength;
// p1
b = 2.0f * (velocity.dotProduct(base - triangle.pointA));
c = (triangle.pointA-base).getLengthSQ() - 1.f;
if (getLowestRoot(a,b,c,t, &newT))
{
t = newT;
foundCollision = true;
collisionPoint = triangle.pointA;
}
// p2
if (!foundCollision)
{
b = 2.0f * (velocity.dotProduct(base - triangle.pointB));
c = (triangle.pointB-base).getLengthSQ() - 1.f;
if (getLowestRoot(a,b,c,t, &newT))
{
t = newT;
foundCollision = true;
collisionPoint = triangle.pointB;
}
}
// p3
if (!foundCollision)
{
b = 2.0f * (velocity.dotProduct(base - triangle.pointC));
c = (triangle.pointC-base).getLengthSQ() - 1.f;
if (getLowestRoot(a,b,c,t, &newT))
{
t = newT;
foundCollision = true;
collisionPoint = triangle.pointC;
}
}
// check against edges:
// p1 --- p2
core::vector3df edge = triangle.pointB - triangle.pointA;
core::vector3df baseToVertex = triangle.pointA - base;
f32 edgeSqaredLength = edge.getLengthSQ();
f32 edgeDotVelocity = edge.dotProduct(velocity);
f32 edgeDotBaseToVertex = edge.dotProduct(baseToVertex);
// calculate parameters for equation
a = edgeSqaredLength* -velocitySqaredLength +
edgeDotVelocity*edgeDotVelocity;
b = edgeSqaredLength* (2.f *velocity.dotProduct(baseToVertex)) -
2.0f*edgeDotVelocity*edgeDotBaseToVertex;
c = edgeSqaredLength* (1.f -baseToVertex.getLengthSQ()) +
edgeDotBaseToVertex*edgeDotBaseToVertex;
// does the swept sphere collide against infinite edge?
if (getLowestRoot(a,b,c,t,&newT))
{
f32 f = (edgeDotVelocity*newT - edgeDotBaseToVertex) / edgeSqaredLength;
if (f >=0.0f && f <= 1.0f)
{
// intersection took place within segment
t = newT;
foundCollision = true;
collisionPoint = triangle.pointA + (edge*f);
}
}
// p2 --- p3
edge = triangle.pointC-triangle.pointB;
baseToVertex = triangle.pointB - base;
edgeSqaredLength = edge.getLengthSQ();
edgeDotVelocity = edge.dotProduct(velocity);
edgeDotBaseToVertex = edge.dotProduct(baseToVertex);
// calculate parameters for equation
a = edgeSqaredLength* -velocitySqaredLength +
edgeDotVelocity*edgeDotVelocity;
b = edgeSqaredLength* (2*velocity.dotProduct(baseToVertex)) -
2.0f*edgeDotVelocity*edgeDotBaseToVertex;
c = edgeSqaredLength* (1-baseToVertex.getLengthSQ()) +
edgeDotBaseToVertex*edgeDotBaseToVertex;
// does the swept sphere collide against infinite edge?
if (getLowestRoot(a,b,c,t,&newT))
{
f32 f = (edgeDotVelocity*newT-edgeDotBaseToVertex) /
edgeSqaredLength;
if (f >=0.0f && f <= 1.0f)
{
// intersection took place within segment
t = newT;
foundCollision = true;
collisionPoint = triangle.pointB + (edge*f);
}
}
// p3 --- p1
edge = triangle.pointA-triangle.pointC;
baseToVertex = triangle.pointC - base;
edgeSqaredLength = edge.getLengthSQ();
edgeDotVelocity = edge.dotProduct(velocity);
edgeDotBaseToVertex = edge.dotProduct(baseToVertex);
// calculate parameters for equation
a = edgeSqaredLength* -velocitySqaredLength +
edgeDotVelocity*edgeDotVelocity;
b = edgeSqaredLength* (2*velocity.dotProduct(baseToVertex)) -
2.0f*edgeDotVelocity*edgeDotBaseToVertex;
c = edgeSqaredLength* (1-baseToVertex.getLengthSQ()) +
edgeDotBaseToVertex*edgeDotBaseToVertex;
// does the swept sphere collide against infinite edge?
if (getLowestRoot(a,b,c,t,&newT))
{
f32 f = (edgeDotVelocity*newT-edgeDotBaseToVertex) /
edgeSqaredLength;
if (f >=0.0f && f <= 1.0f)
{
// intersection took place within segment
t = newT;
foundCollision = true;
collisionPoint = triangle.pointC + (edge*f);
}
}
}// end no collision found
// set result:
if (foundCollision)
{
// distance to collision is t
f32 distToCollision = t*colData->velocity.getLength();
// does this triangle qualify for closest hit?
if (!colData->foundCollision ||
distToCollision < colData->nearestDistance)
{
colData->nearestDistance = distToCollision;
colData->intersectionPoint = collisionPoint;
colData->foundCollision = true;
colData->intersectionTriangle = triangle;
++colData->triangleHits;
return true;
}
}// end found collision
return false;
}
//! Collides a moving ellipsoid with a 3d world with gravity and returns
//! the resulting new position of the ellipsoid.
core::vector3df CSceneCollisionManager::collideEllipsoidWithWorld(
ITriangleSelector* selector, const core::vector3df &position,
const core::vector3df& radius, const core::vector3df& velocity,
f32 slidingSpeed,
const core::vector3df& gravity,
core::triangle3df& triout,
core::vector3df& hitPosition,
bool& outFalling,
ISceneNode*& outNode)
{
if (!selector || radius.X == 0.0f || radius.Y == 0.0f || radius.Z == 0.0f)
return position;
// This code is based on the paper "Improved Collision detection and Response"
// by Kasper Fauerby, but some parts are modified.
SCollisionData colData;
colData.R3Position = position;
colData.R3Velocity = velocity;
colData.eRadius = radius;
colData.nearestDistance = FLT_MAX;
colData.selector = selector;
colData.slidingSpeed = slidingSpeed;
colData.triangleHits = 0;
colData.triangleIndex = -1;
core::vector3df eSpacePosition = colData.R3Position / colData.eRadius;
core::vector3df eSpaceVelocity = colData.R3Velocity / colData.eRadius;
// iterate until we have our final position
core::vector3df finalPos = collideWithWorld(
0, colData, eSpacePosition, eSpaceVelocity);
outFalling = false;
// add gravity
if (gravity != core::vector3df(0,0,0))
{
colData.R3Position = finalPos * colData.eRadius;
colData.R3Velocity = gravity;
colData.triangleHits = 0;
eSpaceVelocity = gravity/colData.eRadius;
finalPos = collideWithWorld(0, colData,
finalPos, eSpaceVelocity);
outFalling = (colData.triangleHits == 0);
}
if (colData.triangleHits)
{
triout = colData.intersectionTriangle;
triout.pointA *= colData.eRadius;
triout.pointB *= colData.eRadius;
triout.pointC *= colData.eRadius;
outNode = selector->getSceneNodeForTriangle(colData.triangleIndex);
}
finalPos *= colData.eRadius;
hitPosition = colData.intersectionPoint * colData.eRadius;
return finalPos;
}
core::vector3df CSceneCollisionManager::collideWithWorld(s32 recursionDepth,
SCollisionData &colData, core::vector3df pos, core::vector3df vel)
{
f32 veryCloseDistance = colData.slidingSpeed;
if (recursionDepth > 5)
return pos;
colData.velocity = vel;
colData.normalizedVelocity = vel;
colData.normalizedVelocity.normalize();
colData.basePoint = pos;
colData.foundCollision = false;
colData.nearestDistance = FLT_MAX;
//------------------ collide with world
// get all triangles with which we might collide
core::aabbox3d<f32> box(colData.R3Position);
box.addInternalPoint(colData.R3Position + colData.R3Velocity);
box.MinEdge -= colData.eRadius;
box.MaxEdge += colData.eRadius;
s32 totalTriangleCnt = colData.selector->getTriangleCount();
Triangles.set_used(totalTriangleCnt);
core::matrix4 scaleMatrix;
scaleMatrix.setScale(
core::vector3df(1.0f / colData.eRadius.X,
1.0f / colData.eRadius.Y,
1.0f / colData.eRadius.Z));
s32 triangleCnt = 0;
colData.selector->getTriangles(Triangles.pointer(), totalTriangleCnt, triangleCnt, box, &scaleMatrix);
for (s32 i=0; i<triangleCnt; ++i)
if(testTriangleIntersection(&colData, Triangles[i]))
colData.triangleIndex = i;
//---------------- end collide with world
if (!colData.foundCollision)
return pos + vel;
// original destination point
const core::vector3df destinationPoint = pos + vel;
core::vector3df newBasePoint = pos;
// only update if we are not already very close
// and if so only move very close to intersection, not to the
// exact point
if (colData.nearestDistance >= veryCloseDistance)
{
core::vector3df v = vel;
v.setLength( colData.nearestDistance - veryCloseDistance );
newBasePoint = colData.basePoint + v;
v.normalize();
colData.intersectionPoint -= (v * veryCloseDistance);
}
// calculate sliding plane
const core::vector3df slidePlaneOrigin = colData.intersectionPoint;
const core::vector3df slidePlaneNormal = (newBasePoint - colData.intersectionPoint).normalize();
core::plane3d<f32> slidingPlane(slidePlaneOrigin, slidePlaneNormal);
core::vector3df newDestinationPoint =
destinationPoint -
(slidePlaneNormal * slidingPlane.getDistanceTo(destinationPoint));
// generate slide vector
const core::vector3df newVelocityVector = newDestinationPoint -
colData.intersectionPoint;
if (newVelocityVector.getLength() < veryCloseDistance)
return newBasePoint;
return collideWithWorld(recursionDepth+1, colData,
newBasePoint, newVelocityVector);
}
//! Returns a 3d ray which would go through the 2d screen coodinates.
core::line3d<f32> CSceneCollisionManager::getRayFromScreenCoordinates(
const core::position2d<s32> & pos, ICameraSceneNode* camera)
{
core::line3d<f32> ln(0,0,0,0,0,0);
if (!SceneManager)
return ln;
if (!camera)
camera = SceneManager->getActiveCamera();
if (!camera)
return ln;
const scene::SViewFrustum* f = camera->getViewFrustum();
core::vector3df farLeftUp = f->getFarLeftUp();
core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;
const core::rect<s32>& viewPort = Driver->getViewPort();
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())
ln.start = f->cameraPosition + (lefttoright * (dx-0.5f)) + (uptodown * (dy-0.5f));
else
ln.start = f->cameraPosition;
ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);
return ln;
}
//! Calculates 2d screen position from a 3d position.
core::position2d<s32> CSceneCollisionManager::getScreenCoordinatesFrom3DPosition(
const core::vector3df & pos3d, ICameraSceneNode* camera, bool useViewPort)
{
if (!SceneManager || !Driver)
return core::position2d<s32>(-1000,-1000);
if (!camera)
camera = SceneManager->getActiveCamera();
if (!camera)
return core::position2d<s32>(-1000,-1000);
core::dimension2d<u32> dim;
if (useViewPort)
dim.set(Driver->getViewPort().getWidth(), Driver->getViewPort().getHeight());
else
dim=(Driver->getScreenSize());
dim.Width /= 2;
dim.Height /= 2;
core::matrix4 trans = camera->getProjectionMatrix();
trans *= camera->getViewMatrix();
f32 transformedPos[4] = { pos3d.X, pos3d.Y, pos3d.Z, 1.0f };
trans.multiplyWith1x4Matrix(transformedPos);
if (transformedPos[3] < 0)
return core::position2d<s32>(-10000,-10000);
const f32 zDiv = transformedPos[3] == 0.0f ? 1.0f :
core::reciprocal(transformedPos[3]);
return core::position2d<s32>(
dim.Width + core::round32(dim.Width * (transformedPos[0] * zDiv)),
dim.Height - core::round32(dim.Height * (transformedPos[1] * zDiv)));
}
inline bool CSceneCollisionManager::getLowestRoot(f32 a, f32 b, f32 c, f32 maxR, f32* root)
{
// check if solution exists
f32 determinant = b*b - 4.0f*a*c;
// if determinant is negative, no solution
if (determinant < 0.0f || a == 0.f ) return false;
// calculate two roots: (if det==0 then x1==x2
// but lets disregard that slight optimization)
// burningwater: sqrt( 0) is an illegal operation.... smth should be done...
f32 sqrtD = (f32)sqrt(determinant);
f32 r1 = (-b - sqrtD) / (2*a);
f32 r2 = (-b + sqrtD) / (2*a);
// sort so x1 <= x2
if (r1 > r2) { f32 tmp=r2; r2=r1; r1=tmp; }
// get lowest root
if (r1 > 0 && r1 < maxR)
{
*root = r1;
return true;
}
// its possible that we want x2, this can happen if x1 < 0
if (r2 > 0 && r2 < maxR)
{
*root = r2;
return true;
}
return false;
}
} // end namespace scene
} // end namespace irr