I put a clip up the other day of the ribbon emitter I was playing with, so here is the code and blog post as promised.
The idea behind the ribbon emitter was to be able to lay down a length of connected particles, normally when we use a regular particle emitter if the velocity of the emitter is quite high you get gaps in the particles emitted rather than the nice particle stream you get at lower velocities. I guess you could compensate for this by lerping from the last particle laid to the emitters current position and then loading the path with particles in-between, but this would add more calculations and vertices to the render, a solution, but not the best.
With the ribbon emitter I have currently written, as the emitter moves I just bind to the end 2 vertices and move those with the emitter, if the emitter changes direction, I add another 4 vertices (a quad) to the end and bind the end two again to the emitter. Here is a clip of the ribbon in wireframe showing the first stages of the ribbon
As you can see, there are just 4 vertices used so far, when the cycles change direction, the old vertices are left behind and 4 new ones are added, and the last two are picked up again
If you run the solution in windows and hit ‘P’ to pause it, zoom up to the selected cycle you can use the keypad keys to rotate and translate the cycle, doing this will result in more of the sections being laid as the cycle does a tight turn like this:
And here is is in wire frame so you can see them better:
It’s really as simple as that, only thing I have not implemented is a life span for each of the sections (quads), but I am sure you don’t want me writing everything for you do you… So, lets look at the code.
I’ll start with the ribbon class, all it is really is a manager for the vertex array, so we have just that, in this case I decided to use the VertexPositionNormalTexture intrinsic XNA vertex element. I then created a method called AddSection, which accepts a Vector3 and a Quaternion for rotation. The new quad will start at the position of the last end vertices in the last quad and the last two of the new quad will be bound to the emitter.
A chunky function, but all it is really doing is adding 4 new vertices to the vertex array each time it’s called, first it copies any old vertices into the old array, extends the vertex array by 4 for the new quad, then sets the position of the two starting points of each quad so it is in the same place as the ends of vertices in the old list, then updates the index array accordingly. Once the new vertex positions are in the array I then calculate their normals and then set their tex coords, then reset the vertex buffer.
The class also has Top and Bottom fields to denote where the leading edge’s top and bottom vertices should be, so in the Update method I ensure that the last two vertices in the array are at the corresponding positions.
Then comes the Draw method which is pretty strait forward really
And that’s the ribbon class.
I created the HUD controls so that on the phone you can control the camera, what cycle you are looking at and so on, this was pretty strait forward to do, first of all creating a set of fields so I can render the sprites to the screen
Then adding in some fields to manage touches and mouse clicks
I also made the spriteBatch in the Game1.cs a public field so I could access it in here too, as well as set up the constructor so that elements got set and ensured the sprite is always drawn last, or has a very high draw order at the very least
When loading the content I calculate the various elements needed for the sprite, click bounds, it’s origin etc.
The update method simply manages the click and touch events
And again a very simple draw call
As you will see I have my regular objects in this solution for the camera and to render a 3D object, so I wont bother going into those :D
In the constructor I just create 4 instances of the ribbon class along with 4 cycles to be used as the emitters, as well as create 4 sets of paths for the cycles so they can go from place to place, then set up the HUD and wire up the events for the buttons and that’s about it…
I feel a TRON game is a MUST do now, just need to find the time to do it :P
I have not applied a texture to the ribbon, but I am sure it’s pretty obvious that it will display an image per quad, and also stretch it as the quad elongates, this too can be compensated for, but this implementation doesn’t.
Down load the source project here