Path: chuka.playstation.co.uk!news1.scei.co.jp!scea!greg_labrec@interactive.sony.com From: Nick Porcino Newsgroups: scea.yaroze.programming.3d_graphics Subject: A wacky trick for normalization Date: Wed, 27 Aug 1997 22:14:47 -0800 Organization: SCEA News Server Lines: 39 Message-ID: <34051754.4A49@bc.sympatico.ca> NNTP-Posting-Host: vcta01m03-148.bctel.ca Mime-Version: 1.0 Content-Type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.01-C-SYMPA (Macintosh; I; PPC) Normalization of a vector normally looks like - mag = pow(x*x + y*y , 0.5) x /= mag; y /= mag; However, if you're trying to do everything in integer math, it gets ugly because n*n can overflow, and / and sqrt are somewhat slow. I came up with a wacky trick in 2D that will actually allow normalization of a vector in 2D using table lookups (thanks to however wrote the lovely atan functions in the 3d demos): theta = atan2(x, y); x = cos(theta); y = sin(theta); presto, chango, no loss of precision, no integer overflow, supremely fast integer based vector normalization. My question - can anyone think of a way to extend this idea to 3D? My solution looks like theta = atan2(x, y); x1 = cos(theta); y1 = sin(theta); rotate vector around z axis by theta degrees phi = atan2(x, z); z = sin(theta); x = x1 * cos(theta); y = y1 * sin(theta); (That's probably all messed up BTW, but you get the idea). The problem is that this version with its 3D rotation is gonna be slower than the pow(n, 0.5) version, methinks. Anyone got any insights? - nick