hey all, i found this small tutorial on 3dmax how to create trees using code, so i decided to make something simalar using darkbasic pro..
check this out:
//Dynamic Tree growing..
//made by Sergey K(GogetaX)
//Originally i took it one of the 3dmax tutorials.. haha! :)
sync on
sync rate 60
autocam off
move camera -200
move camera up 100
move camera left 150
point camera 0,0,0
move camera up 100
type TAnimObj
ObjNum as Integer
FrameNum as Float
TreeID as Integer
endtype
randomize timer()
dim AnimObj(0) as TAnimObj
CreateDynamicTree(1,0,0,0,40,10,2,20,0,0)
CreateDynamicTree(1,50,0,50,40,10,2,20,0,0)
CreateDynamicTree(1,-50,0,0,40,10,2,20,0,0)
CreateDynamicTree(1,-50,0,50,40,10,2,20,0,0)
do
point camera 0,100,0
move camera left 2
sync
loop
function CreateDynamicTree(TreeID as Integer,X as Integer,Y as Integer,Z as Integer,TreeLife as Integer,StartSize as Integer,EndSize as Integer,NewBranchRate as Byte,OnObj as Integer,LastColor as Integer)
local a as integer
local b as integer
local LastObj as Integer
LastObj = 0
local CurSize as Float
local Radius as Integer
local Color as Integer
color = 0
Radius = 30
for b = 0 to TreeLife
if StartSize = 0 then StartSize = 1
CurSize = ((EndSize * (TreeLife-b)) / (StartSize))*1.0
`CurSize = StartSize + ((EndSize * (TreeLife-b)) / (StartSize))*1.0
dim AnimObj(array count(AnimObj(0))+1) as TAnimObj
a = array count(AnimObj(0))-1
AnimObj(a).ObjNum = FreeObject()
AnimObj(a).TreeID = TreeID
make object cube AnimObj(a).ObjNum,CurSize
if color = 0
if OnObj = 0
position object AnimObj(a).ObjNum,X,Y,Z
Color = rgb(rnd(100)+155,rnd(100)+155,rnd(100)+155)
else
Color = Lastcolor
endif
endif
set object diffuse AnimObj(a).ObjNum,Color
if OnObj <> 0
position object animObj(a).ObjNum,object position x(OnObj),object position y(OnObj),object position z(OnObj)
if SuccessRate(Radius)=1 then move object left animObj(a).ObjNum,CurSize else move object right animObj(a).ObjNum,CurSize
if SuccessRate(Radius)=1 then move object animObj(a).ObjNum,CurSize else move object animObj(a).ObjNum,-CurSize
LastObj = OnObj
`Color = Get color object
`OnObj = 0
endif
if LastObj <> 0
if OnObj = 0 then position object animObj(a).ObjNum,object position x(LastObj),object position y(LastObj),object position z(LastObj)
rotate object animObj(a).ObjNum,object angle x(LastObj),object angle y(LastObj),object angle z(LastObj)
if OnObj = 0 then move object up AnimObj(a).ObjNum,CurSize+(CurSize*0.2)
OnObj = 0
endif
rotate object AnimObj(a).ObjNum,object angle x(AnimObj(a).ObjNum)+(rnd(Radius)-(Radius/2)),object angle y(AnimObj(a).ObjNum)+(rnd(Radius)-(Radius/2)),object angle z(AnimObj(a).ObjNum)+(rnd(Radius)-(Radius/2))
LastObj = AnimObj(a).ObjNum
if SuccessRate(NewBranchRate)=1 and (CurSize <> 0) and (CurSize > EndSize+1)
CreateDynamicTree(TreeID,0,0,0,TreeLife-b,StartSize,EndSize,NewBranchRate,AnimObj(a).ObjNum,Color)
endif
next b
endfunction
function SuccessRate(RateNum as Byte)
if Rnd(99)+1<=RateNum then Exitfunction 1
endfunction 0
function FreeObject()
local a as integer
a = 0
repeat
inc a
until object exist(a)=0
endfunction a
note: it may take some time to load it, it has a lots of calculations on it.
to generate custom trees, here is the function:
CreateDynamicTree(TreeID as Integer,X as Integer,Y as Integer,Z as Integer,TreeLife as Integer,StartSize as Integer,EndSize as Integer,NewBranchRate as Byte,OnObj as Integer,LastColor as Integer)
tell me what you think