Simulation

Overview and aims

    • Describe ways in which simulation is used for computer animation
    • Use object-orientation, modularity and encapsulation to build a simulation engine
    • Create animated, interactive simulations in Processing
  • By the end of the lecture you will be able to:

Animation

Creating complex behaviours

Animation is all about creative complex, interesting behaviour and movement, but how can we do it on a computer?

2 ways to do it

Data

Algorithms

Samples Synthesis
Photographs Computer Graphics
Key-framing Simulation
In most creative uses of computing we can divide methods into those where content is based on data (image, sound samples etc.) or those where they are completely computer generated via algorithms (called procedural methods).

In animation procedural methods are often referred to as simulation. A number of objects or characters are controlled by algorithms that simulate their behaviour in the real world.

A common method is to simulate the laws of physics acting on inanimate objects, using a physics engine. We’ll talk about that in the next session.

This session we will talk mostly about simulating the behaviour of characters.

Examples

One of the first uses of simulation for animation was the simulation of flocking birds, called boids, by Craig Reynolds:

http://www.red3d.com/cwr/boids/

This uses 3 simple rules to create complex interactions between characters.

This algorithm is still extensively used in film, games and digital art.

Particle systems are a simple simulation methods that can be used for many natural phenomena such as fire, rain, clouds, etc.

Games are often based on simulation, SimCity is an early example.

The Sims provides a simulation of human behaviour.

The basic principle is that each Sim has a number of drives that control their behaviour, these include hunger, tiredness, sociability. As time goes by without a drive being satified its value increases. The Sims try to satisfy their high value drives by using various objects, e.g. a fridge satifies hunger, a bed satifies tiredness.

Simulation is also used extensively in films, particularly crowd simulations. Many of the algorithms use Reynolds’ boids as a starting point.

Simulation

You would use simulation in situations that that have a number of factors:

Simple rules

The behaviour of your characters/objects can be captured by simple rules or well understood laws (e.g. physics)

Lots of work

It would be a lot of work to do by hand, e.g. lots of small items likes blades of grass

Interactive

The behaviour needs to be highly interactive, respond in complex ways to user interaction.

Physical accuracy

The behaviour needs to Physically correct, or mathematically correct in some other way.

Keyframing

You would use keyframing/hand animation instead when:

Complex/subtle

The behaviour too complex or subtle to express in simple rules (e.g. most human behaviour)

Artistic control

You need a lot artistic control over the details of the behaviour

Expressiveness/Cartoon

The focus is on the expressiveness, or stylization or cartooniness of the animation, not of physical accuracy.

Creating an engine

Problem:

Complexity

solution:

Break up the problem

We need to create complex behaviour, it can seem overwhelming. We can break up the problem into smaller ones.

Object Orientation

Modularity

Encapsulation

Object Orientation

oop

Simulation is perfect for object oriented methods. We can create a class for the characters (“Agents”) we are simulating. The class can include all the variables and code that controls them (in the class’s methods).

Modularity

modularity

Modularity means that you break up complex code into smaller, more manegeable modules. The modules can be very simple, each having a single, simple task. They can combine together to create complex behaviour.

They also mean that code can be re-used, combining together existing modules in new ways

<

There will be more examples below.

Encapsulation

encapsulation

The problem with having a lot of modules is that it can make code even move complex if every module has to take account of what is happening in every other module. The result can hideous spaghetti code

We need to simplify the interaction between all of the modules. We make sure that there is a simple interface between the modules. Their behaviour can be complex but they interact with only one or two variables.

This makes it easy to understand all of the modules, and to change the modules indepedently. If you change the code of a module without changing its interface, you can leave all other modules the same

A simulation engine

Behaviour
Velocity
Movement
Position
Appearance

We can break the simulation engine up into 3 independent modules: behaviour, movement and appearance.

We use encapsulation to make the interfaces between them simple. The movement module’s output is just the position of the agent (and maybe the rotation), which is passed to the appearance module which draws the agent. Similarly, the behaviour module controls the characters only by changing its velocity

This set up means that you can easily change the look of the character without worrying about how it effects the behaviour, and apply the same behaviour modules to lots of different types of agent.

Appearance

Transforms in Processing make it easy to create movement that is independent of appearance. The translate command moves everything that is drawn after it. You can use if to set the position of the character, and then use any drawing commands you want to create its appearance.

If you are not familiar with transforms, read the processing reference for translate. They use Matrices as you will have learnt in your maths class. What do pushMatrix and popMatrix do? If you aren’t sure read up on them in the Processing reference, and try leaving them out of your code to see what happens.

    void appearance()
    {
        pushMatrix();
        translate(position.x, position.y);
 
        //draw whatever you want
 
        popMatrix();
    }

Movement

The movement module is very simple, it just adds the current velocity to the position to get the new position. Remind yourself what adding two vectors does. You need to use the frameRate command in Processing to make sure your sketch runs at a constant frame rate, otherwise the speed of the character will vary with the framerate

  void movement()
  {
    position.add(velocity);
  }

Behaviour

The behaviour module can itself be modular, with lots of different behaviours, each of which affects the velocity separately. You need to think of how you will trade off the different behaviours against eachother. (Reynolds’ papers gives suggestions about this).

  void behaviour()
  {
     seekFood();
     fleePredators();
     avoidObstacles();
  }
PDF    Send article as PDF to