[away3d] Ray.getIntersect(p0:Number3D, p1:Number3D, v0:Number3D, v1:Number3D, v2:Number3D):Number3D

48 views
Skip to first unread message

Michael Iv

unread,
May 3, 2010, 3:43:02 PM5/3/10
to away3d.dev
Someone can tell me what are v0,v1,v2 in Ray.getIntersect(p0:Number3D, p1:Number3D, v0:Number3D, v1:Number3D, v2:Number3D):Number3D ???
Just can't find any example explaining these

Thanks!

--
Michael Ivanov ,Programmer
Neurotech Solutions Ltd.
Flex|Air |3D|Unity|
www.neurotechresearch.com
Tel:054-4962254
explo...@gmail.com
te...@neurotech.co.il

Reinorvak

unread,
May 3, 2010, 3:50:40 PM5/3/10
to Away3D.dev
v0-2, are the vertices used in a triangle. getIntersect is testing if
the starting point p0, and the ending point p1, intersect the triangle
defined by v0, v1, and v2.

On May 3, 3:43 pm, Michael Iv <explomas...@gmail.com> wrote:
> Someone can tell me what are* v0,v1,v2* in* Ray.getIntersect(p0:Number3D,
> p1:Number3D, v0:Number3D, v1:Number3D, v2:Number3D):Number3D* ???
> Just can't find any example explaining these
>
> Thanks!
>
> --
> Michael Ivanov ,Programmer
> Neurotech Solutions Ltd.
> Flex|Air |3D|Unity|www.neurotechresearch.com
> Tel:054-4962254
> explomas...@gmail.com
> t...@neurotech.co.il

Michael Iv

unread,
May 3, 2010, 4:03:26 PM5/3/10
to away3...@googlegroups.com
I thought about it and tried to test against face v0 v1 v2  of sphere . Got nothing of this .

Reinorvak

unread,
May 3, 2010, 4:10:53 PM5/3/10
to Away3D.dev
The number3D that the function returns, I believe, is the point of
intersection. If the return should show up blank, there is no
intersection.

On May 3, 4:03 pm, Michael Iv <explomas...@gmail.com> wrote:
> I thought about it and tried to test against face v0 v1 v2  of sphere . Got
> nothing of this .
>
> On Mon, May 3, 2010 at 10:50 PM, Reinorvak <timpellitter...@gmail.com>wrote:
>
>
>
> > v0-2, are the vertices used in a triangle. getIntersect is testing if
> > the starting point p0, and the ending point p1, intersect the triangle
> > defined by v0, v1, and v2.
>
> > On May 3, 3:43 pm, Michael Iv <explomas...@gmail.com> wrote:
> > > Someone can tell me what are* v0,v1,v2* in* Ray.getIntersect(p0:Number3D,
> > > p1:Number3D, v0:Number3D, v1:Number3D, v2:Number3D):Number3D* ???
> > > Just can't find any example explaining these
>
> > > Thanks!
>
> > > --
> > > Michael Ivanov ,Programmer
> > > Neurotech Solutions Ltd.
> > > Flex|Air |3D|Unity|www.neurotechresearch.com
> > > Tel:054-4962254
> > > explomas...@gmail.com
> > > t...@neurotech.co.il
>
> --
> Michael Ivanov ,Programmer
> Neurotech Solutions Ltd.
> Flex|Air |3D|Unity|www.neurotechresearch.com
> Tel:054-4962254
> explomas...@gmail.com
> t...@neurotech.co.il

Michael Iv

unread,
May 3, 2010, 4:43:34 PM5/3/10
to away3...@googlegroups.com
Yes , but the problem is that there has to be one because I am casting a ray from cam position to the mouse screen X Y Z coords . So I am pointing to the very Face where I should get the intersection

Reinorvak

unread,
May 3, 2010, 5:16:43 PM5/3/10
to Away3D.dev
One of the strange quirks of Away3D that I've discovered working on
enclosed spaces, is that the mouse X,Y,Z stops at the first object it
hits and doesn't continue farther into the scene. I've also tried my
own C++ versions of ray casting in Away3D to come up fruitless. What
exactly are you trying to achieve with this ray intersection?

