Director Tutorials

 

Terrain Following

Moving a model over a contoured surface

This tutorial builds on the last. However, It also uses a different environment, You will need to download terrain.dir which includes the code from the collision 2 tutorial.

1. Open the terrain.dir movie, and open the scene control behavior. Add the following to the end of the characterMove handler:

-- move along terrain
-- create reference for terrain (ground)
terrain = p3Dmember.model("Terrain")

-- store character's position
charPos = pCharacter.worldPosition

-- cast a ray down
collisData = \
p3Dmember.modelsUnderRay(charPos,vector(0,0,-1),#detailed)

-- if model is picked up on ray
if collisData.count then

-- store total no of models detected by the ray
totalCount = collisData.count

repeat with modelNo = 1 to totalCount

-- check if over the terrain model
if (collisData[modelNo].model = terrain) then

terrainPos = collisData[modelNo].isectPosition

-- find out the distance the character should move
-- up or down
diff = (terrainPos.z - \
pCharacter.worldPosition.z) + 45

-- move the character
pCharacter.translate(0,0,diff,#world)

end if

end repeat

end if

In the above, we used the modelsUnderRay command to send a ray down and check the distance between the character and the ground. Since there could be other models on the ground, we do a check through the list of models to make sure that we only move the character if the terrain model is on the collision list.

2. Play the movie and test what you just created.
You will notice that when you try walk through the wall on the flat part of the ground, you are prevented from doing so. However, if you walk up the mound, you can walk through the wall. The reason for this is that the ray being cast forward is missing the wall (is going over it).
You can download the completed movie from here.