GetThreeClosestObjsFromArray

From scripting
Revision as of 06:01, 22 April 2017 by Nickpisca (talk | contribs) (Created page with " global proc string[] GetThreeClosestObjsFromArray(string $MainObj, string $OtherArr[] ) { //Author Nick Pisca 0001d 2009 string $ClosestArr[2]; float $ClosestDis...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
global proc string[] GetThreeClosestObjsFromArray(string $MainObj, string $OtherArr[] ) {
       //Author Nick Pisca 0001d 2009 
	string $ClosestArr[2];
	float $ClosestDistArr[2];
	float $LocArr[] = `getAttr($MainObj+".translate")`;
	vector $LocVec = <<$LocArr[0], $LocArr[1], $LocArr[2]>>;
	$ClosestDistArr = {999999999, 999999999, 999999999};
	for ($xx=0;$xx<size($OtherArr);$xx++) {
		float $OppArr[] = `getAttr($OtherArr[$xx]+".translate")`;
		vector $OppVec = <<$OppArr[0], $OppArr[1], $OppArr[2]>>;
		vector $Diff = $LocVec - $OppVec;
		float $DiffDist = mag($Diff);	
		int $BiggestIndex = GetBiggestFloatIndex($ClosestDistArr);	
		
		if ($DiffDist < $ClosestDistArr[$BiggestIndex] && $DiffDist != 0) {
			$ClosestDistArr[$BiggestIndex] = $DiffDist;
			$ClosestArr[$BiggestIndex] = $OtherArr[$xx];
		}
	}
	return $ClosestArr;
}



More information on vector calculations and retrieving location attributes, read pages 14-16, 20, 90-101 in YSYT.