On May 3, 4:43 pm, Michael Iv <explomas...@gmail.com> wrote:
> Yes , but the problem is that there has to be one because I am casting a ray
> from cam position to the mouse screen X Y Z coords . So I am pointing to the
> very Face where I should get the intersection
>
> explomas...@gmail.com
> t...@neurotech.co.il

Michael Iv

unread,
May 3, 2010, 5:34:52 PM5/3/10
to away3...@googlegroups.com
Today earlier one guy here asked how to optimize his geometry .He was using 64 different plains for cheese cell in order to highlight them when a chess takes a cell .  I once did something similar in PV3D but with JIGLIB where I needed to check if my car is still on the road . So I was casting a ray  and gettting materials pixels colors and by this getting to know if I am on the road or not . Now I wanted to help this guy .And because I could not find the ray casting code that I have done sometime ago and also because I knew that in Away there is RAY so decided to try it on sphere . I thought that getIntersect returns us a face where the intersection is happening . But actually I paid attention that that it returns some Number 3d data once per 30-40 samplings because  I tried to loops througth all the faces while testing the intersect like this :
///// it runs on enter frame////////////////
for(var i:int=0;i<_sf.faces.length;++i){
                trace(_ray.getIntersect(_ray.orig,_ray.dir,_sf.faces[i].v0.position,_sf.faces[i].v1.position,_sf.faces[i].v2.position));
   }
and basically I am getting some output once a 200-300 frames .
May be it is good only for plains ?

//////////////////////////////////////

Michael Iv

unread,
May 3, 2010, 5:38:22 PM5/3/10
to away3...@googlegroups.com
Also I agree with you on those Ray---> Mouse anomalies . I see that when I cast the ray it's direction Z part is growing (that is should be ) but when I am on Object3D it stops Z on the Object's surface and then continues to grow . So if you append some Object to these coords you will see that that object is like a ball spinning back and forth on Z .

Strange stuff .

On Tue, May 4, 2010 at 12:16 AM, Reinorvak <timpell...@gmail.com> wrote:

Reinorvak

unread,
May 3, 2010, 6:09:16 PM5/3/10
to Away3D.dev
It should work for any face based on the 3 vertices, not sure though
since I didn't write it.

Just took a quick look at it and the method should work for any face
object. For a sphere though it has a different function entirely.

intersectBoundingRadius(pos:Number3D, radius:Number):Boolean

Maybe not so good to test with a sphere and try a cube instead?

On May 3, 5:38 pm, Michael Iv <explomas...@gmail.com> wrote:
> Also I agree with you on those Ray---> Mouse anomalies . I see that when I
> cast the ray it's direction Z part is growing (that is should be ) but when
> I am on Object3D it stops Z on the Object's surface and then continues to
> grow . So if you append some Object to these coords you will see that that
> object is like a ball spinning back and forth on Z .
>
> Strange stuff .
>
> explomas...@gmail.com
> t...@neurotech.co.il

Fabrice3D

unread,
May 3, 2010, 6:15:23 PM5/3/10
to away3...@googlegroups.com
dir, origin, v0, v1, v2
to check if a ray hits a triangle

Fabrice

Michael Iv

unread,
May 3, 2010, 6:16:14 PM5/3/10
to away3...@googlegroups.com
I am thinking already about a little different approach , because intersectBoundingRadius would not give me a face of intersection .
Actually the easiest is to assign mouse listeners to all the faces and that is all !
Just need to figure out how to detect the adjacent face that forms a polygon  together with the first . Then it is possible to change material on cells like I said before . 

Michael Iv

unread,
May 3, 2010, 6:17:28 PM5/3/10
to away3...@googlegroups.com
Fabrice i am trying it already for 4 hours with Sphere , but it returns null most of the time


///// it runs on enter frame////////////////
for(var i:int=0;i<_sf.faces.length;++
i){
                trace(_ray.getIntersect(_ray.orig,_ray.dir,_sf.faces[i].v0.position,_sf.faces[i].v1.position,_sf.faces[i].v2.position));
   }
