Thursday, 30 November 2017

[Animation Scene] Asteroid Field - Populating the scene with asteroids

Now that I have created 3 different types of asteroid, I need to populate my scene with a lot of the quickly. In order to achieve this, I decided to create a particle emitter. I placed this emitter in the middle of my scene and created it with the settings shown below in order to achieve the correct spread needed from the emitter.




With the emitter created and in place, I then selected my 3 asteroid objects and the particle object created by the emitter and then selected the "Instancer" option in the nParticles menu. This adds the 3 asteroid shapes as particles for this emitter, however the emitter currently just creates instances of the first shape in the list in the exact same angle. We will change this later using code.



In order to make the particle emitter randomly create asteroids based off of our 3 asteroid objects we need to go into the dynamic attributes settings of the asteroid particle shape. Clicking on the "General" option in this menu brings up the menu to add a general attribute to this shape. As shown below, I create an attribute called "asteroidIndex" which will be the array that holds my 3 asteroid shapes



With this attribute added, I can move on to creating an expression. This is essentially some code that will tell the emitter how I want it to create these objects, if I want them to have any special settings on creation and which objects to use as particles for the emitter. Below is a screenshot of the expression I have made. This expression is telling the emitter that for the shape of the particle assigned to it, it should assign a random shape from my newly created "asteroidIndex" between zero and 2.99. This is because the asteroidIndex variable is an array, which has an index starting from zero (and in this instance) going up to 2. In Maya, this value has to be a float, so we set the maximum number to 2.99 to ensure that the code doesn't try and get a value out side of the size of the array, but still maintains a fair ratio of randomness between the 3 objects in the array.



With this code in place, I then assigned it to an instancer under the object index option in general options. This will then make sure that the code is used to select randomly between the 3 asteroid objects. I found that using code in Maya to achieve a task that otherwise would take a very long time manually incredibly effective. This also played into my strengths as a programmer.


Below are the pictures of the resulting asteroid field, now looking much more random and sporadic.


This effect could be better however as all of the asteroids are created at the same size and rotation so I decided to go back into the expression editor and create some more code to randomise these elements. Below is the code that I used in order to randomise both the rotation of each asteroid by a random set of vector 3 coordinates and also to scale each asteroid differently. With the scaling options I had to ensure to make the asteroids scale by a uniformed number (I picked a random number between 1 and 3 each time) so that they didn't scale skewed on each axis (for example, 1 on X axis, 3 on Y axis, 2 on Z axis) and appear squashed or malformed. Below is a picture of the finished asteroid belt effect.



The final thing I did to set the scene was to keyframe the position and scale of the asteroids after my code had created a random spread. I set this keyframe at frame 100 and made sure to stop the emitter from creating any more asteroids at this point. I also made sure that the conserve setting was at 1 for the asteroids, this gave them the effect of slowly floating in space and made them appear less static and more dynamic.


No comments:

Post a Comment