POV-Ray : Newsgroups : povray.binaries.scene-files : Collision detector problem : Re: Collision detector problem Server Time
2 Sep 2024 00:18:07 EDT (-0400)
  Re: Collision detector problem  
From: Tor Olav Kristensen
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.