[fix] vectors normalization

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
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

[fix] vectors normalization

Post by warui »

Here's Yet Another Bug Fix by Warui :)

When you try to normalize [0,0,0] vector you will get weird result because of division by zero. That can cause for example node disappearing when fly straight animator from potin A to point A is added to it.

Here's fixed normalize() methods for vector2d and vector3d classes.

Code: Select all

// vector2d.h
	vector2d<T>& normalize()
	{
		T inv;
  	if(getLength() != 0.0) inv = (T)(1.0 / getLength());
		else inv = (T)0.0;
		X *= inv;
		Y *= inv;
		return *this;
	}


// vector3d.h
	vector3d<T>& normalize()
	{
		T inv;
		if(getLength() != 0.0) inv = (T)(1.0 / getLength());
		else inv = (T)0.0;
		X *= inv;
		Y *= inv;
		Z *= inv;
		return *this;
	}
Tomasz Nowakowski
Openoko - www.openoko.pl
calimero
Posts: 45
Joined: Sun Aug 08, 2004 4:31 pm
Location: Nice, France

Post by calimero »

hello warui,

nice to correct this but i think you can improve your modification because
when you do :

Code: Select all

if(getLength() != 0.0) inv = (T)(1.0 / getLength());


you make 2 calls to getLength and so you multiply the cost (in time) of the function (and sqrt cost a lot !!!). IMHO it will be be better to have something like that :

Code: Select all

f64 length = getLength();
if (length!=0.0) inv = (T)(1.0)/(T)length;
Post Reply