and basi


devlander

unread,
May 3, 2010, 8:33:13 PM5/3/10
to Away3D.dev
Hello, i am having the exact same problem here.

I am trying to determine the intersection on a 3D object at a given
viewport location (view3D.mouseX, view3D.mouseY):

Here is my code:


private function _pickMesh(mesh:Mesh):PickingResult {

var pr:PickingResult = new PickingResult(); // Store picking data
here

var rayPos:Number3D = view3D.camera.unproject(view3D.mouseX,
view3D.mouseY);

// info retreived from here
// http://www.mail-archive.com/away3...@googlegroups.com/msg09318.html

var rayDir:Number3D = new Number3D();
rayDir.x = pMouse.x;
rayDir.y = pMouse.y;
rayDir.z = pMouse.z;
rayDir.normalize();
rayPos.add(rayPos, _view3D.camera.position);

var ray:Ray = new Ray();
var pickedPoint:Number3D;

for each (var face:Face in mesh.faces) {

pickedPoint = ray.getIntersect(rayPos , rayDir , face.v0.position ,
face.v1.position , face.v2.position);

if (pickedPoint!=null) {
pr.hasHit = true;
pr.point = pickedPoint;
pr.face = face;
break;
}
}

pr.mesh = mesh;

return pr;
}

The problem is I always get null.

Note: Since my mesh is a child of an Objectcontainer3D element I have
called mesh.applyRotations()
to make sure all the mesh faces are computed relative to the world
coord system.

I suspect there is a problem in the computation of the ray params
(position or direction).

Fabrice can you confirm that the signature of the Ray.getIntersect
method is as follow (no info is given in the class):

p0: ray direction (or is it position ?)
p1: ray position [in world coord system] (or is it direction?)
v0: face.v0.position [in global world coord system]
v1: face.v0.position [in global world coord system]
v2: face.v0.position [in global world coord system]


Thanks

On May 4, 12:17 am, Michael Iv <explomas...@gmail.com> wrote:
> Fabrice i am trying it already for 4 hours with Sphere , but it returns null
> most of the time
>
> ///// it runs on enter frame////////////////
> for(var i:int=0;i<_sf.faces.length;++
> i){
>
> trace(_ray.getIntersect(_ray.orig,_ray.dir,_sf.faces[i].v0.position,_sf.faces[i].v1.position,_sf.faces[i].v2.position));
>    }
> and basi
>
>
>
>
>
>
>
>
>
> On Tue, May 4, 2010 at 1:15 AM, Fabrice3D <fabric...@gmail.com> wrote:
> > dir, origin, v0, v1, v2
> > to check if a ray hits a triangle
>
> > Fabrice
>
> > On May 3, 2010, at 9:43 PM, Michael Iv wrote:
>
> > Someone can tell me what are* v0,v1,v2* in* Ray.getIntersect(p0:Number3D,
> > p1:Number3D, v0:Number3D, v1:Number3D, v2:Number3D):Number3D* ???
> > Just can't find any example explaining these
>
> > Thanks!
>
> > --
> > Michael Ivanov ,Programmer
> > Neurotech Solutions Ltd.
> > Flex|Air |3D|Unity|
> >www.neurotechresearch.com
> > Tel:054-4962254
> > explomas...@gmail.com
> > t...@neurotech.co.il
>
> --
> Michael Ivanov ,Programmer
> Neurotech Solutions Ltd.
> Flex|Air |3D|Unity|www.neurotechresearch.com
> Tel:054-4962254
> explomas...@gmail.com
> t...@neurotech.co.il

devlander

unread,
May 3, 2010, 8:49:14 PM5/3/10
to Away3D.dev
=================================================================
Please forget about my previous post (many copy&past mistakes in the
code snippet)
=================================================================

Here is my code (corrected):

