POV-Ray : Newsgroups : povray.binaries.scene-files : Collision detector problem Server Time
2 Sep 2024 02:20:49 EDT (-0400)
  Collision detector problem (Message 1 to 3 of 3)  
From: Jörg 'Yadgar' Bleimann
Subject: Collision detector problem
Date: 6 Jul 2003 17:21:23
Message: <3F08B04A.5FC5BAB8@tiscalinet.de>
High!

Since about two months I'm fiddling around with a PoV script to place
randomly distributed objects on a plane without
overlappings... as long as I restrained myself to spheres, it worked
very well, even with random radii, but as I started to
check random cubes for overlappings, the problems began.

I'm pretty sure that the collision condition stated in lines 112 and 113
is incomplete, as it checks only for the x and z
coordinates of the lower left front corner and the upper right back
corner, whereas the left back and right front corners
should also be taken into consideration.

But regardless of that, the condition even as it is now never becomes
true - the warning statement given in line 114 is never
displayed! What did I wrong?

Clueless,

Yadgar

P. S. Don't bother about all those German comments - I just didn't have
the nerves to translate them:..


Post a reply to this message


Attachments:
Download 'distri_0-04_provi.pov.txt' (6 KB)

From: Mike Williams
Subject: Re: Collision detector problem
Date: 6 Jul 2003 20:40:54
Message: <5KbisAAYFMC$Ewz6@econym.demon.co.uk>
Wasn't it J?rg 'Yadgar' Bleimann who wrote:
>High!
>
>Since about two months I'm fiddling around with a PoV script to place
>randomly distributed objects on a plane without
>overlappings... as long as I restrained myself to spheres, it worked
>very well, even with random radii, but as I started to
>check random cubes for overlappings, the problems began.
>
>I'm pretty sure that the collision condition stated in lines 112 and 113
>is incomplete, as it checks only for the x and z
>coordinates of the lower left front corner and the upper right back
>corner, whereas the left back and right front corners
>should also be taken into consideration.

That's easy to do, you just add the tests for the other two corners

>But regardless of that, the condition even as it is now never becomes
>true - the warning statement given in line 114 is never
>displayed! What did I wrong?

As soon as you find one box that doesn't overlap you #define cflag=0
which causes you to jump out of the loop, so you ended up only testing
against the very first box. The code that was commented out, using a
second cflag2 control variable was nearly right except that it should
have been initially set to 0, not 1.

Here are the relevant lines corrected (German comments removed to
prevent line wrap)

        #declare cflag2=0; 
        #declare b=0;
        #while (b<a & cflag=1)
           #if   ((((objs[b][0]<objs[a][0]) & (objs[a][0]<objs[b][2])) 
                 & ((objs[b][1]<objs[a][1])  & (objs[a][1]<objs[b][3])))
               | (((objs[b][0]<objs[a][2]) & (objs[a][2]<objs[b][2])) 
                 & ((objs[b][1]<objs[a][3]) & (objs[a][3]<objs[b][3])))
               | (((objs[b][0]<objs[a][0]) & (objs[a][0]<objs[b][2])) 
                 & ((objs[b][1]<objs[a][3]) & (objs[a][3]<objs[b][3])))
               | (((objs[b][0]<objs[a][2]) & (objs[a][2]<objs[b][2])) 
                 & ((objs[b][1]<objs[a][1]) & (objs[a][1]<objs[b][3]))))
              #warning "Error! Cubes overlapping!"            
              #declare c=c+1;
              #declare cflag2=1;
           #else
              //#declare cflag=0;
              #declare c=0;
           #end
           #declare b=b+1;
        #end
        #if (cflag2=0)
          #declare cflag=0;
        #end

Unfortunately, this still isn't quite sufficient, because it is possible
for cubes of different sizes to overlap without any corners of [a] being
inside [b]. There's an example in the upper left where a grey box [a]
lies on top of a red box [b].

For as complete check you can do this:-

        #declare cflag2=0; 
        #declare b=0;
        #while (b<a & cflag=1)
           #if   ((objs[b][2]>objs[a][0]) & (objs[b][0]<objs[a][2])
               &  (objs[b][3]>objs[a][1]) & (objs[b][1]<objs[a][3]))
              #warning "Error! Cubes overlapping!"            
              #declare c=c+1;
              #declare cflag2=1;
           #else
              //#declare cflag=0;
              #declare c=0;
           #end
           #declare b=b+1;
        #end
        #if (cflag2=0)
          #declare cflag=0;
        #end


-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Collision detector problem
Date: 7 Jul 2003 00:08:50
Message: <Xns93B13EB70643Dtorolavkhotmailcom@204.213.191.226>

