ScriptSpot

Forum topic · General Scripting

Rotate intersecting trees

By kuato · 2009-12-08

Description

This is my first undertaking with maxscript, and I'm in over my head. I'm converting a scene for a game which has ~17k trees (2 plane objects). The problem is that the game's engine doesn't handle intersecting trees very well and I get flickering and other z-buffer issues. I mashed together a script that compares two trees and if they collide it rotates one of them on the x-axis. (not very useful when you have this many trees)

 
fn SetRotation obj rx ry rz =
(
    local translateMat = transMatrix obj.transform.pos
     local scaleMat = scaleMatrix obj.transform.scale
    obj.transform = scaleMat * translateMat

    rotate obj (angleaxis rx [1,0,0])
    rotate obj (angleaxis ry [0,1,0])
    rotate obj (angleaxis rz
[0,0,1])
)

------------------------------------------------------
objs = selection as array
for i = 1 to objs.count do
(
for j = i+1 to objs.count do
(
    if (intersects objs[i] objs[j]) then
SetRotation objs[i] 30 0 0 
) 
)

Is there a way to make something like this work if I multiple objects are selected? What I mean is, if I select say 1000 trees, have the script find the colliding trees and then rotate them?

Thanks for helping out a noob.

Comments (1)

Clavery · 2009-12-11

it looks like you got that workin allready... i mean that setRotation function is way beyond me (looks like mathematical voodoo to me)... but otherwise looks good.

all i would say is do the "for each" style... like...


objs = $ as Array
for i in objs do
(

 for j in objs do
 (
  if (i!=j) then
  (
   if (intersects i j) then(SetRotation i 30 0 0)
  )
 )

)

ur version looks like it would work just like that anyways... i sincerely doubt i have helped u at all... but i just had to try.