Navigation


RSS : Articles / Comments


distanceTo - simple but nimble

9:36 PM, Posted by Jim Foley, 3 Comments

distanceTo is a little known method (to me/some) within the Papervision 3D class library. What it does is it returns the value of the distance between two objects. Using it is simple, all you have to do is access the distanceTo method from any 3D object as though it were a standard property.

Example:
//returns the distance between the two objects.
trace(myBox.distanceTo(mySphere));

This is a simple but powerful tool. For what you may ask? Well, recently I had to make a set of objects fade off into the distance, so what I did was set up a dummy DisplayObject3D object, added it to the scene and placed it way back on the Z axis. Then I calculated how far the objects were from the dummy object. The farther back they went the more they faded out (or, the closer they got to the dummy object the more they faded out), almost as if they were fading off in a fog.

Distance example: click here
Distance source: click here

3 Comments

Dustin @ October 14, 2008 8:43 AM

I'm not sure why you say that it distanceTo() is little known. Pretty much everyone I've worked with, and many people on the mailing list use this in a lot of their PPV3D projects. Back in the earlier days of PPV3D people were having a lot of trouble rotating and orbiting things around objects. One thing they used to calculate the position of the orbiting object was to use distanceTo(). I'm not sure if its used quite as much these days, but it's by no means little known.

Jim Foley @ October 14, 2008 10:00 AM

Thanks for enlightening me Dustin. I modified my post for ya. ;-)

Dustin @ October 14, 2008 10:33 AM

I like it :)