Title: 3D Space
13D Space
- Authoring Interactivity 2
23D
33D - Collision
- Collision detection is a very important part of
any scripted interaction. - It is fairly simple and fast to detect the
intersection/collision of 2d assets as it is only
necessary to check across the surface of a 2d
plane - 3d intersections require considerably more
processing power and some complex mathematical
algorithms - At the simplest level, collision of 3d objects is
detected by mathematically defining a bounding
sphere that completely encloses the objects and
then constantly checking to see if these spheres
intersect -
43D Collision Bounding sphere
In this 3d world, the bounding spheres of the 3d
objects have been made visible
- The only object with a good fit for the
bounding sphere is the sphere object - All other objects have spheres considerably
larger than their meshes
53D Collision Bounding spheres
- This means that collision is detected before the
objects actually intersect -
63D Collision Bounding spheres
- A useful relationship between max and director
means that even when the pivot point is moved,
the bounding sphere is still drawn from the
objects centre, not from the adjusted pivot point -
73D Collision
- In director to detect collision
- We have to add a modifier to the object
- Modifiers are Directors way of adding
functionality to 3d models - There are different types of modifier we will
explore others later - We are interested in the collision modifier
- member("2obs").model("sphere01").addmodifier(col
lision)
3d world object
Model object
addModifier method
Director eg
83D Collision
- It is necessary to add the collision modifier to
all objects we wish to check for intersection - on beginsprite me
- member("2obs").resetworld()
- member("2obs").model("sphere01").addmodifier(coll
ision) - member("2obs").model("box01").addmodifier(collisi
on) - end
- This code has been placed in a beginsprite
handler so it runs only once - Is this the best place for it?
- Maybe.
- Collision detection is processor hungry, once the
modifier is added, collision is checked for all
the time can be more sensible to switch it on
when objects are closer - More on this later..
93D Collision Modes
- Our collision detection is imperfect because the
bounding sphere doesnt fit the box object well - Usually, we have a choice of modes that we can
apply to collision detection - Director allows for box and mesh
- Mode is a property and so can be changed by
simple assignment
103D Collision Modes
- Setting the collision mode
- member("2obs").model("box01").addmodifier(collisi
on) - member("2obs").model("box01").collision.modebo
x
model object
box mode
3d world object
Collision object
Mode property
Director eg
113D Collision Mesh
- Obviously, the box mode is only useful in some
instances - Director example
- Often more useful to use the mesh modifier
- If we do, we have to apply it to all models
- member("cone").model("sphere01").addmodifier(coll
ision) - member("cone").model("sphere01").collision.mode
mesh - member("cone").model("cone01").addmodifier(coll
ision) - member("cone").model("cone01").collision.modem
esh - Director example
123D Collision What to do.
- Detecting collision is all well and good, but how
do we respond to it? - We use the registerForEvent method
- This registers a collision to call a function
when the collision is detected (can be used to
register other events) - Looks like this
- member("cone").registerForEvent(collideAny,
hascollided, me)
3d world object
registeForEvent method
parameters
133D Collision handling
- We then need to write an on hasCollided handler
- on exitFrame me
- if keypressed (126) then
- member("cone").model("cone01").translate(2,0,0
) - end if
- end
- on beginsprite me
- member("cone").resetworld()
- member("cone").model("sphere01").addmodifier(co
llision) - member("cone").model("cone01").addmodifier(coll
ision) - member("cone").model("cone01").collision.modeb
ox - member("cone").registerForEvent(collideAny,
hascollided, me) - end
- on hascollided me, collisionData
- whichobjcollisiondata.modela.name
- member("cone").model(whichobj).removefromWorld()
143D Collision handling
Director example
- Lets look at that handler in a bit more detail
A handler in director is like a function in
ActionScript Always begins with on (the name is
given by you) me identifies where the script
is collisionData is a built in object, passed
into the handler
on hasCollided me, collisionData
whichobjcollisiondata.modela.name
member("cone").model(whichobj).removefromWorld()
end
Here, I am storing the name of the model collided
with in a variable called whichObj
Here, I removing the object collided with from
the world
Handlers always need an end at the end
collisionData has modelA, ModelB,
pointOfContact, collisionNormal
153D Collision Modifier Summary
- Mesh is very processor hungry
- Box less so
- Sphere least of all
- Sphere is usually a default collision mode
- Should consider turning collision detection on
only when objects are near - need to do some
maths usually involving trigonometry - Hows your trig?
- Should only use in simple environments with few
possible collisions - Often sphere mode is fine as collision responses
are faster than the eye perceives - Use registerForEvent to deal with collisions
- Theres a better solution but its more
complicated..
163D Collision Casting Rays
- Almost all 3d scripting/processing languages have
a method which allows an invisible ray to be
projected - from a particular point
- in a particular direction
- The method usually returns
- a list of the models intersected by the ray
- The distance from the rays origin to the
intersected model - We can use this to cast a ray from a particular
model to check for intersection with other models - Because this process is much simpler than testing
for bounding collision, it is also much, much,
much faster - As such it is commonly used in complex game
environments where there are many possible model
intersections
173D Collision Casting Rays
- The director method is
- modelsUnderRay
- It needs 2 parameters
- What will they be?
- Origin of the ray
- Direction of the ray
- Can have another 2 parameters
- How many models to check for
- How detailed the information it gives back is
- Can just give the intersected models name but if
we choose a detailed list, it includes - Model name
- distance to model
- Position of intersection as a vector
- Direction of intersection as a vector
183D Collision Casting Rays
- modelsUnderRay(rayStart, rayDirection, 1,
detailed)
193D Collision Casting Rays
- Simple case study
- on exitFrame me
- if keypressed (123) then
- member("cone").model("cone01").rotate(0,0,1)
- end if
- --
- if keypressed (124) then
- member("cone").model("cone01").rotate(0,0,-1)
- end if
- --
- if keypressed (126) then
- rayStartmember("cone").model("cone01").transf
orm.position - raystart.z5
- rayDirectionmember("cone").model("cone01").tr
ansform.xaxis - put raydirection
- thelistmember("cone").modelsUnderRay(rayStart
, rayDirection, 1, detailed) - if thelist.countgt0 then
- nearnessthelist1.distance
203D Collision Casting Rays
- There are issues with the raycasting method
discussed - Objects can pass through other objects at the
edges if the ray does not intersect - We can avoid this by casting more than one ray
from an object this is usually done by casting
rays from extremities of the object - We create objects that have limited extremities
(as part of the design process
213D Collision Casting Rays
We could cast rays from this objects extremities
to detect the proximity of other objects and
respond to collisions
223D Collision Casting Rays
- If we need very fine collision detection, we can
combine ray tracing with the collision modifier - We detect the proximity of an object and if it is
close enough to worry about, we turn mesh
collision detection on - Director example
- How could the example be optimised?
- By only turning the collision detection on for
the single object that was detected by
raytracing.