Considerations in Collision Detection with PV3D

col.png
use left and right arrows.

The collision detection in Papervision3D is not a big deal. Is easy to detect collision between two DisplayObject3D for that task PV3D provide us with two methods

public function hitTestPoint( x:Number, y:Number, z:Number ):Boolean

public function hitTestObject( obj:DisplayObject3D,multiplier:Number=1 ):Boolean

The first method is to test if the current DisplayObject3D is hitting a point in the scene and the second one is to detect if the current object is hitting another DisplayObject3D in the scene.
This is quite similar to the display architecture used by flash and its DisplayObject the difference is when we have a main DisplayObject3D with other objects 3D as childs.
In that case we would be tempted to use hitTestObject assuming that the collision test will be tested using the parent DisplayObject3D and their childs but that is wrong.

Lets suppose that we have a parent DisplayObject3D A on our Scene and inside that object we have a Collada mesh object which has been added to A at initialization time. Inside the main loop I’d like to test if my object A is hitting another DisplayObject3D called B which has and sphere object as child.
To do this I use something like this:

if( A.hitTestObject(B) )
{
	trace("hit");
}

This would never do what we expected to do. The hit test between A and B will never return true. Let’s see why. The main reason is that the hit test of a DisplayObject3D do not take in consideration their childs, it only use the geometry of current object and the geometry of the object received as parameter.
When I say geometry I am referring to the “geometry” member of the class DisplayObject3D which its is GeometryObject3D.
From the PV3D documentation:”When different objects share the same geometry, they become instances. They are the same object, displayed multiple times. Changing the shape of this object changes the shape of all of its instances.
Instancing an object saves system memory, and is useful to display an object multiple times while maintaining its shape.
For example, you could create armies and forests full of duplicate objects without needing the memory to handle that much actual geometry. Each instance has its own transform node so it can have its own position, rotation, and scaling.”When we create an empty DisplayObject3D in this way:

private var mContA:DisplayObject3D = new DisplayObject3D();

The geometry of the mContA is set to null so when the hit test is made there is no geometry object to test if the two objects are colliding.
One possible solution is assign to the geometry of the parent the geometry of the child what you want to be used when the hiTestObject is called.
Other solution is evaluate your collision using dummy DisplayObject3D attached to the object what you want to test collision, so when you call hitTestObject you do it on the dummy object for example an sphere or a plane.
A third possible solution is use the following function what I made using the PV3D code for hitTestObject but I just add two extra arguments, geometries. So you give the parents DisplayObject3D as parameter and the geometries of their childs what will be used in the hit test.
You can find a sample source code of this case here.

Advertisements
Explore posts in the same categories: Actionscript, flash, Papervision3D

Tags: , , , , ,

You can comment below, or link to this permanent URL from your own site.

One Comment on “Considerations in Collision Detection with PV3D”

  1. Wred Says:

    Aha! Thanks for this. I was wondering why my hit tests weren’t working and this explains it… Good article.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: