Setalpha by distance traveled
BlitzMax Forums/BlitzMax Programming/Setalpha by distance traveled
| ||
Hey, i'm trying to make some partical effects, one of them is a partical explosion. Basicly, you create it, at [blank] point using [blank] image, using [blank] amount of particals, and a max travel distance is set (imagine a circle around the start point). Each partical is then assigned a direction randomly generated. Then, this is where I'm stumped, you set the partical alpha according to how much distance it traveled. I have two problems: 1. How to calculate the alpha setting, where 1 (fully solid) being at the beginning point, and 0 (completely transparent) being at the max distance (imaginary circle around the start point) 2. How to calculate how much distance the partical has traveled (i'm having trouble becase it doesnt just travel on one axis. And because it can be going any direction) Here is my code at the moment: Global ParticalList:TList = CreateList() Global ParticalExpList:TList = CreateList() Type TPartical Field x:Int,y:Int,a:Float,r:Int Field d_x:Int,d_y:Int Field img:TImage Field dist_traveled:Int Field ID:String Method Create(_id:String,x:Int,y:Int,_img:TImage,_d_x:Int = 0,_d_y:Int = 0) ListAddLast(ParticalList,Self) x = _x y = _y img = _img ID = _id End Method Method Update() x:+d_x y:+d_y dist_traveled:+(d_x+d_y) SetRotation(r) SetAlpha(a) DrawImage img,x,y SetRotation(0) SetAlpha(1) End Method Method SetAlpha(alpha:Float) a = alpha End Method Method SetRotation(rot:Int) r = rot End Method Method Destroy() ParticalList.Remove(Self) End Method End Type Type TParticalExp Field part_n:Int,part_img:TImage Field dist:Int Field x:Int,y:Int Field d_x:Int,d_y:Int Method Create(_x:Int,_y:Int,_part_img:TImage,_part_n:Int,dist:Int) ListAddLast(ParticalExpList,Self) part_n = _part_n part_img = _part_img x = _x y = _y d_x = Rand(-3,3) d_y = Rand(-3,3) For Local i:Int = 1 To part_n Local part:TPartical = TPartical.Create("pexp",x,y,part_img,d_x,d_y) Next End Method Method Update() For Local i:TPartical = EachIn ParticalList If i.ID = "pexp" If i.dist_traveled > i.SetAlpha(i. i.Update() End If End Type Any help appriciated! |
| ||
first you need to work out the distance: Function distance#(x1, y1, x2, y2) Local x=Abs(x1-x2) Local y=Abs(y1-y2) Return x+y End Function then, calcuate a number from zero to one based on distance particle_alpha#=distance()/maxdistance# make sure all numbers are floats involved in the calculation |
| ||
I'm still not sure i get it, the function seems to calculate the distance between two points... while i'm trying to get the distance an object has traveled in pixels, and bare in mind these objects can be traveling in any direction. (see my code) But that does answer one of my questions: getting the setting of alpha based on the distance between two points |
| ||
x1 and y1 are the x and y coordinates of the particles start point x2 and y2 are where the particle is at the current moment it doesnt matter what direction it travels, the function still works (because of the abs command) |
| ||
alright thanks, i'll mess with it. |
| ||
Right, I've gotten rid of the partical explosion function, and created one type, and a function to create the explosion.. But it isnt working correctly... anyone know why? note the DistanceTraveled() function. In my testing, it doesnt always work, and when it does, it seems to fire a solid yellow block in one direction, and it doesnt fade... I'll see if i can get a working example |
| ||
Alright here's an example i knocked up, just paste 'n compile. click to create a partical explosion with 50 particals and a max distance of 100. But as you can see it doesnt work correctly, what am i doing wrong? |
| ||
Here's one based on your code above, but changed a bit in places. Rather than set a dx,dy using Ints, here we pass in a random direction (0-359), as well as a random speed. The random speed thing gives you a bigger "spread" of particles. It now runs on a max number of frames, rather than distance, which it simply counts down, removing the particle when the count is 0. Fading is based on a range of 1 -> 1/num frames. |
| ||
Thats awsome, much simpler. You continue to amaze me brucey, thanks! |
| ||
Here ist also some old code in wich i tried to generate a Starfield: This ist the Image |
| ||
Cool, Speaking of particles, is there any way to optimise this? I see people talking about making the particles "one surface" or "a single dynamic mesh", how would you do this? And would this increase speed? The reason why iIask is because, on some of my lower end systems, it gets slow around 1500-2000 particals. Alternatively, are there any free particle modules / source code out there? (using images) |
| ||
Single surface, mesh, etc, would help slightly perhaps alot, but....99% of Blitzmax coders are not using it. They just use the built in handling, especially useful when you start porting to other platforms. "The reason why iIask is because, on some of my lower end systems, it gets slow around 1500-2000 particals." That's alot of image rendering, regardless the hardware running. Ask yourself if you need that many, would 200 or so get the job done "good enough"? Also consider creating an image with several particles already on it and then manipulate the render in real time with scaling, rotation, etc. This is a valuable technique alot of pros use to fool the user into thinking there's alot more rendering going on then there actually is. ;) |