Dragon with Fire Help

Blitz3D Forums/Blitz3D Beginners Area/Dragon with Fire Help

Jonny(Posted 2008) [#1]
***UPDATE: Still needs help please (see 3 posts down)


Hi,

I am using this code for the animated dragon with fire (based on the demo script - modified slightly to incorporate it in to the game engine that I am using - the engine code calls the 'TestDragonFire' function):

========

Type fire
Field x#
Field y#
Field z#
Field alpha#
Field entity
Field scale#
End Type


Function TestDragonFire(DragonX#, DragonY#, DragonZ#)

SetBuffer BackBuffer()
enable=True
vert=False

dragon = LoadMD2 ("C:\blitz3d\Blitz3D\samples\dragon\model\dragon.md2 ")
tex = LoadTexture("C:\blitz3d\Blitz3D\samples\dragon\model\dragon.bmp")
PositionEntity dragon,DragonX#,DragonY#,DragonZ#

EntityTexture dragon,tex
AnimateMD2 dragon,1,0.05,90,130



;--------------- fire--------------------
pivFire=CreatePivot()

sprite = LoadSprite("C:\blitz3D\Blitz3D\samples\dragon\Fire.bmp ",2,pivFire)
PositionEntity sprite,3,20,43,False
RotateEntity sprite,0,90,0,False
HideEntity sprite
con#=0.5:conx#=0.3:no=5
Md=4
;----------------------------------------
time=MilliSecs()


;-------------- mod of fire------

If Md=1 Then
a1=117:b1=122
con#=1:conx#=0.8:no=5
PositionEntity sprite,3,20,43,False
RotateEntity sprite,0,90,0,False

End If
If Md=2 Then
a1=125 :b1=127
con#=0.4:conx#=0.4:no=1
PositionEntity sprite,-18,10,23,False

End If
If Md=3 Then
a1=100 :b1=102
con#=0.4:conx#=0.3:no=3
PositionEntity sprite,-3,3,33,False

End If
If Md=4 Then
a1=90 :b1=93
con#=1:conx#=1:no=7
PositionEntity sprite,10,15,43,False
RotateEntity sprite,0,75,0,False
End If
;--------------- fire --------------------
u=u+1
If u>360 Then u=1
Repeat
elapsed=MilliSecs()-time
Until elapsed>0

time=time+elapsed
dt#=elapsed*60.0/1000.0
anim=MD2AnimTime(dragon)
If anim>129 Then
Md=4
End If
If anim>93 And anim<100 Then
Md=3
End If
If anim>100 And anim<122 Then
Md=1
End If


If anim>122 And anim<127 Then
Md=2
End If


If anim>a1 And anim<b1
For i=1 To no
f.fire=New fire
f\alpha#=1;Rnd(0,1)

f\scale#=con#*Rnd(8,12)

f\entity=CopyEntity (sprite,piv1)
SpriteViewMode f\entity,1
RotateSprite f\entity,Rnd(360)
num=mum+1
Next
End If

For f.fire=Each fire

f\alpha#=f\alpha#-0.02
;f\scale#=f\scale#-Rnd(0.02,0.1)

If f\alpha>0
EntityAlpha f\entity,f\alpha
If f\alpha#<0.4Then
EntityColor f\entity,80,200,150
f\x#=con#*conx#*3
f\y#=con#*Rnd(2.4,6.4)
f\z#=con#*Rnd(-4.8,4.8)
ElseIf f\alpha#<0.8 And f\alpha#>0.4Then
f\scale#=con#*Rnd(10,14)
f\x#=con#*conx#*4;Rnd(1.9,2)
f\y#=con#*Rnd(-6,6)
f\z#=con#*Rnd(-6,6)
Else
f\scale#=con#*4
f\x#=con#*conx#*Rnd(2,4)
f\y#=con#*Rnd(-2,2)
f\z#=con#*Rnd(-2,2)
EndIf
ScaleSprite f\entity,f\scale#,f\scale#
MoveEntity f\entity,f\x,f\y,f\z
EntityBlend f\entity, 3

Else
FreeEntity f\entity
Delete f
num=num-1
EndIf

Next
;--------------------------------------------
Flip

End Function


======

The dragon appears in-game and animates ok but no fire particles appear.

I'm new to blitz and I fear I may have edited something without realising that is stopping them.

I would appreciate if someone could check the above to see if it is going wrong somewhere.

Many Thanks!


jfk EO-11110(Posted 2008) [#2]
As ar as I see you have the initialisation and the update in the same function, which is a nono. Make one function to init the thing (loading dragon, loading sprites etc. initialize start values of variables) and a second function that will update the fire etc. I think you need to learn this anyway, so I will not show you where exactly the init parts and the update parts of your program are, only one thing:

A program usually loads thing and then branches to an endless loop that may be ended with the Escape Key:

mesh=LoadMEsh("thingie.3ds")

while keydown(1)=0
; update object 1
turnentity mesh,0,1,0 ; (example)

renderworld()
Flip
wend

The fire seems to be some kind of particle engine. This part:

;--------------- fire--------------------
pivFire=CreatePivot()

sprite = LoadSprite("C:\blitz3D\Blitz3D\samples\dragon\Fire.bmp ",2,pivFire)
PositionEntity sprite,3,20,43,False
RotateEntity sprite,0,90,0,False
HideEntity sprite
con#=0.5:conx#=0.3:no=5
Md=4
;----------------------------------------
time=MilliSecs()


is the initialisation of the fire. The rest down to the flip, is the update part that needs a special function that can be called frequently. I wouldn't use the flip there, instead do it this way:

while keydown(1)=0 ; main loop
update_fire()
rednerworld()
flip
wend

See also the timing code, that you may have to move to the main loop.


jfk EO-11110(Posted 2008) [#3]
Uh, me again. Well maybe this is easier: simply take your existing function and add a loop structure to the update part. I don't know if it works, I haven't got the files here, so you try it.

Type fire
Field x#
Field y#
Field z#
Field alpha#
Field entity
Field scale#
End Type


Function TestDragonFire(DragonX#, DragonY#, DragonZ#)

SetBuffer BackBuffer()
enable=True
vert=False

dragon = LoadMD2 ("C:\blitz3d\Blitz3D\samples\dragon\model\dragon.md2 ")
tex = LoadTexture("C:\blitz3d\Blitz3D\samples\dragon\model\dragon.bmp")
PositionEntity dragon,DragonX#,DragonY#,DragonZ#

EntityTexture dragon,tex
AnimateMD2 dragon,1,0.05,90,130



;--------------- fire--------------------
pivFire=CreatePivot()

sprite = LoadSprite("C:\blitz3D\Blitz3D\samples\dragon\Fire.bmp ",2,pivFire)
PositionEntity sprite,3,20,43,False
RotateEntity sprite,0,90,0,False
HideEntity sprite
con#=0.5:conx#=0.3:no=5
Md=4
;----------------------------------------
time=MilliSecs()


while keydown(1)=0  ; main loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<

;-------------- mod of fire------

If Md=1 Then
a1=117:b1=122
con#=1:conx#=0.8:no=5
PositionEntity sprite,3,20,43,False
RotateEntity sprite,0,90,0,False

End If
If Md=2 Then
a1=125 :b1=127
con#=0.4:conx#=0.4:no=1
PositionEntity sprite,-18,10,23,False

End If
If Md=3 Then
a1=100 :b1=102
con#=0.4:conx#=0.3:no=3
PositionEntity sprite,-3,3,33,False

End If
If Md=4 Then
a1=90 :b1=93
con#=1:conx#=1:no=7
PositionEntity sprite,10,15,43,False
RotateEntity sprite,0,75,0,False
End If
;--------------- fire --------------------
u=u+1
If u>360 Then u=1
Repeat
elapsed=MilliSecs()-time
Until elapsed>0

time=time+elapsed
dt#=elapsed*60.0/1000.0
anim=MD2AnimTime(dragon)
If anim>129 Then
Md=4
End If
If anim>93 And anim<100 Then
Md=3
End If
If anim>100 And anim<122 Then
Md=1
End If


If anim>122 And anim<127 Then
Md=2
End If


If anim>a1 And anim<b1
For i=1 To no
f.fire=New fire
f\alpha#=1;Rnd(0,1)

f\scale#=con#*Rnd(8,12)

f\entity=CopyEntity (sprite,piv1)
SpriteViewMode f\entity,1
RotateSprite f\entity,Rnd(360)
num=mum+1
Next
End If

For f.fire=Each fire

f\alpha#=f\alpha#-0.02
;f\scale#=f\scale#-Rnd(0.02,0.1)

If f\alpha>0
EntityAlpha f\entity,f\alpha
If f\alpha#<0.4Then
EntityColor f\entity,80,200,150
f\x#=con#*conx#*3
f\y#=con#*Rnd(2.4,6.4)
f\z#=con#*Rnd(-4.8,4.8)
ElseIf f\alpha#<0.8 And f\alpha#>0.4Then
f\scale#=con#*Rnd(10,14)
f\x#=con#*conx#*4;Rnd(1.9,2)
f\y#=con#*Rnd(-6,6)
f\z#=con#*Rnd(-6,6)
Else
f\scale#=con#*4
f\x#=con#*conx#*Rnd(2,4)
f\y#=con#*Rnd(-2,2)
f\z#=con#*Rnd(-2,2)
EndIf
ScaleSprite f\entity,f\scale#,f\scale#
MoveEntity f\entity,f\x,f\y,f\z
EntityBlend f\entity, 3

Else
FreeEntity f\entity
Delete f
num=num-1
EndIf

Next
;--------------------------------------------
updateworld()
renderworld()
Flip
wend  ; end of main loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<

End Function




Jonny(Posted 2008) [#4]
Hi,

I tried the loop idea but that didn't seem to work so I followed your first advice about the update function:



It's now in two functions (so I removed the 'SetBuffer' and 'Flip' and the update function is called as part of the main update loop.

The dragon appears and animates properly still and the fire does appear for a few seconds (though not in the right place - it comes out the wrong end of the dragon!) but then doesn't appear again, even though it should at the right animation frames.

I checked that all the 'MoveEntity' and 'RotateEntitiy' commands have their last parameter set to 'false' so their positions are relative to their parent.

In the demo scene with the dragon and fire, the fire appears correctly, in the right place and at the right times and I haven't edited any of the position/rotation variables.

-Jonny.