private function _pickMesh(mesh:Mesh):PickingResult {

var pr:PickingResult = new PickingResult(); // Store picking
data here

var rayPos:Number3D = view3D.camera.unproject(view3D.mouseX,
view3D.mouseY);

var rayDir:Number3D = new Number3D();
rayDir.x = rayPos.x;
rayDir.y = rayPos.y;
rayDir.z = rayPos.z;
rayDir.normalize();
rayPos.add(rayPos, view3D.camera.position);

var ray:Ray = new Ray();
var pickedPoint:Number3D;

for each (var face:Face in mesh.faces) {

pickedPoint = ray.getIntersect(rayPos , rayDir ,
face.v0.position , face.v1.position , face.v2.position);

if (pickedPoint!=null) {
pr.hasHit = true;
pr.point = pickedPoint;
pr.face = face;
break;
}
}

pr.mesh = mesh;

return pr;

}

The problem is I always get null.

Note: Since my mesh is a child of an Objectcontainer3D element I have
called mesh.applyRotations()
to make sure all the mesh faces are computed relative to the world
coord system.

I suspect there is a problem in the computation of the ray params
(position or direction).

Fabrice can you confirm that the signature of the Ray.getIntersect
method is as follow (no info is given in the class):

p0: ray direction (or is it position ?)
p1: ray position [in world coord system] (or is it direction?)
v0: face.v0.position [in global world coord system]
v1: face.v1.position [in global world coord system]
v2: face.v2.position [in global world coord system]

Thanks for any help on this.






On May 4, 2:33 am, devlander <pascal.deba...@gmail.com> wrote:
> Hello, i am having the exact same problem here.
>
> I am trying to determine the intersection on a 3D object at a given
> viewport location (view3D.mouseX, view3D.mouseY):
>
> Here is my code:
>
> private function _pickMesh(mesh:Mesh):PickingResult {
>
>         var pr:PickingResult = new PickingResult();  // Store picking data
> here
>
>         var rayPos:Number3D = view3D.camera.unproject(view3D.mouseX,
> view3D.mouseY);
>
>         // info retreived from here
>         //http://www.mail-archive.com/away3...@googlegroups.com/msg09318.html

Michael Iv

unread,
May 4, 2010, 1:18:42 AM5/4/10
to away3...@googlegroups.com
Now I can see I am not only one having that problem

Sent from my iPhone

Fabrice3D

unread,
May 4, 2010, 3:44:38 AM5/4/10
to away3...@googlegroups.com
Looking at it asap as I need it as well...

devlander

unread,
May 4, 2010, 5:11:11 AM5/4/10
to Away3D.dev
ok Fabrice.
Please notify us if you can get something working ( this is a critical
feature for my project ).

Thanks for your support.

On May 4, 9:44 am, Fabrice3D <fabric...@gmail.com> wrote:
> Looking at it asap as I need it as well...
>
> On May 4, 2010, at 7:18 AM, Michael Iv wrote:
>
>
>
>
>
>
>
> > Now I can see I am not only one having that problem
>
> > Sent from my iPhone
>

Michael Iv

unread,
May 5, 2010, 7:09:07 AM5/5/10
to away3...@googlegroups.com
I was looking at the following function in BitmapMaterial Class:
if(showNormals){
               
                _nn.rotate(tri.faceVO.face.normal, tri.view.cameraVarsStore.viewTransformDictionary[tri.source]);
                 
                _sv0x = (tri.v0x + tri.v1x + tri.v2x) / 3;
                _sv0y = (tri.v0y + tri.v1y + tri.v2y) / 3;
                 
                _sv1x = (_sv0x - (30*_nn.x));
                _sv1y = (_sv0y - (30*_nn.y));
                 
                _session.renderLine(_sv0x, _sv0y, _sv1x, _sv1y, 0, 0xFF00FF, 1);
            }
        }

And I can see ( if I am right) that you are setting _nn roation multiplying face normal by face matrix  that was received from camera space .
May be that is what has to be done  when getting faces for getIntersection ?

Fabrice3D

unread,
May 5, 2010, 8:25:11 AM5/5/10
to away3...@googlegroups.com
yes, ray and tris needs to be in same space. I've sended you my RayDebugger class, should be visually obvious.

Working atm on other stuff using rays, I do see the trunk ray class can use more handy stuffs.
So I hope in near future be able update it with more friendly, less expensive and usefull hittests/returns

