Collision Detection in Maya

Posted by on Oct 13, 2010 in code, Maya, MEL | 2 Comments

Based on this tutorial. You will need to download and source this script.

I added attributes to the cubes: .col1(int), .theCol_u(int), and .vel(float) to watch for collisions, “unique” collisions (see the tutorial), and the velocity of the cube, respectively.

Two expressions are created for each cube, each rigidBody. The first is created to watch the dynamic rigid body collisions, and the velocity of the rigid body.

// variables
string $myRigidBody = "rigidBody1";
int $contactCount;
int $uniqueContactCount;

//check velocity
vector $theVelocity1 = `getAttr rigidBody1.velocity`;
float $velCheck1 = ($theVelocity1.x + $theVelocity1.y + $theVelocity1.z);
float $absVel = `abs $velCheck1`;

pCube1.vel = $absVel;

// get $contactCount
$contactCount = `getAttr($myRigidBody+".contactCount")`;

// compute theCol_u
if ($contactCount > 0)
{
  string $array[] = `rigidBody -q -contactName $myRigidBody`;
  string $flatArray[] = flattenStringArray($array);
  $uniqueContactCount = size($flatArray);
}
else
{
  $uniqueContactCount = 0;
}

// set interface attributes
pCube1.col1 = $contactCount;
pCube1.theCol_u = $uniqueContactCount;

The second expression affects a shader, based on the collision and velocity values collected by the expression above.

int $theTime = `currentTime -query`;
if ($theTime > 0)
	if (myLam1.incandescenceR >.05)
		myLam1.incandescenceR = myLam1.incandescenceR -.075;
	else
		if (pCube1.vel > 0.2)
			if ( pCube1.theCol_u >=1)
				myLam1.incandescenceR = 2.5;
		else
			myLam1.incandescenceR = 0;

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

2 Comments

  1. Alejandro Mejía
    April 20, 2012

    Maya says that: Attribute not found or variable missing ‘$’: Lambert.incandescenceR

    in the following script you provided, please help me with the missing variable.

    int $theTime = `currentTime -query`;
    if ($theTime > 0)
    if (myLam1.incandescenceR >.05)
    myLam1.incandescenceR = myLam1.incandescenceR -.075;
    else
    if (pCube1.vel > 0.2)
    if ( pCube1.theCol_u >=1)
    myLam1.incandescenceR = 2.5;
    else
    myLam1.incandescenceR = 0;

  2. Oliver
    April 20, 2012

    I think you need to have a lambert called myLam1 for this to work. It’s been a while. Let me know if that helps.

Leave a Reply