// // Copyright (C) Jedrik D. Eliasen // // File: rigidbody_pluginCmd.cpp #pragma once #include "stdio.h" #include #include #include #include #include "Vec3f.h" #include "Sphere.h" #include "Collision.h" #include "math.h" //Create a maya command called 'collision_occurance' that //will call the doIt function DeclareSimpleCommand( collision_occurance, "Jedrik D. Eliasen", "7.0"); //this function is what Maya will process MStatus collision_occurance::doIt( const MArgList& args ) { MStatus stat = MS::kSuccess; //set x,y,z for simple array accessing int x = 0; int y = 1; int z = 2; //set the paramater variables double radius1, radius2; double translationS1[3]; double translationS2[3]; unsigned index; //set the 1st sphere's position x coord to translationS1[x] index = args.flagIndex( "tx1", "translateS1X"); args.get(index + 1, translationS1[x]); //set the 1st sphere's position y coord to translationS1[y] index = args.flagIndex( "ty1", "translateS1Y"); args.get(index + 1, translationS1[y]); //set the 1st sphere's position z coord to translationS1[z] index = args.flagIndex( "tz1", "translateS1Z"); args.get(index + 1, translationS1[z]); //set the 1st sphere's radius to radius1 index = args.flagIndex( "r1", "radius1"); args.get(index + 1, radius1); //set the 2nd sphere's position x coord to translationS2[x] index = args.flagIndex( "tx2", "translateS2X"); args.get(index + 1, translationS2[x]); //set the 2nd sphere's position y coord to translationS2[y] index = args.flagIndex( "ty2", "translateS2Y"); args.get(index + 1, translationS2[y]); //set the 2nd sphere's position z coord to translationS2[z] index = args.flagIndex( "tz2", "translateS2Z"); args.get(index + 1, translationS2[z]); //set the 2nd sphere's radius to radius2 index = args.flagIndex( "r2", "radius2"); args.get(index + 1, radius2); //set the centers of the spheres to two vectors Vec3f vec1(translationS1[x],translationS1[y],translationS1[z]); Vec3f vec2(translationS2[x],translationS2[y],translationS2[z]); //create two sphere's using the vectors just created and the radii Sphere s1(vec1, radius1); Sphere s2(vec2, radius2); //////////////////////////////////////////////////////////////////////////////// Collision_State result = NONE; Collision g; float Dist = 0.0f; Vec3f distVec(0.0f,0.0f,0.0f); Collision_State State = NONE; //distVec = s1.COM_Position - s2.COM_Position; distVec = s1.Center - s2.Center; Dist = distVec.GetMag() - s1.Radius - s2.Radius; //Check to see if it is reasonably close to zero if ( ( Dist < 0.1f ) && ( Dist > -0.1f ) ) { State = TOUCHING; } else if ( Dist < -0.1f ) { State = OVERLAPPING; } result = State; //////////////////////////////////////////////////////////////////////////////// int result_to_pass_back; //we can't pass back an enumeration to MEL, so we must convert //the results to ints and use those ints in MEL if(result == NONE) { result_to_pass_back = 0; } else if(result == OVERLAPPING) { result_to_pass_back = 2; } else if(result == TOUCHING) { result_to_pass_back = 1; } //'return' result_to_pass_back to MEL clearResult(); setResult( result_to_pass_back ); //this will return to MEL a true or false depending on whether the function //was successful or not return stat; }