COLLISION: AVATAR x WALLS AND OTHER COLLISIONS

Director has the usual system of "spheres of collision" for detection of the moment when two objects collide. We will talk a little about this in the lesson.

But first: we need to review a little "who is our avatar":

The values of: avatarheight, cameraoffset (2 values) and projectionangle need sometimes to be redefined in a new project for the best precision of collisions of the avatar.

When we work detecting collision, the ideal values of cameraoffset are something like: 0.001 and -0.003. The proportion between this values define the basic angle of the camera. If they are little, the camera and the object aux (used to glue the camera to the avatar) are inside the avatar and the precision of the collision is better.

The collison between two objects are detected (there are some other techniques but this is the cheaper in machine cicles and is the Director default) using an sphere that is around the object.

. It's not a very precise method but works.

All the basic lines of code to work with the collision of an avatar are in the avatarobj script/new

To an object, in a Director world be available for collision it needs to be declared in the lines of code:

  myworld.model("avatar").addmodifier(#collision)
  myworld.model("avatar").collision.enabled = true
  myworld.model("avatar").collision.resolve = true

If we are trying to administrate the collision of the avatar against the object/model: "ball", by example, this object needs also to be declared:

 myworld.model("ball").addmodifier(#collision)
 myworld.model("ball").collision.enabled = true
 myworld.model("ball").collision.resolve = true

When an avatar collides with another model that has the #collision modifier attached, it will stop moving.

Sometimes this method of colliding "spheres" - the Director default - is inviable because the object is not like a sphere and we need to have an acceptable precision . In this case you need to add another line in the group (for each object and for the avatar):

myworld.model("avatar").collision.mode = #mesh

This technique spend lots of CPU time if the object is very complex (many polygons in the mesh).

There are the option #box if your object is near the box format.

We can capture some data in the moment of the collision if we define for an object (the avatar in our case):

  
  myworld.model("avatar").collision.setCollisionCallBack(#mydata,me) 
 

mydata is the name of a method that we need to create to use the data - could be another name:

on mydata me , collisionData
 -- we can use the properties of the collision to many things.
 put collisionData.modelA --A and B are the objects in the collision
 put collisionData.modelB
 put collisionData.pointOfContact
 put collisionData.collisionNormal
end
Using put we only write the data in the Message window (a interesting tool to help the programmers).

Play the demo . Now the avatar will collide against the plaque with the logo (but not against the sphere because this was not declared).




PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE