On Collision with Trees


As there may be some of you, actually using this tool, I want to provide you with some code for CopperCube, on how to implement fast collision response with the 1200 trees, and make them work in conjunction with Linepicks inside a shooting regime. The generator outputs a file named "treepositions_data.dta". Copy it to the copperlichtdata folder of your CC project. Then add a cube in CC, scale it to the shape of a tree stem and name it "collbox". Then add the following two behaviors to the root scene node. They use a contingent of 20 cube clones that are placed dynamically around the nearest trees.

[code]

// ------------------------------------- before 1st drawing (also check "on reload", or it will work only every other time when run)

ccbSetCopperCubeVariable("tree_c",0);

var conti=20;

ccbSetCopperCubeVariable("tree_conti",conti);

 fetch("copperlichtdata/treepositions_data.dta")

  .then(response => response.text())

  .then( data => {

  // here we can process the data

//  alert(data);

//  alert(data.length);

  const mydata=data.split("\n"); // split at linebreaks

//  alert(mydata[0]); // show 1st line

 mtreex=new Array();

 mtreey=new Array();

 mtreez=new Array();

 mtreer=new Array();

 mtreecoll=new Array();

 var anz=parseInt(mydata[1]); // number of trees

 var collbox = ccbGetSceneNodeFromName("collbox"); 

 for(i=0;i<anz;i++){

  mtreex[i]=parseFloat(mydata[ 2+(i*5) ])-500;

  mtreey[i]=parseFloat(mydata[ 2+(i*5)+1 ]);

  mtreez[i]=parseFloat(mydata[ 2+(i*5)+2 ])-500;

  mtreer[i]=parseFloat(mydata[ 2+(i*5)+3 ]);

 }

 conti=ccbGetCopperCubeVariable("tree_conti");

 for(i=0 ; i<conti ; i++){

  mtreecoll[i]=ccbCloneSceneNode(collbox);

 ccbSetSceneNodeProperty(mtreecoll[i],"Position",i*10,-2500,0 );

 }

 ccbSetCopperCubeVariable("tree_c", anz);

 } );

//------------------------------------- every 500 ms

if(ccbGetCopperCubeVariable("tree_c") != 0){

 var  startPos3D = 

 ccbGetSceneNodeProperty(ccbGetActiveCamera(), "Position");

 var anz=ccbGetCopperCubeVariable("tree_c");

 var maxdist=40;

 var coco=0;

 var conti=ccbGetCopperCubeVariable("tree_conti");

 for(i=0;i<anz;i++){

  var dx=Math.abs(startPos3D.x -  mtreex[i]);

  var dy=Math.abs(startPos3D.y -  mtreey[i]);

  var dz=Math.abs(startPos3D.z -  mtreez[i]);

  var manhattan=(dx+dy+dz);

  if(manhattan<maxdist){

   if(coco<conti){

ccbSetSceneNodeProperty(mtreecoll[coco],"Position",mtreex[i],mtreey[i],mtreez[i]);

   coco++;

  } // from: if we still got contingent boxes to position?

 } // from if tree is near 

// else { } // not near 

 } // next i

} // from if data ready 


[/code]

Get Fancy Procedural Terrain

Comments

Log in with itch.io to leave a comment.

(1 edit)

Not sure whether I mentioned it, but these collision boxes, when set invisible in CopperCube, their collision won't work. So use a fully transparent texture instead.