Collision Detection in Maya

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.
[text collapse=”True”]
// 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;
[/text]

The second expression affects a shader, based on the collision and velocity values collected by the expression above.
[text collapse=”True”]
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;
[/text]

2 comments

  1. Alejandro Mejía

    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;

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>