//////////////////////// // Jolex Del Pilar // // 3D Scripting // // Rigid Body Solver // //////////////////////// //Give Direction and Speed //////////////////////// // Create Window GUI // ////////////////////// string $window = `window -title "Bouncing Ball Sim" -iconName "Boucing Ball" -widthHeight 300 400`; columnLayout -adjustableColumn true; button -label "Create Arena" -c "CreateArena();"; button -label "Create Ball" -c "CreateBall();"; button -label "Delete Ball" -c "DeleteBall();"; button -label "Begin Simulation" -c "playButtonForward"; text -label "Direction"; floatField VectorXfield; floatField VectorYfield; floatField VectorZfield; text -label "Velocity"; floatField VelocityField; text -label "Drag (1%-100%)"; floatField DragField; text -label "Gravity (Meters Squared)"; floatField GravField; setParent ..; showWindow $window; /////////////////// // Create Arena // ///////////////// global proc CreateArena() { //Create Cube Arena and reverse normals, removes double-sided attr polyCube -n CubeArena -w 20 -h 20 -d 20 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -tx 1 -ch 1; polyNormal -nm 0 CubeArena; setAttr CubeArena.doubleSided 0; if(`objExists Ball_Exp`) { confirmDialog -title "Create Arena" -message "Expression implemented!"; } else { //Create Expression expression -n "Ball_Exp" -s "moveObj(\"bouncingball\");" -o "" -ae 1 -uc all ; } } ///////////////////// // Global Counter // /////////////////// //int $beginCounter = 0; ////////////////// // Create Ball // //////////////// global proc CreateBall() { if(`objExists bouncingball`) { confirmDialog -title "Create Arena" -message "Bouncing Ball already exist!"; } else { //Create Ball and turn into "active rigid body" polySphere -n "bouncingball" -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1; addAttr -ln "Vector" -multi bouncingball; addAttr -ln "Velocity" bouncingball; addAttr -ln "Drag" bouncingball; addAttr -ln "Gravity" bouncingball; float $VectorX = `floatField -q -value VectorXfield`; float $VectorY = `floatField -q -value VectorYfield`; float $VectorZ = `floatField -q -value VectorZfield`; float $VelocityBall = `floatField -q -value VelocityField`; float $DragBall = `floatField -q -value DragField`; float $GravBall = `floatField -q -value GravField`; print $VectorX; print $VectorY; print $VectorZ; //int $randLocationX; //$randLocation = `rand -5 5`; //int $randLocationY; //$randLocation = `rand -5 5`; //int $randLocationZ; //$randLocation = `rand -5 5`; //setAttr bouncingball.translateX $randLocationX; //setAttr bouncingball.translateY $randLocationY; //setAttr bouncingball.translateZ $randLocationZ; setAttr bouncingball.Vector[0] $VectorX; setAttr bouncingball.Vector[1] $VectorY; setAttr bouncingball.Vector[2] $VectorZ; setAttr bouncingball.Velocity $VelocityBall; setAttr bouncingball.Drag $DragBall; setAttr bouncingball.Gravity $GravBall; } } /////////////////// // Delete Ball // ///////////////// global proc DeleteBall() { if (`objExists bouncingball`) { select -r bouncingball ; doDelete; } else { confirmDialog -title "Create Arena" -message "Bouncing Ball does not exist!"; } } /////////////////// // Collision // ///////////////// global proc collisionObj(string $obj_ball, float $x, float $y, float $z) { float $xX = `getAttr($obj_ball + ".translateX")`; float $xY = `getAttr($obj_ball + ".translateY")`; float $xZ = `getAttr($obj_ball + ".translateZ")`; int $switch = 0; if($xX > 9.5) { setAttr ($obj_ball + ".translateX") 9.5; $switch = 1; } else if($xX < -9.5) { setAttr ($obj_ball + ".translateX") -9.5; $switch = 2; } else if($xY > 9.5) { setAttr ($obj_ball + ".translateY") 9.5; $switch = 1; } else if($xY < -9.5) { setAttr ($obj_ball + ".translateY") -9.5; $switch = 2; } else if($xZ > 9.5) { setAttr ($obj_ball + ".translateZ") 9.5; $switch = 1; } else if($xZ < -9.5) { setAttr ($obj_ball + ".translateZ") -9.5; $switch = 2; } if($switch==1) { setAttr ($obj_ball+ ".Vector[0]") `rand -1 0`; setAttr ($obj_ball+ ".Vector[1]") `rand -1 0`; setAttr ($obj_ball+ ".Vector[2]") `rand -1 0`; } else if($switch==2) { setAttr ($obj_ball+ ".Vector[0]") `rand 1`; setAttr ($obj_ball+ ".Vector[1]") `rand 1`; setAttr ($obj_ball+ ".Vector[2]") `rand 1`; } } ////////////////// // Move Object // //////////////// global proc moveObj(string $obj) { float $tempDir[] = `getAttr ($obj + ".Vector")`; float $tempVel = `getAttr ($obj + ".Velocity")`; float $tempDrag = `getAttr ($obj + ".Drag")`; float $tempGrav = `getAttr ($obj + ".Gravity")`; print "retrieved values:\n"; print ("Vector: " + $tempDir[0]+ ", " + $tempDir[1]+ ", " + $tempDir[2]+ "\n"); print ("velocity: " + $tempVel + "\n"); float $x = $tempDir[0]; float $y = $tempDir[1]; float $z = $tempDir[2]; float $dirMag = getMag($x,$y,$z); print ("direction magnitude: " + $dirMag + "\n"); //create unit vector float $unitVec[]; $unitVec[0] = $tempDir[0] * (1/$dirMag); $unitVec[1] = $tempDir[1] * (1/$dirMag); $unitVec[2] = $tempDir[2] * (1/$dirMag); //apply velocity $unitVec[0] = $unitVec[0] * $tempVel; $unitVec[1] = $unitVec[1] * $tempVel-pow(($tempGrav*.1), 2); $unitVec[2] = $unitVec[2] * $tempVel; //apply Drag setAttr "bouncingball.Velocity" ($tempVel*(1-($tempDrag*.01))); collisionObj($obj, $unitVec[0], $unitVec[1], $unitVec[2]); //move target relative to its position move -r $unitVec[0] $unitVec[1] $unitVec[2] $obj; } ///////////////////////////////////// //get the magnitude of the vector // /////////////////////////////////// global proc float getMag(float $x, float$y, float$z){ float $var = 0; $var = pow($x,2) + pow($y,2) + pow($z,2); $var = sqrt($var); return $var; }