Fixed div by zero bug found by TomiZ.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@1005 dfc29bdd-3216-0410-991c-e03cc46cb475
master
hybrid 2007-09-27 15:42:35 +00:00
parent 738951ff4b
commit d7ac9ad1fd
2 changed files with 20 additions and 36 deletions

View File

@ -11,7 +11,7 @@ namespace scene
//! constructor
CSceneNodeAnimatorFollowSpline::CSceneNodeAnimatorFollowSpline(u32 time,
const core::array< core::vector3df >& points, f32 speed,
const core::array<core::vector3df>& points, f32 speed,
f32 tightness)
: Points(points), Speed(speed), Tightness(tightness), StartTime(time)
{
@ -22,13 +22,6 @@ CSceneNodeAnimatorFollowSpline::CSceneNodeAnimatorFollowSpline(u32 time,
//! destructor
CSceneNodeAnimatorFollowSpline::~CSceneNodeAnimatorFollowSpline()
{
}
inline s32 CSceneNodeAnimatorFollowSpline::clamp(s32 idx, s32 size)
{
return ( idx<0 ? size+idx : ( idx>=size ? idx-size : idx ) );
@ -38,36 +31,32 @@ inline s32 CSceneNodeAnimatorFollowSpline::clamp(s32 idx, s32 size)
//! animates a scene node
void CSceneNodeAnimatorFollowSpline::animateNode(ISceneNode* node, u32 timeMs)
{
core::vector3df p, p0, p1, p2, p3;
core::vector3df t1, t2;
const u32 pSize = Points.size();
if (pSize==0)
return;
const f32 dt = ( (timeMs-StartTime) * Speed * 0.001f );
const f32 u = core::fract ( dt );
s32 idx = core::floor32( dt ) % pSize;
//f32 u = 0.001f * fmodf( dt, 1000.0f );
const s32 idx = core::floor32( dt ) % pSize;
//const f32 u = 0.001f * fmodf( dt, 1000.0f );
p0 = Points[ clamp( idx - 1, pSize ) ];
p1 = Points[ clamp( idx + 0, pSize ) ];
p2 = Points[ clamp( idx + 1, pSize ) ];
p3 = Points[ clamp( idx + 2, pSize ) ];
const core::vector3df& p0 = Points[ clamp( idx - 1, pSize ) ];
const core::vector3df& p1 = Points[ clamp( idx + 0, pSize ) ];
const core::vector3df& p2 = Points[ clamp( idx + 1, pSize ) ];
const core::vector3df& p3 = Points[ clamp( idx + 2, pSize ) ];
// hermite polynomials
f32 h1 = 2.0f * u * u * u - 3.0f * u * u + 1.0f;
f32 h2 = -2.0f * u * u * u + 3.0f * u * u;
f32 h3 = u * u * u - 2.0f * u * u + u;
f32 h4 = u * u * u - u * u;
const f32 h1 = 2.0f * u * u * u - 3.0f * u * u + 1.0f;
const f32 h2 = -2.0f * u * u * u + 3.0f * u * u;
const f32 h3 = u * u * u - 2.0f * u * u + u;
const f32 h4 = u * u * u - u * u;
// tangents
t1 = ( p2 - p0 ) * Tightness;
t2 = ( p3 - p1 ) * Tightness;
const core::vector3df t1 = ( p2 - p0 ) * Tightness;
const core::vector3df t2 = ( p3 - p1 ) * Tightness;
// interpolated point
p = p1 * h1 + p2 * h2 + t1 * h3 + t2 * h4;
node->setPosition(p);
node->setPosition(p1 * h1 + p2 * h2 + t1 * h3 + t2 * h4);
}
@ -77,7 +66,7 @@ void CSceneNodeAnimatorFollowSpline::serializeAttributes(io::IAttributes* out, i
out->addFloat("Speed", Speed);
out->addFloat("Tightness", Tightness);
u32 count = Points.size();
const u32 count = Points.size();
if ( options && (options->Flags & io::EARWF_FOR_EDITOR))
{
@ -95,6 +84,7 @@ void CSceneNodeAnimatorFollowSpline::serializeAttributes(io::IAttributes* out, i
}
}
//! Reads attributes of the scene node animator.
void CSceneNodeAnimatorFollowSpline::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
@ -105,13 +95,10 @@ void CSceneNodeAnimatorFollowSpline::deserializeAttributes(io::IAttributes* in,
for(u32 i=1; true; ++i)
{
core::stringc pname = "Point";
pname += (int)i;
pname += i;
if (in->existsAttribute(pname.c_str()))
{
core::vector3df pos = in->getAttributeAsVector3d(pname.c_str());
Points.push_back(pos);
}
Points.push_back(in->getAttributeAsVector3d(pname.c_str()));
else
break;
}

View File

@ -23,9 +23,6 @@ namespace scene
const core::array< core::vector3df >& points,
f32 speed = 1.0f, f32 tightness = 0.5f);
//! destructor
virtual ~CSceneNodeAnimatorFollowSpline();
//! animates a scene node
virtual void animateNode(ISceneNode* node, u32 timeMs);