Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on 2D Particle Engines. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
Hi, I have followed the tutorial about 2d particle engines on your website, and it works almost perfectly. One thing that I want to know how to do though: I want the particles to fade away as the time goes by. I have spent countless hours trying to find the answer. Another website I found was riemers and his particle engine I can't seem to incorperate into my game. It just doesnt work properly. Can you please show me how I can make the alpha value decrement every update?
Thanks,
Davis
You should be able to make particles fade out by using a modified version of the original color that includes an alpha component that fades from 1 when the particle is first generated to 0 when the particle is about to disappear. In this tutorial, the Particle class has a Color property and a TTL property. You might want to add in another property that keeps track of the original life time of the particle when it was created (perhaps called InitialLife). This way you know what percent of its life the particle has gone through. The percent of life remaining would be something like this: float lifeRemaining = TTL / InitialLife. When you go to draw the particle, instead of using the particle's Color property, use new Color(Color.R, Color.G, Color.B, lifeRemaining * 255); which uses the original color's red, green, and blue values, while setting the alpha value, based on the amount of life left in the particle, compared to the initial amount of life.
Just make sure to set InitialLife correctly at the beginning (InitialLife equal to the original TTL), and something like that should work for you.
You will also need to use SpriteBlendMode.AlphaBlend when you call SpriteBatch.Draw, but I'm thinking that might be the default, so you might not need to actually specify it. Off the top of my head, I don't remember.
Hi - can you please let me know how to apply multiple particle sources on screen at the same time, when I apply to multiple insances of an object it jumps from one to the other, rather than all at the same time?
Thanks.
It depends on how you are doing this, but try adding a second particle engine (which will have its own particle source) rather than a second particle source to the same particle engine.
Technically, though, there is nothing wrong with adding a second particle source to a single particle engine, so something is messed up in the code somewhere. If you can post some of your code, that would be helpful.
Hi, I was making a simple spaceship game where you control the spaceship with your keyboard and do stuff. I've just started learning game programming and I found your site very very good. I used your particle engine code and got the flame behind the spaceship. But the flame should always go down right? I mean the flame should always be perpendicular to the tail of the spaceship. Can you explain the angle parameters a bit and thus help me with how I can achieve that. Lets say I'm trying to achieve something similar to the plane you showed in the screenshot in the part 1 of the tutorial (http://rbwhitaker.wikidot.com/2d-particle-engine-1).
Thanks.
When I say flame, I meant the engine exhaust. :)
Also, going with explaining the angle stuff, how do I rotate my plane in accordance with my keyboard keys. Again, this is similar to your screenshot . If Lets say, my left and right arrow keys will be for rotation and the front and back for movement.I press left, then the plane should rotate left. When I press front/back, the plane should move in the same angle it was pointing to at that instant. Something like that.
I'm trying to do something very similar, but i don't know how to track the sprite position.
I got a rotating sprite:
pastebin DOT com/mTczHyVd
And the particle engine from this tutorial where i need to inform a vector with the position where the particles will be draw, and i want it to be the bottom of the sprite, but i don't know how to keep track of the sprite position to set it as the particle emitter position. Here's the update method:
pastebin DOT com/fEtqGZEe
The emitter position, right now it gets the cursor position.
Note: the images on the links are very small and white (invisible), and are positioned in top left corner of the browser window.
I have used your full examples, but in the main Game file, I get this error:
ParticleEngine2D is a namespace but it's used like a type.
Any clues?
The error means… um… pretty much what it says. ParticleEngine2D is the name of a namespace (a collection of related classes/types) but you're using it as a type (like making a variable with that type, or trying to create an instance of ParticleEngine2D.
Did you create a class called ParticleEngine2D? In my example code, I've created these two types/classes: Particle and ParticleEngine. Aside from whatever else you may have made, that should be all there is. Check your code and make sure you don't have anywhere where you say something like new ParticleEngine2D();
Additionally, while you can name your types anything you want (including ParticleEngine2D) you should always try to name types something different from any namespace you're using, to avoid a naming conflict.
"tweaked" the code here as im using a windows phone and im trying to get the particles to follow the player object
particle.Engine Emitter Location = new Vector2 ( player.Position.X, player.Position.Y ) ;
But nothing will draw on the screen…
Does it work with the base code in the tutorial, without trying to change the particle emitter's location? If it isn't working at all, then it might be a matter of getting the base code right to start. For instance, you may not be telling the particle emitter to generate new particles, or you may not be updating the particles correctly, etc.
Otherwise, I'd try debugging your program and seeing what value is actually being set for the emitter's location. Make sure it's within the window, and that it isn't being hid by something else (like the player object).
For the record, I have not tried this tutorial out on the Windows phone, but I honestly can't imagine that it wouldn't work.
Hi again,
Thanks for your help
Tried what you said and debugged the game and got to see that the particles were indeed being created by the emitter at an X and Y location. Fixed it by rearranging the order of sprites being drawn. Also i dont know if this will happen to anyone else using the windows phone but i found that having spritebatch draw begin and end in the draw methods of particleengine and game class caused problems so i removed them in the particle engine class and all was go.
Also how do you post code on here? wont let me without saying im posting a link :/
Yeah, having a SpriteBatch object that is controlled by multiple classes can get… tricky. You can't start it once it is already on, and you can't stop it and try to draw something else.
Just looking back through the code I've got here, I'm having the particle engine begin and end the drawing, which means you can't call the ParticleEngine's Draw method when you're already between a SpriteBatch's Begin and End calls. If I'm not mistaken, you could have the ParticleEngine class create and use it's own SpriteBatch, but I can also still imagine that having some strange problems.
Again, I don't think that's specifically a windows phone problem. I think it applies to any platform. (If I'm understanding the problem you were seeing correctly.)
You can add code like this:
[[code]]
int a = 3; // Add whatever code in here…
[[/code]]
Alternatively, you can apply a language, though there's no C#. (I'm kind of frustrated with that, especially after all these years. But it's Wikidot's decision, not mine.)
Instead, I always apply Java as the language, since it is pretty close:
[[code type="java"]]
int a = 3;
[[/code]]
So that's how you do it. I'm not 100% sure who has rights to do that. I'll check and see, but I'm 99% sure I've seen other people (even anonymous people) post blocks of code.
Yep. Looks like people should have the ability to post code here, even without an account.
Thanks man
Now that ive got some particle effects going, how do i maybe create maybe another variable linked to the particle engine but producing a different result? Something a bit more smokey? Should i just call another instance of it? and then apply a different png image?
Yeah, that would do it. A different sprite, and possibly tweaking the properties for the particle generation. Smoke tends to rise in a specific direction (or perhaps you're looking for something like exhaust, which won't "rise" per se, but will still move in a particular direction) but again, that should be configurable with the parameters that are already there. Playing with additive blending, or using some sort of transparency with blending (same idea as additive blending, with slightly different options/parameters) might also go a long way. Give it a go and see what you can make happen.
Post preview:
Close preview