proc float normalizeAngle(float $angle) { float $returnVal; if($angle > 360.0) { $returnVal = $angle - 360.0; return $returnVal; } else if($angle < -360.0) { $returnVal = $angle + 360.0; return $returnVal; } else { return $angle; } } global proc moveInDirection(string $objName, float $dirx1, float $dirz1, float $v1, float $dirx2, float $dirz2, float $v2) { float $maxSpeed = 0.125; float $maxTurn = 12.0; /* string $selectedguys[] = `ls -sl`; if($selectedguys[0] == $objName) { print("flocking influence = " + $dirx1 + "," + $dirz1 + " velocity = " + $v1 + "\n"); print("obstacle influence = " + $dirx2 + "," + $dirz2 + " velocity = " + $v2 + "\n"); } */ int $ObstacleInfluence = 1; if($v2 < 0.0) { $ObstacleInfluence = 0; } float $dotted2 = $dirx1*$dirx2 + $dirz1 * $dirz2; float $dotted = 1-$dotted2; if($ObstacleInfluence) { $dirx1 = $dirx1 * $dotted; $dirz1 = $dirz1 * $dotted; } $dirx2 = $dirx2 * $dotted2; $dirz2 = $dirz2 * $dotted2; float $rotateY = `getAttr ($objName + ".rotateY")`; float $velocity = `getAttr ($objName + ".Velocity")`; float $vectx, $vectz,$vls; float $tx,$tz; $tx = sind($rotateY);//* $velocity; $tz = cosd($rotateY);//* $velocity; if($ObstacleInfluence) { $vectx = $dirx1 + $dirx2; $vectz = $dirz1 + $dirz2; $vls = ($v1 + $v2) / 2.0; } else { $vectx = $dirx1; $vectz = $dirz1; $vls = $v1; } if($v2 < 0.1 && $ObstacleInfluence) { $vls = $v2; } $originalDirectionX = $tx; $originalDirectionZ = $tz; $tx += $vectx; $tz += $vectz; $size = sqrt($tx*$tx + $tz*$tz); if($size > 0) { $tx /= $size; $tz /= $size; } else { $tx /= $size; $tz /= $size; } float $weightFactor = (1.0 / $maxSpeed); float $velocityWeight = $velocity * $weightFactor; if($velocityWeight < 0.05) { $velocityWeight = 0.05; } $deltaY = (atan2d($vectx, $vectz)); $selectedguys = `ls -sl`; $deltaY *= $velocityWeight; if($deltaY > 15) { $deltaY = 15; } if($deltaY < -15) { $deltaY = -15; } float $crossProduct[3]; $crossProduct = crossProduct({$originalDirectionX,0,$originalDirectionZ},{$vectx,0,$vectz},0,1); $dotProduct = ($originalDirectionX * $vectx) + ($originalDirectionZ * $vectz); float $turnRight = $crossProduct[1]; $deltaY = $maxTurn * $turnRight * (1.0 - $dotProduct); rotate -r 0 $deltaY 0 $objName; setAttr ($objName + ".Velocity") ($vls); move -r ($tx * $vls) 0 ($tz * $vls) $objName; }