Director 3D > Collision Detection

The Collision Modifier

To enable the models in your 3D cast member to detect contact with each other, you must set up some special model properties. By adding the collision modifier to each model's modifier list and defining a handler to be called when a collision occurs, you can make your models react to collisions in appropriate ways.

The collision modifier provides a basic level of collision detection for the models in your environment.

More information about this can be found at Macromedia’s tutorial site and in these excellent tutorials

Here is a sample of the collision modifier in use (click "Create Stuff" to create two models and "Move Stuff" to move them towards each other. Watch what happens when they collide):

This is the script that sets up the collision modifier:

The script starts with several commands that create the spheres:

sp=w.newModelResource("ball", #sphere)
ball1=w.newModel("ball1", sp)
ball2=w.newModel("ball2", sp)


The resetWorld() command clears the 3D cast member to its originally saved state, which in this case is a world with lights but no models. A new #sphere model resource is added to the world with the newModelResource() command. The script gives the sphere a radius of 1 by setting the radius property.

The model resource that was created is then used to make two sphere models appear in the world with the newModel() command. The translate command is used to position them opposite each other.

Next, the collision modifier is added to each of the sphere models with the addModlifier() command. The enabled and resolve properties of the collision modifier must also be set to TRUE .


Finally, a Lingo handler must be assigned to each model so that the handler can be called when collisions occur. The setCollisionCallback() command takes two arguments. The first is the name of the Lingo handler to be called; this name is always represented as a symbol. The second is the name of the script object that contains the handler. This can be the name of a script cast member, a variable containing a script object reference, or me if the handler is in the same script as the setCollisionCallback() command.

ball1.collision.setCollisionCallBack(#collision, me)
ball2.collision.setCollisionCallBack(#collision, me)

In your own movies, the collision callback will usually include some code that makes the models bounce off of one another, crumble from the impact, or something similar. In this case we just call a simple alert box.

The collision callback handler contains the Lingo you want to execute when collisions occur. At the moment of the collision, the handler is called and a property list containing information about the details of the collision is passed to the handler.

In the example movie, the callback handler is also in the Set Up Collision script. The handler includes an argument called colData , which will receive the property list of collision details. The handler plays a beep sound, then assigns the colData argument's information to the list called datList . It then calls a handler in the Collision Update script called collisionNotification and passes it the collision information now contained in the datList variable.

on collision me, colData

datlist=[coldata.modela, coldata.modelb, coldata.pointOfContact, coldata.collisionNormal]
sendAllSprites (#collisionNotification, datlist)
if pflag = true then
alert "The models have collided!"
end if


Note: The collision callback must not use the updateStage command.

The collisionNotification handler simply sets the text of the field sprite on the Stage to the string representing the datList variable, which is passed into it in the collisionData argument.

on collisionNotification me, collisionData
sprite(me.spritenum).member.text = string(collisionData)

It is important that the models you use for collisions do not include intersecting geometry—that is, two models that overlap each other. However, models that touch and are grouped will handle collisions cleanly.

< Previous | Next >

July 2003