stay tuned

Fabrice

Fabrice3D

unread,
May 5, 2010, 8:51:49 AM5/5/10
to away3...@googlegroups.com
made little demo to show you thats this is probably what is going on in your code.

--> this demo projects a stroke 8 px wide in center screen (800x600), in this case at 0,0 to 0,400 x/y
the ray debugger shows you the camera, viewport, and the stroke.
the debug rays are using the unproject camera method. (debug rays projected each 20 pixels only first column to stay friend with the flash player)
as you can see the direction obtained once normalized, is what you want. And also shows Ray class is not wrong.

Now, the sphere in the world is still object space. not having any of the camera perpective.
Changing camera after calcs to hovercam to explore, you can see the debug trace shows that once you put yourself behind the camera
that the result of the stroke does match the rays using the Ray class where no perpective or lens corrections are applied.
except its obvious that the rays do not fit the image where the white (the hit) should be way smaller if in same space as camera


Again, I'll try improve that class to make it smarter.. its all a question of time :)

Fabrice



On May 5, 2010, at 1:09 PM, Michael Iv wrote:

Michael Iv

unread,
May 5, 2010, 9:01:28 AM5/5/10
to away3...@googlegroups.com
I am quite sure that the ray class casts the ray in right direction , but the problem that I could not understand was why no intersection was returned .
In your example do you receive the intersection of these rays with the globe ?

Fabrice3D

unread,
May 5, 2010, 9:04:45 AM5/5/10
to away3...@googlegroups.com
the stroke bmd shows white pixel when hit, no hit black.

fabrice

Fabrice3D

unread,
May 5, 2010, 9:07:50 AM5/5/10
to away3...@googlegroups.com
I think I may know why you miss....
in demo I have offseted the screen, because coordinates flash are top left.
pixel = _view.camera.unproject(startx-400, starty+j-300);

here 400 and 300 being hardcoded as 1/2 values of my screen 800x600

Fabrice

Michael Iv

unread,
May 5, 2010, 9:13:17 AM5/5/10
to away3...@googlegroups.com
That is interesting point . I wll try this . Sorry I cant see the white pixels , it is too far from the screen :)

devlander

unread,
May 5, 2010, 10:37:46 AM5/5/10
to Away3D.dev
Fabrice where can I get your RayDebug debug class ?

I tried to apply the screen offset but still no success.

Thanks

On May 5, 3:13 pm, Michael Iv <explomas...@gmail.com> wrote:
> That is interesting point . I wll try this . Sorry I cant see the white
> pixels , it is too far from the screen :)
>
>
>
>
>
>
>
> On Wed, May 5, 2010 at 4:07 PM, Fabrice3D <fabric...@gmail.com> wrote:
> > I think I may know why you miss....
> > in demo I have offseted the screen, because coordinates flash are top left.
> > pixel = _view.camera.unproject(startx-400, starty+j-300);
>
> > here 400 and 300 being hardcoded as 1/2 values of my screen 800x600
>
> > Fabrice
>
> > On May 5, 2010, at 3:01 PM, Michael Iv wrote:
>
> > I am quite sure that the ray class casts the ray in right direction , but
> > the problem that I could not understand was why no intersection was returned
> > .
> > In your example do you receive the intersection of these rays with the
> > globe ?
>
> ...
>
> read more »

Fabrice3D

unread,
May 5, 2010, 10:59:02 AM5/5/10
to away3...@googlegroups.com


here you go...

Fabrice
RayDebugger.as

Michael Iv

unread,
Jun 3, 2010, 3:42:52 PM6/3/10
to away3...@googlegroups.com
Hi Fabrice , did you implement the Ray Class in the example you have given here for sphere intersection . I have tried everything and still ray.getIntersect () returns null . What can be wrong in what I do here ?

