Director Tutorials

 

Collision Detection

Using the Collision Modifier

This tutorial builds on the last. So, you will need your completed Director file. If you want to download the completed one, it is available here (3Denv_cameras.dir).

1. Open the movie and open the scene control behavior.

2.  Add the following to the end of the beginSprite handler.
createWall

3. Now we will define what the createWall does. As the name suggests, we will create a wall which will be used for collision detection with the character. Before we write the script, you will need to download the image brickmap.bmp, and import it into your movie. This will be used as a texture for the wall.

4. Add the following to the script.

on createWall

  -- create a box model resource for the wall
  wallRes = p3Dmember.newModelresource("wallRes",#box)
  wallRes.height = 10
  wallRes.length = 50
  wallRes.width = 150

  -- create a model from the model resource
  wallMod = p3Dmember.newModel("wallMod",wallRes)

  -- locate the wall in our scene
  wallMod.translate(-50,-20,25)

  -- create a texture for the wall, using the image brickMap
  wallTexture =
p3Dmember.newTexture \
("brick",#fromCastmember, member("brickMap"))

  -- assign the texture to the shader for the wall
  wallMod.shader.texture = wallTexture

  -- scale down the texture and tile the bricks along
  -- the wall
  wallMod.shader.textureTransform.scale  = \
vector
(0.2, 0.4, 1)

end

We have just scaled the image to a width of 1 fifth (0.2) of the original and the height to 2 fifths (0.4). By default, the textureRepeat property is set to TRUE so the tiling will occur. If the following statement is included, the texture will not tile:
wallModel.shader.textureRepeat = FALSE

There are other ways you can control texture mapping in Director including:
textureMode specifies how the texture is mapped onto the surface of the model
textureModeList property specifies textures for layers other than the first layer

4. You can now play the movie and move the character around. As you will see, the character can move through the wall. We will now set up the collision detection using the collision modifier.
Add the following to the end of the beginSprite handler (before the end statement).
createCollionDetect

5. Add the following to the script.

on createCollionDetect

  -- add collision modifier to the "wall"
  p3Dmember.model("wallMod").addModifier(#collision)

  -- set bounding geometry for collision detection to
  -- bounding box of model
  p3Dmember.model("wallMod").collision.mode = #box

  -- resolve collision for wall
  p3Dmember.model("wallMod").collision.resolve = TRUE

  -- add collision modifier to the character
  pCharacter.addmodifier(#collision)

  -- set bounding geometry for collision detection to
  -- bounding box of model
  pCharacter.collision.mode = #box

   -- resolve collision for the character
  pCharacter.collision.resolve = TRUE

end

By attaching the collision modifier to objects in a 3D world, we can detect collisions with between all the objects to which the modifier has been applied. Once the collision modifier has been attached to a model, the following collision modifier properties are accessible:
enabled (collision) allows you to turn off the collision detection (by setting the value to FALSE) while the modifier is applied. The default value is TRUE.
resolve indicates whether collisions models stop when they collide with each other. If the resolve is set to FALSE, then that model will continue to move. The default value is TRUE. In our example, we could have left out the resolve statements. Try change the character resolve to FALSE and see what happens.
immovable indicates whether a model can be moved as a result of collisions. This default value is FALSE.
mode (collision) indicates the geometry used for collision detection and could be:
#mesh uses the actual mesh geometry of the model’s resource.It is more precise but usually slower than #box or #sphere.
#box uses the bounding box of the model. It is useful for objects that can fit more tightly in a box than in a sphere, such as our wall.
#sphere is the fastest mode, because it uses the bounding sphere of the model. It is the default value for this property.

7.  Play the movie and test what you just created.
You can download the completed movie from here.

The use of the collision modifier is useful for relatively simple applications only. It can be very processor intensive, especially if there are complex models and a large number of modifiers attached to objects. The recommended technique for collision detection of more complex models is to use the modelsUnderRay command. You can see that in the next tutorial.