news:3F08B04A.5FC5BAB8@tiscalinet.de:

> High!
> 
> Since about two months I'm fiddling around with a PoV script to place
> randomly distributed objects on a plane without
> overlappings... as long as I restrained myself to spheres, it worked
> very well, even with random radii, but as I started to
> check random cubes for overlappings, the problems began.
> 
> I'm pretty sure that the collision condition stated in lines 112 and 113
> is incomplete, as it checks only for the x and z
> coordinates of the lower left front corner and the upper right back
> corner, whereas the left back and right front corners
> should also be taken into consideration.
> 
> But regardless of that, the condition even as it is now never becomes
> true - the warning statement given in line 114 is never
> displayed! What did I wrong?


Low !

;)


I see that Mike has already replied to you question.

Below is my rewrite of your script (if you're interested).

And further below there is another version which uses POV-
Ray's "built in" 3D-vectors more extensively.


Tor Olav



// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.5;

#include "colors.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#default {
  finish {
     ambient color White
     diffuse 0
  }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NrOfSquares = 150;
#declare MaxErrors = 4000;

#declare FrameSizeX = 3;
#declare FrameSizeZ = 5;

#declare OffsetX = -2;
#declare OffsetY =  0;
#declare OffsetZ =  3;

#declare SizeMin = 0.2;
#declare SizeMax = 0.4;

#declare Seed = seed(400);

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare High = max(FrameSizeX, FrameSizeZ);

#declare SizeScope = SizeMax - SizeMin;
 
#declare Cubes = array[NrOfSquares][4]
#declare CubesHeights = array[NrOfSquares]
   
#declare IminX = 0;
#declare IminZ = 1;
#declare ImaxX = 2;
#declare ImaxZ = 3;

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare ErrorCnt = 0;
#declare TryCnt = 0;

#declare A = 0;
#while (A < NrOfSquares & ErrorCnt < MaxErrors)

  #declare TryCnt = 0;
  #declare Error = true;
  #while (Error & ErrorCnt < MaxErrors)

    #while (Error & ErrorCnt < MaxErrors)
      #declare CubesHeights[A] = SizeMin + rand(Seed)*SizeScope;
      #declare Cubes[A][IminX] = OffsetX + rand(Seed)*FrameSizeX;
      #declare Cubes[A][IminZ] = OffsetZ + rand(Seed)*FrameSizeZ;
      #declare Cubes[A][ImaxX] = Cubes[A][IminX] + CubesHeights[A];
      #declare Cubes[A][ImaxZ] = Cubes[A][IminZ] + CubesHeights[A];
      #if (
            Cubes[A][ImaxX] <= OffsetX + FrameSizeX &
            Cubes[A][ImaxZ] <= OffsetZ + FrameSizeZ)
        #declare Error = false;
      #end // if 
      #declare TryCnt = TryCnt + 1;
/*
      #debug
        concat(
          "\n",
          str(TryCnt, 1, 0), ". attempt to place the ",
          str(A + 1, 1, 0), ". cube. "
        )
*/
      #if (Error)
        #declare ErrorCnt = ErrorCnt + 1;
//        #debug "--> Result: Cube outside frame."
      #end // if
    #end // while

    #if (!Error & A > 0)
      #declare B = 0;
      #while (!Error & B < A)
        #if (
              Cubes[A][IminX] < Cubes[B][ImaxX] &
              Cubes[A][IminZ] < Cubes[B][ImaxZ] &
              Cubes[A][ImaxX] > Cubes[B][IminX] &
              Cubes[A][ImaxZ] > Cubes[B][IminZ])
          #declare Error = true;
        #end // if
        #declare B = B + 1;
      #end // while

      #if (Error)
        #declare ErrorCnt = ErrorCnt + 1;
//        #debug "--> Result: Cubes overlapping."
      #end // if
    #end  // if

  #end // while

/*
  #if (!Error)
    #debug "--> Result: Cube plazed !\n"
  #end // if
*/

  #declare A = A + 1;
#end // while

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NrOfSquares = (Error ? A - 1: A);

#debug "\n"
#debug "\n"
#debug concat ("Number of cubes placed: ", str(NrOfSquares, 0, 0))
#debug "\n"
#debug concat ("Number of errors: ", str(ErrorCnt, 0, 0))
#debug "\n"
#debug "\n"

#declare A = 0;
#while (A < NrOfSquares)
  box {
    <Cubes[A][IminX],               0, Cubes[A][IminZ]>,
    <Cubes[A][ImaxX], CubesHeights[A], Cubes[A][ImaxZ]> 
    pigment { color rgb <rand(Seed), rand(Seed), rand(Seed)> }
  }
  #declare A = A + 1;
