POV-Ray : Newsgroups : povray.general : Check if objects intersect : Re: Check if objects intersect Server Time
30 Jul 2024 16:25:45 EDT (-0400)
  Re: Check if objects intersect  
From: Tim Attwood
Date: 9 Nov 2008 17:33:09
Message: <49176525@news.povray.org>
>> In some circumstances you may be able to construct an approach using the
>> min_extent and max_extent functions with the object you get by using an
>> intersection of the two objects you're interested in to help determine 
>> this,
>> but these functions use the bounding box of the resulting object and, as 
>> the
>> help says: "this is not perfect, in some cases (such as CSG intersections
>> and differences or isosurfaces) the bounding box does not represent the
>> actual dimensions of the object".
>>
>> Given this, you could (with a considerable degree of caution) construct a
>> macro that may be able to serve your particular needs by combining the
>> min_extent and max_extent functions and the trace function. If the extent
>> values returned are very large (10000000000), then the objects don't
>> intersect. If any of the dimensions is zero, then they don't intersect.
>> Otherwise you'd need to fire a grid of lines through the space using the
>> trace function to see if any hit the object. If any hit the intersection
>> object, then it exists. If none hit the object it's still possible that 
>> the
>> original objects intersected, but that your rays were'nt close enough
>> together to hit it.
>>
>> I've pasted in a bit of SDL below that may help if you wish to explore 
>> this
>> approach.
>>
>> Hope that helps.
>>
>> Regards,
>> Chris B.
>
> Thank you very much for your excellent reply! I do not, however, want to 
> go to
> so much trouble at this time. I have decided that it is sufficient for my
> purposes to approximate things by constructing "bounding spheres".
>
> -Mike

I worked on this before...
this macro detects collisions of two objects.

It works by casting random rays at an intersection
some number of times.
While theoretically it could miss a collision, in
practice the sort of collisions it misses are
the sort of thing that are hard for the human eye
to see.

#macro collision(A B rez)
   #local result = false;
   #if (((min_extent(A).x > max_extent(B).x ) |
         (min_extent(B).x > max_extent(A).x ) |
         (min_extent(A).y > max_extent(B).y ) |
         (min_extent(B).y > max_extent(A).y ) |
         (min_extent(A).z > max_extent(B).z ) |
         (min_extent(B).z > max_extent(A).z ))=false)
      #local AB = intersection{object{A} object{B}};
      #local Mn = min_extent(AB);
      #local Mx = max_extent(AB);
      #local S1 = seed(1);
      #local cnt = 0;
      #while ((result = false) & (cnt < rez))
         #local Pt = VRand_In_Box(Mn, Mx, S1);
         #local Norm = <0,0,0>;
         #local Hit = trace(AB,<Pt.x,Mn.y-0.1,Pt.z>,y,Norm);
         #if (vlength(Norm)!=0)
            #local result = true;
         #else
            #local Hit = trace(AB,<Mn.x-0.1,Pt.y,Pt.z>,x,Norm);
            #if (vlength(Norm)!=0)
               #local result = true;
            #else
               #local Hit = trace(AB,<Pt.x,Pt.y,Mn.z-0.1,>,z,Norm);
               #if (vlength(Norm)!=0)
                  #local result = true;
               #end
            #end
         #end
         #local cnt = cnt + 1;
      #end
   #end
   (result)
#end


#declare sample1 = union {
   box {<0,0,0>,<1,1,1>}
   sphere{<0,1,0>,0.1}
   pigment{Red}
};

#declare sample2 = union {
   box {<0,0,0>,<1,1,1>}
   sphere{<1,0,0>,0.1}
   sphere{<0,0,0>,0.1}
   pigment{Blue}
   translate <-1.05,-0.085,0>
};

#local result = collision(sample1 sample2 1000);

object {sample1}
object {sample2}

union {
#switch (result)
   #case (0)
      text {ttf "tahoma.ttf" "Doesn't collide." 0.1,0}
   #break
   #case (1)
      text {ttf "tahoma.ttf" "Collides!" 0.1,0 }
   #break
#end
   scale <0.2,0.2,1>
   translate <-1,-0.5,-1>
   pigment {Orange}
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.