aabbox3d intersectsWithLine bug

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Rv
Posts: 10
Joined: Fri Mar 31, 2006 9:38 am

aabbox3d intersectsWithLine bug

Post by Rv »

Appologies if this has already been posted:

There is a bug in interectsWithLine(). The algorithm used is supposed to use the extents from the center of the box outwards, but is in fact using the width/height/depth of the box, so the results are out by a factor of two.

Quick fix:

Code: Select all

bool intersectsWithLine(const vector3d<T>& linemiddle, 
		const vector3d<T>& linevect,
		T halflength) const
{
//.	const vector3d<T> e = getExtent();
	//. Rv: Looks like a bug in the engine here: the algorithm needs
	//. _half_ the extent that the engine provides...
	vector3d<T> e = getExtent() * 0.5f;
	const vector3d<T> t = getCenter() - linemiddle;
	float r;

	if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) || 
		(fabs(t.Y) > e.Y + halflength * fabs(linevect.Y)) ||
		(fabs(t.Z) > e.Z + halflength * fabs(linevect.Z)) )
		return false;

	r = e.Y * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.Y);
	if (fabs(t.Y*linevect.Z - t.Z*linevect.Y) > r )
		return false;

	r = e.X * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.X);
	if (fabs(t.Z*linevect.X - t.X*linevect.Z) > r )
		return false;

	r = e.X * (T)fabs(linevect.Y) + e.Y * (T)fabs(linevect.X);
	if (fabs(t.X*linevect.Y - t.Y*linevect.X) > r)
		return false;

	return true;
}
[/color]
mdc

a fix to the Intersects with line bug

Post by mdc »

Yes, this was working in Irrlicht 0.6, but I recently ported a little app to 0.14 and suddenly my node mouse picking was out (by a factor of two)...

To fix it, you can recompile the source with a modification to class aabbox3d (found in source/irrlicht/include/aabbox3d.h). You need to change the function:

bool intersectsWithLine(const vector3d<T>& linemiddle,
const vector3d<T>& linevect,
T halflength) const


...by adding the following bit in bold to the first line of code:

const vector3d<T> e = getExtent() * (T)0.5;

The final modified function will read:

Code: Select all

		//! Tests if the box intersects with a line
		//! \return Returns true if there is an intersection and false if not.
		bool intersectsWithLine(const vector3d<T>& linemiddle, 
								const vector3d<T>& linevect,
								T halflength) const
		{
			const vector3d<T> e = getExtent() * (T)0.5;
			const vector3d<T> t = getCenter() - linemiddle;
			float r;

			if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) || 
				(fabs(t.Y) > e.Y + halflength * fabs(linevect.Y)) ||
				(fabs(t.Z) > e.Z + halflength * fabs(linevect.Z)) )
				return false;

			r = e.Y * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.Y);
			if (fabs(t.Y*linevect.Z - t.Z*linevect.Y) > r )
				return false;

			r = e.X * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.X);
			if (fabs(t.Z*linevect.X - t.X*linevect.Z) > r )
				return false;

			r = e.X * (T)fabs(linevect.Y) + e.Y * (T)fabs(linevect.X);
			if (fabs(t.X*linevect.Y - t.Y*linevect.X) > r)
				return false;

			return true;
		}
Now all your object picking will be pixel perfect to each node's bounding box. Like it used to be in version 0.6.

Hope this helps someone... I spent ages diff'ing files from the 0.6 source and the 0.14 source to find the bug!

...And great engine by the way.
hybrid

Post by hybrid »

Maybe you should check for known bugs in the engine first. There had been a number of bugs found directly after the release. Bug fixes were available immediately. Check the wiki and the patch page.
Post Reply