help with sprite engine!

Blitz3D Forums/Blitz3D Beginners Area/help with sprite engine!

gellyware(Posted 2003) [#1]
Ok, those who have seen my posts realize ive only been doing this for a week. Enough said, Im stumped with a particle engine I am trying to write. Im in the experimental stage, so some of the variables arent used yet. So far, Im trying to make the sprites travel a certain distance. Even though i have a dist# of 2, the sprites seem to be going forever. Even though I have a sprite creation total of 2 (numSprite), again it seems like infinity of sprites. Here is the code:


Graphics3D 800,600,32,2
SetBuffer BackBuffer()

AmbientLight 255,255,255

camera = CreateCamera()
PositionEntity camera, 0,0,0

Global particle_Img, particle_Spr; particle engine

Type particle

Field ent ;reference created sprite
Field scale# ;sprites scale
Field dist# ;sprites distance to travel
Field speed# ;sprites speed
Field x#, y#, z# ;initial location of sprite

End Type

flag = 1; flag to enable particle creation one time

While Not KeyDown(1)

particleEffects ("fire.bmp",flag, 2, 0.5, 2, 10, 0, 0, 10)
;release some particles

UpdateWorld
RenderWorld

Flip
Wend
End


Function particleEffects (img$, flag, num_Particles,speed#, dist#, scale#, x#, y#, z#)

;img$ = filename
;flag = first time creating?
;num_particles = number of total particles
;speed# = particles speed
;dist# = distance to travel : scale# = particles scale
;x#,y#,z# = initial x,y,z location

If flag = 1
particle_Img = LoadTexture (img)
particle_Spr = CreateSprite ()
EntityTexture particle_Spr, particle_Img
ScaleEntity particle_Spr, scale#, scale#, scale#
PositionEntity particle_Spr, x#, y#, z#

For i = 1 To num_Particles
particle.particle = New particle
particle\ent = CopyEntity (particle_Spr)
particle\x# = x# : particle\y# = y# : particle\z# = z#
particle\scale# = scale#
particle\dist# = dist#
particle\speed# = speed#
Next

For particle.particle = Each particle

MoveEntity
particle\ent, particle\dist#,particle\dist#, particle\dist#

Next

flag = 0

EndIf

End Function


-----------------------------------------------------
sorry for the mess, I dont know how people post code so well :) the code looks better on my screen :)


semar(Posted 2003) [#2]
Hum...

One thing comes in my mind.

You create a particle with
particle_Spr = CreateSprite () 

so this particle will always be there, because you don't delete it.
Later, you copy this particle with
particle\ent = CopyEntity (particle_Spr) 

Instead, I would do this:
For i = 1 To num_Particles 
particle.particle = New particle 
particle\ent = createsprite() 
EntityTexture particle\ent, particle_Img 
ScaleEntity particle\ent, scale#, scale#, scale# 
PositionEntity particle\ent, x#, y#, z# 


More, if you call the function <particleEffects> at each loop, then you will create always num_Particles particles each time, that's why you will have an *huge* number of particles in a few moment.

I've noticed you use a flag to determine if the function should create the sprites or not; unfortunately, you did not declared that flag as Global, so when you think that inside the function flag is set to 0, in reality you set a local variable to 0, and the scope of that variable is only inside the function; the 'flag' variable that you use in the main loop, will not be affected by the setting done in the function. So, all the time you call that function, the flag value will always be 1, because the function does not change it, since it's not declared as global.
Instead, if you declare flag as a global variable, then your function <particleEffects> will indeed change its value.

Another way to do what you did, is:
(outside the main loop)
Global flag = 1
(inside the main loop)
if flag = 1 then
particleEffects()
flag = 0
endif


Remember also, if you want to delete the sprite, you have to:
- delete each sprite individually
- THEN delete the type element which holds that sprite

Example:
for particle.t_particle = each t_particle
freentity particle\ent ;frees the sprite
delete particle ;delete the type element
next



More, I usually call the type structure and the type pointer with different names:
type T_particle
.
end type
Global particle.T_particle


Hope this has sense for you,
Sergio.


gellyware(Posted 2003) [#3]
Thanks sergio, I will try this tommorrow. Its kinda late here 3:34am. I read over your feedback and it does make sense. Now I have something to work with :) thanks!


_PJ_(Posted 2003) [#4]

Ok, those who have seen my posts realize ive only been doing this for a week.



Dont worry about it. You should see the amount of really simple questions I have asked (and still do)

I have also found, that a lot of more efficient coding given as examples isn't as clear as less efficient, and more spaced-out code. In time, it gets easier to see what's going on.

Compared to me, you're doing really well for one week. I didn't dare approach Types for a couple of months :)


gellyware(Posted 2003) [#5]
Well most of this stuff isnt that hard for me since I used Actionscript for a year or so. Actually I find a lot of similarities, though I think some of blitz code is a bit awkward. In actionScript, you would declare and object like this:
hero = new Object();
hero.x = 20;
hero.y = 40;
hero.lives = 2;

etc....
i kinda like that format better since it is less confusing. When you are doing stuff like this:
For particle.T_particle = each T_particle
particle\x = 20
particle\y = 40
particle\lives = 2
next

anyway. I dont like the slash format and i think the for/each loop was a bit hard to grasp. only because it looks very awkward. I know how to apply it in code now, but it still does not make sense to me. Im not sure if *each T_particle* refers to every single object in that group. For instance:

particle.T_particle = new T_particle
redglow.T_particle = new T_particle
yellowGlow._T_particle = new T_particle

now, When you have all these references to T_Particles, I dont understand the Each loop. Unless, It is bassically saying for Every particle.T_particle in the particle.T_particle reference? Logic tells me this is how its interpreted. But still, awkward.

Ok Im making myself a bit nuts. :)


gellyware(Posted 2003) [#6]
Actually on the type note. I dont understand why its not like this:

particle = new T_particle
particle.x = 50
particle.y = 20


this looks so much cleaner, and particle just references the T_particle type. where the slash in particle\y = something.... is beyond me.