////////////////////////code//////////////////////////////////
package
{
    import away3d.core.base.Face;
    import away3d.core.base.Segment;
    import away3d.core.base.Vertex;
    import away3d.core.draw.ScreenVertex;
    import away3d.core.math.Number3D;
    import away3d.core.utils.Cast;
    import away3d.materials.BitmapMaterial;
    import away3d.materials.ColorMaterial;
    import away3d.materials.utils.data.Ray;
    import away3d.primitives.Sphere;
    import away3d.sprites.DirSprite2D;
    import away3d.sprites.Sprite2D;
    import away3d.sprites.Sprite3D;
   
    import flash.display.BitmapData;
    import flash.events.Event;
   
    import utils.RayDebugger;

    [SWF(backgroundColor="#677999", frameRate="30", quality="LOW", width="800", height="600")]
    public class RayTracingDemo extends AwayTemplate
    {
        private var _rayDebug:RayDebugger;
        private var _sp:Sphere;
        private var _ray:Ray;
        private var _screenVertex:ScreenVertex;
        private var _vertex:Vertex;
        private var _sprite3:Sprite3D;
        private var _traceDefaultPos:Number3D=new Number3D(-100,100,-100);
        public function RayTracingDemo()
        {
           
            super();
        }
        override protected function initGeometry() : void{
            RayDebugger.setView(_view);
           
         
           
           _sp=new Sphere({radius:30,material:new ColorMaterial(0x339955)});
           _view.scene.addChild(_sp);
           _sp.z=500;
           _sp.y=0;
           _sp.x=0;
      
    
     
        _ray=new Ray();
       
       
        }
        override protected function onEnterFrame(e:Event) : void{
            super.onEnterFrame(e);
            var mouseDir1:Number3D=_cam.unproject(mouseX-400,mouseY-300);
       
            mouseDir1.x*=10;
            mouseDir1.y*=10;
            mouseDir1.z*=10;
            _ray.orig= _cam.position;
            _ray.dir= mouseDir1;///_sp.position;///_cam.transform.forward;
     
          
               
                    for(var b:int=0;b<_sp.faces.length;++b){
                       
                       
                       
              
                        var face:Face=_sp.faces[b];
                   
                        _ray.orig= _traceDefaultPos;
                        _ray.dir= mouseDir1;d;
                   
                           
                    trace(    _ray.getIntersect(_ray.orig,_ray.dir,face.v0.position,face.v1.position,face.v2.position));
              
                        
                        }
                       
                }
          
               
                    RayDebugger.traceRay(_ray.orig,_ray.dir,0x228833,false);
                    RayDebugger.traceViewPortRect(0,0,800,600,400,300);

Fabrice3D

unread,
Jun 3, 2010, 4:53:31 PM6/3/10
to away3...@googlegroups.com
yes, exact same one, what does the debugger shows?
I mean, I've used it for something else last week (and dying to go back to it soon )
all debugs and returns were correct...

But what I did in a specific case was this.

pixel = _view.camera.unproject(i-centerx, j-centery);
_dir.x = pixel.x;
_dir.y = pixel.y;
_dir.z = pixel.z;
_dir.normalize();
pixel.x += (1000000*_dir.x)+poscam.x;
pixel.y += (1000000*_dir.y)+poscam.y;
pixel.z += (1000000*_dir.z)+poscam.z;

because in some cases, the class subs causing negative/reverts
this was just a measure (read quick fix) for this issue. for the rest, works just fine. Prefab renders...
note also the excenter top left to compensate with the screencoords

but if you do use the raydebugger i gave you you should visualize the rays you shoot.
use the debugger also to draw your vertexes points... this class visually helped me often when I was taking an away rendering
being equal to my faces/vertex definition passed to the class...

Fabrice

Michael Iv

unread,
Jun 3, 2010, 5:13:13 PM6/3/10
to away3...@googlegroups.com

That is the problem. I do use your debugger and the Ray is drawn just like I defined. But the function returns null. May be it is because I use a sphere for the check?
Thanks
Sent from my iPhone

Fabrice3D

unread,
Jun 3, 2010, 5:40:43 PM6/3/10
to away3...@googlegroups.com
Well if the ray is ok, then the face it's supposed to hit is not at the right place at least in terms of definition for the class.

Fabrice
Reply all
Reply to author
Forward
0 new messages