#end // while
    
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare vOffset = <OffsetX, OffsetY, OffsetZ>;
                         
#declare W = High/100;
#declare pA = -<W, 0.00, W>/2;
#declare pB =  <W, 0.02, W>/2;

union {
  box { pA, pB + FrameSizeZ*z }
  box { pA, pB + FrameSizeZ*z translate FrameSizeX*x }
  box { pA, pB + FrameSizeX*x }
  box { pA, pB + FrameSizeX*x translate FrameSizeZ*z }
  translate vOffset
  pigment { color Blue }
} 

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

background { color White }

camera {
  orthographic
  location <FrameSizeX, High*4, FrameSizeZ>/2
  look_at <FrameSizeX, 0, FrameSizeZ>/2
  translate vOffset
  angle 40
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7





// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.5;

#include "colors.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#default {
  finish {
     ambient color White
     diffuse 0
  }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NrOfCubes = 400;
#declare MaxErrors = 10000;

#declare vFrameSize = <5, 0, 4>;
#declare pOffset = <-2, 0, 3>;

#declare SizeMin = 0.1;
#declare SizeMax = 0.4;

#declare Seed = seed(314);

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare High = max(vFrameSize.x, vFrameSize.z);

#declare SizeScope = SizeMax - SizeMin;
 
#declare Cubes = array[NrOfCubes][2]

#declare Imin = 0;
#declare Imax = 1;

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare ErrorCnt = 0;
#declare TryCnt = 0;

#declare A = 0;
#while (A < NrOfCubes & ErrorCnt < MaxErrors)

  #declare TryCnt = 0;
  #declare Error = true;
  #while (Error & ErrorCnt < MaxErrors)

    #while (Error & ErrorCnt < MaxErrors)
      #declare Cubes[A][Imin] =
        pOffset + vFrameSize*<rand(Seed), 0, rand(Seed)>;
      #declare Cubes[A][Imax] =
        Cubes[A][Imin] + <1, 1, 1>*(SizeMin + SizeScope*rand(Seed));
      #if (
            Cubes[A][Imax].x <= pOffset.x + vFrameSize.x &
            Cubes[A][Imax].z <= pOffset.z + vFrameSize.z)
        #declare Error = false;
      #end // if 
      #declare TryCnt = TryCnt + 1;
/*
      #debug
        concat(
          "\n",
          str(TryCnt, 1, 0), ". attempt to place the ",
          str(A + 1, 1, 0), ". cube. "
        )
*/
      #if (Error)
        #declare ErrorCnt = ErrorCnt + 1;
//        #debug "--> Result: Cube outside frame."
      #end // if
    #end // while

    #if (!Error & A > 0)
      #declare B = 0;
      #while (!Error & B < A)
        #if (
              Cubes[A][Imin].x < Cubes[B][Imax].x &
              Cubes[A][Imin].z < Cubes[B][Imax].z &
              Cubes[A][Imax].x > Cubes[B][Imin].x &
              Cubes[A][Imax].z > Cubes[B][Imin].z)
          #declare Error = true;
        #end // if
        #declare B = B + 1;
      #end // while

      #if (Error)
        #declare ErrorCnt = ErrorCnt + 1;
//        #debug "--> Result: Cubes overlapping."
      #end // if
    #end  // if

  #end // while

/*
  #if (!Error)
    #debug "--> Result: Cube plazed !\n"
  #end // if
*/

  #declare A = A + 1;
#end // while

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NrOfCubes = (Error ? A - 1: A);

#debug "\n"
#debug "\n"
#debug concat ("Number of cubes placed: ", str(NrOfCubes, 0, 0))
#debug "\n"
#debug concat ("Number of errors: ", str(ErrorCnt, 0, 0))
#debug "\n"
#debug "\n"

#declare A = 0;
#while (A < NrOfCubes)
  box {
    Cubes[A][Imin], Cubes[A][Imax]
    pigment { color rgb <rand(Seed), rand(Seed), rand(Seed)> }
  }
  #declare A = A + 1;
#end // while
    
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare W = High/200;
#declare pA = -<W, 0.00, W>/2;
#declare pB =  <W, 0.02, W>/2;

union {
  box { pA, pB + vFrameSize*z }
  box { pA, pB + vFrameSize*z translate vFrameSize*x }
  box { pA, pB + vFrameSize*x }
  box { pA, pB + vFrameSize*x translate vFrameSize*z }
  translate pOffset
  pigment { color Blue }
} 

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

background { color White }

camera {
  orthographic
  location 2*High*y
  look_at 0*y 
  translate pOffset + vFrameSize*(x + z)/2
  angle 40
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7


Post a reply to this message

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