GetNClosestObjsFromArray

From scripting
Revision as of 05:58, 22 April 2017 by Nickpisca (talk | contribs) (Created page with " global proc string[] GetNClosestObjsFromArray(string $MainObj, string $OtherArr[], int $NCount) { //Author Nick Pisca 0001d 2009 string $ClosestArr[]; float $Clos...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
global proc string[] GetNClosestObjsFromArray(string $MainObj, string $OtherArr[], int $NCount) {
	//Author Nick Pisca 0001d 2009
       string $ClosestArr[];
	float $ClosestDistArr[];
	clear($ClosestDistArr);
	float $LocArr[] = `getAttr($MainObj+".translate")`;
	vector $LocVec = <<$LocArr[0], $LocArr[1], $LocArr[2]>>;
	for($e=0;$e<$NCount;$e++) {
		$ClosestDistArr[$e] = 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.