Electric Effect on Windows Phone 7 using XNApublished on: 4/8/2012 | Views: N/A | Tags: XNA windows-phone
Electric effect, like other effects such as flame, smoke, explosion, is one of the most common visual effects used in game development. However, not too many examples are found, even fewer were proved ready for mobile application. So let's talk about how to "invent" one.
The image below shows the final electric effect implemented on Windows Phone 7 using XNA:
Download the source code here
You can also follow the project on Codeplex.
Abstraction of electric effect
At first, I reached out for Particle System. But after several days of work, I found it's not the right solution for an electric effect. Here's why:
Despite the complexity of a particle system, its core logic is all about spreading particles (sprites, or small glowing textures) in random directions, at random speeds, and sometimes including an environmental force in a fixed direction (gravity). Therefore a particle system is especially appropriate for effects such as flame, smoke, and explosion. There is one significant difference between flame and electric flow: points (particles) in a flame are distributed and move along radial direction, while points in an electric flow are distributed and move along a linear curve.
An electric flow can be abstracted as follows:
Each electric flow consists of a series of points. At any given moment, these points are distributed along a certain curve. The "wriggling" movement of an electric flow can be simulated by choosing a number of control points and moving them continually.
The figure below shows a blue line connecting the starting point and finishing point. The five red points are selected control points.
Try to imagine, at any certain moment, the five control points are located away from the original position by a random amount of distance, like this:
In the figure above, the red lines indicate the actual distribution of all points. Connecting control points using straight lines is the simplest approach, however, it doesn't feel realistic enough. We can improve it by applying different curve algorithms. The figure below shows the result of an electric flow using Catmull Rom function:
Notice: Catmull Rom function is included in XNA's Vector2 class. You can find more information about Catmull Rom algorithm here.
In order to make the effect more vivid, the movement of the control points should be constrained within certain ranges rather than being boundless:
In the figure above, translucent blue areas indicate the range of movement for the control points. Each control point starts from the original (center) position, and moves toward a certain direction (randomly picked at starting point), at a certain speed (also random). When it's going to move out of the range, the control point jumps back to the original position, and starts another round of random movement. By putting all these control points' wriggling movement together, we are able to simulate a realistic electric flow effect.
You can also google for key words "Plasma Ball" and look at the animations, the electric curve bends to some extent and bounces back. That's why we are making the flow jump back from time to time.
The wriggling movement is implemented, but we are not done yet. Take another look at the "Plasma Ball", you will find the electric flow not only wriggles, but also flickers. Here's a simple and quick solution: repeatedly change the opacity of the points. Unfortunately, human eyes are too sharp to trick, any mechanical repeat feels artificial to us. Therefore, we also need to update the points with different glowing point textures, randomly of course. The basic principle is that these textures should be slightly different from each other, otherwise the result can get "messy".
Thanks to the independent movement of the control points, there are always some points that are closer to each other, while others are loosely distributed. Such a diverse density produces smooth transition of luminance. What makes it even more interesting is that, during the wriggling movement, the unevenness of the luminance flows wildly back and forth, amusing our picky eyes.
Source code description
In the demo code, JasonElectricEffect project is a distributable Windows Phone 7 Library, in which:
- ElectricEffect class is used to hold an electric effect, which may include one or more ElectricFlows.
- ElectricFlow class, as its name implies, indicates an electric flow, which consists of a number of control points. These control points are described by ElectricNode class. Notice that though, ElectricNode class is used to describe control points only, not all the glowing points within the electric flow.
- ElectricEffectType enum is used to specify the curve function that connects the control points, including: Line, Bezier and CatmullRom. Different curve functions generate different effects.
- Density property of ElectricEffect class is used to specify the density of the glowing points that fill in between two adjacent control points.
- LifeSpan property of ElectricFlow class is used to specify the interval at which the texture is changed.
For how to quickly generate an electric effect, please refer to InitElectricEffect() method in GamePage.xaml.cs in JasonElectricEffectDemo project.
Download the source code here
You can also follow the project on Codeplex.
posted by: [email protected] on 4/10/2012 12:41:43 PM
I was looking for a lightning effect but this would definitely help. Thanks for a great write up.
Well done, thanks
posted by: Remco on 7/27/2012 2:19:01 AM
Thanks Jason, well done, gives me a good starting point fore some experimentation.
posted by: Quoc Bao on 9/16/2012 2:29:13 PM
Thanks so much! This is really helpful!
Very well done :)
posted by: Jakob "xnafan" Krarup on 1/2/2013 10:02:37 PM
Well written and nicely implemented - kudos! :) What did you use to create the control point illustrations? It's very elegant.
Kind regards - Jakob
posted by: YutApp on 3/16/2013 7:21:30 PM
Thanks so much!!!
How Can This Work Using XNA only?
posted by: 1Dev on 5/22/2013 5:08:25 PM
This is exactly what I'm looking for. Very, very nice. However, how can one get this working in an XNA only project for Windows Phone. Or, how can one add Silverlight to the XNA application to make this work. What would be perfect is an example project for XNA only and one for Silverlight only. I am using Cocos2D-X in my tower defense application. This is the 3rd release into the market and I would love to have some electricity effect to destroy creatures. Any help would be great. Thanks in advance.
Top Windows Phone Development Resources
- Windows 8 Development Guide
- Windows Phone Development Guide
- Windows Phone Toolkit In Depth e-Book
- WindowsPhoneGeek Developer Magazine
- Top Components for Windows Phone and Windows 8 app development
- 400+ Windows Phone Development articles in our Article Index
- PerfecTile, ImageTile Tools for Windows Phone and Windows 8
- Latest Windows Phone Development News & community posts
- Latest Windows 8/ WinRT Development News & comunity posts
- Windows Phone & Windows 8 Development Forums
Our Top Tips & Samples
- What's new in Windows Phone 8 SDK for developers
- Implementing in-app purchasing in Windows Phone 8
- All about Live Tiles in Windows Phone 8
- Send automated Email with attachments in Windows Phone
- All about the new Windows Phone 8 Location APIs
- Creating Spinning progress Animation in Windows Phone
- Getting started with Bluetooth in Windows Phone 8
- The New LongListSelector control in Windows Phone 8 SDK in depth
- Make money from Windows Phone: Paid or Free app, which strategy to choose
- Getting Started with the Coding4Fun toolkit ImageTile Control
- Building cross platform mobile apps with Windows Phone and PhoneGap/Cordova
- Windows Phone Pushpin Custom Tooltip: Different Techniques