Comments on Assignments - PowerPoint PPT Presentation

About This Presentation
Title:

Comments on Assignments

Description:

Embed data and code in a class. Create basic behaviours of multiple objects moving and ... Want to be able to move around without bumping into obstacles ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 87
Provided by: mas0
Category:

less

Transcript and Presenter's Notes

Title: Comments on Assignments


1
(No Transcript)
2
Comments on Assignments
  • Everyone did very well
  • Report writing needs more work
  • Jumping scale
  • Update first then draw

3
\\ aims
  • By the end of the session you will be able to
  • Use Loops and Arrays to control the behaviour of
    multiple objects
  • Create a class
  • Embed data and code in a class
  • Create basic behaviours of multiple objects
    moving and interaction with each other

4
\\ Objects and Behaviour
  • Lots of our programs involve things that move
    around and interact
  • Often we want lots of these things
  • How do we handle many interacting objects?

5
\\ Loops and arrays
  • The answer is with loops and arrays
  • Gone over last week as well as in java

6
\\ Instructions and data
  • A program consists of 2 things
  • Instructions
  • The commands we give
  • e.g. size(200,200) x xy
  • Data
  • The variables, state of a program
  • e.g. int x 100

7
\\ Instructions and data
  • Instructions are typed into a program in a
    sequence from top to bottom
  • They are executed one by one
  • But the real power comes when you change the order

8
\\ Instructions and data
  • Instructions are typed into a program in a
    sequence from top to bottom
  • They are executed one by one
  • But the real power comes when you change the
    order
  • If statements select particular commands to
    execute

9
\\ Instructions and data
  • Instructions are typed into a program in a
    sequence from top to bottom
  • They are executed one by one
  • But the real power comes when you change the
    order
  • Loops execute commands multiple times

10
\\ Loops
  • Loops let you do a set of commands many times
    while only typing them once
  • More importantly, you dont need to know how many
    times until you get to the loop

11
\\ Arrays
  • You also want to do the same thing with data
  • Have many repeated data item
  • Give them a single name
  • Arrays

12
\\ Arrays
X2
0
1
2
3
4
13
\\ Arrays and Loops
  • Arrays and Loops go very well together
  • There isnt much use to having arrays unless you
    can step through them and do stuff to each
    element
  • Loops do just that

14
\\ Arrays and Loops
  • int X new int30
  • for (int i 0 i lt X.length i)
  • Xi i3

15
\\ Objects and Behaviour
  • Lots of our programs involve things that move
    around and interact
  • Often we want lots of these things
  • How do we handle many interacting objects?

16
\\ Objects and Behaviour
  • You can use Arrays to store data about our
    objects
  • e.g. position, velocity
  • Loops to create their behaviour

17
  • // the number of balls
  • int ballNumber 20
  • // The data we need to know about balls
  • // This is the "State" of the program
  • // As we have multiple balls we need to hold
    their state
  • // we use a set of array with a posiion for each
    ball
  • // the size of a ball
  • int ballSize 10
  • // the x and y position
  • float xPos, yPos
  • // the velocity
  • float xVel, yVel

18
  • void setup()
  • size(500, 500)
  • // create all the arrays
  • xPos new floatballNumber
  • yPos new floatballNumber
  • xVel new floatballNumber
  • yVel new floatballNumber
  • // put data in them
  • // create a random position and velocity for
    each
  • for (int i 0 i lt ballNumber i)
  • // create a random position on the screen
  • // make sure all positions are at least
  • // ballSize from the edge of the screen

19
  • void draw()
  • background(255)
  • // update all the balls
  • for (int i 0 i lt ballNumber i)
  • // update the position of the ball
  • // by adding on the velocity
  • xPosi xVeli
  • yPosi yVeli
  • // draw the ball
  • strokeWeight(4)
  • ellipse(xPosi, yPosi, ballSize,
    ballSize)

20
\\ Objects and Behaviour
  • Doesnt seem quite right
  • xPos, xVel etc. are all features of a ball
  • Dont they some how belong together, not in
    separate arrays?
  • Shouldnt we somewhere have something that
    represents a ball?
  • That is where classes come in.

21
\\ Classes
  • Classes are a way of putting data that belong
    together in the same place
  • They can represent real world things like balls
  • You use them to create your own types

22
\\ Classes
  • Rather than having an array of positions and an
    array of velocities
  • You can have an array of balls
  • A ball contains a position and a velocity

23
\\ Classes
  • A class is like a new type (int, float etc.)
  • It represents a generic class like Balls
  • A particular ball is an object, e.g. a variable
    of type Ball

24
  • // create a class to represent a ball
  • class Ball
  • // all the data for the ball is now
  • // stored in the class
  • float xPos, yPos
  • float xVel, yVel
  • float Size
  • Ball ball new Ball()
  • ball.xPos 100

25
\\ new and dot
  • To create an object of a class you use the
    keyword new
  • You can put the result in a variable
  • To access a variable (member) of a class, you use
    a dot .

26
  • // create a class to represent a ball
  • class Ball
  • // all the data for the ball is now
  • // stored in the class
  • float xPos, yPos
  • float xVel, yVel
  • float Size
  • Ball ball new Ball()
  • ball.xPos 100

27
\\ Classes
  • We can now create an array of Ball objects
  • Use them in our simulation instead of individual
    arrays
  • Much tidier

28
  • // Now we only need one array to hold all the
    balls
  • Ball balls
  • void setup()
  • size(500, 500)
  • // create an array of balls
  • balls new BallballNumber
  • // Add new items to the array
  • for (int i 0 i lt balls.length i)
  • ballsi new Ball()
  • // set up the data for the ball
  • ballsi.Size 10

29
  • void draw()
  • background(255)
  • // update an draw the balls
  • for (int i 0 i lt balls.length i)
  • ballsi.xPos ballsi.xVel
  • ballsi.yPos ballsi.yVel
  • // draw the ball
  • strokeWeight(4)
  • ellipse(ballsi.xPos, ballsi.yPos,
    ballsi.Size, ballsi.Size)

30

\\ Exercises
  • Create a class for a ball
  • Using that class create a program where multiple
    balls move around bouncing off the edges of the
    screen
  • Extra can you create more complex behaviour?
  • Extra can you make the balls interact with each
    other?

31

\\ Classes
  • Im still not that happy
  • Weve now put all the data for a ball together
  • But there are also instructions that relate to
    balls
  • e.g. moving and drawing
  • Shouldnt they go in the class as well?
  • Yes Methods

32

\\ Blocks of code
  • Instructions are grouped together into blocks
  • Curly brackets create blocks
  • E.g. Loops, if statements, functions

33
  • if (i lt 100)
  • // this is one block
  • else
  • // this is another
  • for (int i 0 i lt 100 i)
  • // this is a third block

34

\\ Functions
  • You can think of these as block of code that you
    can call in other places
  • A way of being able to reuse blocks of code
  • A bit like a variable allows us to reuse data

35
  • int ballx 10
  • int bally 10
  • // a function
  • void drawBall()
  • ellipse(ballx, bally, 10, 10)
  • // another function
  • void setup()
  • for (int i 0 i lt 100 i)
  • ballx 10
  • drawBall()

36

\\ Methods
  • A method is a function that is attached to a
    class
  • It allows us to put both data and instructions
    together in a single class
  • The behaviour of the class becomes part of the
    class

37
  • class Ball
  • float xPos, yPos, xVel, yVel
  • float Size
  • // a constructor
  • Ball()
  • // Update the position of the ball
  • void update()
  • // draw the ball on screen
  • void draw()

38

\\ Methods
  • You can add whatever methods you like to a class
  • You call methods using the dot . notation just
    like member variables

39

\\ Constructors
  • Classes have special methods called
    constructors
  • They are called when the class is created
  • A bit like a setup method for a class
  • They have the same name as the class

40

\\ Constructors
  • Constructors are used to set up all the data in
    the class
  • Provide initial values to all the variables

41
  • class Ball
  • float xPos, yPos, xVel, yVel
  • float Size
  • // a constructor
  • Ball()
  • // set the value of the size
  • Size 10.0
  • // set up the initial positions to random
    values
  • xPos random(Size, width-Size)
  • yPos random(Size, height-Size)
  • // set up random velocities
  • xVel random(-2, 2)
  • yVel random(-2, 2)

42

\\ Constructors
  • All you need to do now is create an object of the
    class
  • The variables now set themselves up in the
    constructor

43
  • // Now we only need one array to hold all the
    balls
  • Ball balls
  • void setup()
  • size(500, 500)
  • // create an array of balls
  • balls new BallballNumber
  • // Add new items to the array
  • for (int i 0 i lt balls.length i)
  • ballsi new Ball()

44

\\ Simulating behaviour
  • A good way of simulating behaviour is to have a
    set of 3 methods
  • Constructor
  • Update
  • Draw

45

\\ Simulating behaviour
  • A good way of simulating behaviour is to have a
    set of 3 methods
  • Constructor
  • Sets up all the variables
  • Update
  • Draw

46

\\ Simulating behaviour
  • A good way of simulating behaviour is to have a
    set of 3 methods
  • Constructor
  • Update
  • Updates the variables each frame
  • e.g. changes the position
  • Draw

47

\\ Simulating behaviour
  • A good way of simulating behaviour is to have a
    set of 3 methods
  • Constructor
  • Update
  • Draw
  • Draw the object to the screen

48
  • class Ball
  • float xPos, yPos, xVel, yVel
  • float Size
  • // a constructor
  • Ball()
  • // Update the position of the ball
  • void update()
  • xPos xVel
  • yPos yVel
  • // draw the ball on screen
  • void draw()

49

\\ Simulating behaviour
  • Update and draw can then be called from the main
    draw function

50
  • void draw()
  • background(255)
  • // update and draw the balls
  • for (int i 0 i lt balls.length i)
  • ballsi.update()
  • ballsi.draw()

51

\\ Exercises
  • Take your existing code and rewrite it using
    methods

52
\\ aims
  • By the end of the session you will be able to
  • Use Loops and Arrays to control the behaviour of
    multiple objects
  • Create a class
  • Embed data and code in a class
  • Create basic behaviours of multiple objects
    moving and interaction with each other

53
\\ Behaviours
  • Now wed like our objects to do some more
    interesting things
  • Im going to go through a bunch of behaviours
  • Seek
  • Flee
  • Obstacle avoidance

54
\\ Seek
  • Head towards an object
  • Each frame we update the velocity so that we are
    getting closer to a target
  • Add a small factor in the direction of the target

55
\\ Seek
  • How do we work out the direction between two
    objects?
  • Subtract their positions
  • xDir target.ball.xPos
  • yDir target.ball.yPos

56
\\ Seek
  • Subtract the balls position from the targets
    position
  • ball.vel ball.vel (target.pos - ball.pos)
  • This would take us there in one step
  • But dont want it to move so fast, so only use a
    fraction
  • ball.vel ball.vel (target.pos - ball.pos)/c
  • Where c is a constant

57
\\ Flee
  • The opposite of seek
  • Run away from the target
  • ball.vel ball.vel - (target.pos - ball.pos)/c

58
\\ Obstacle Avoidance
  • Want to be able to move around without bumping
    into obstacles
  • Could just flee from all obstacles, but you dont
    want to avoid ones that are very far away
  • Make the change inversely proportional to the
    distance squared

59
\\ Obstacle Avoidance
  • How do we work out the distance
  • Pythagoras Theorem

h
y
x
h2 x2 y2
60
\\ Obstacle Avoidance
  • h2 x2 y2
  • Distance
  • h sqrt(x2 y2)
  • Direction
  • x/h, y/h

61
\\ Obstacle Avoidance
  • dirx obstacle.posx ball.posx
  • h sqrt(dirxdirx dirydiry)
  • dirx dirx/h
  • dirx disp/h
  • ball.velx ball.velx (dirx)/(hh)

62
\\ Obstacle Avoidance
  • Lots of other ways of doing it

63

\\ Exercises
  • Implement Seek behaviour (to the mouse?)
  • Create an obstacle class
  • Get your objects to avoid the obstacles

64
\\ aims
  • By the end of the session you will be able to
  • Use Loops and Arrays to control the behaviour of
    multiple objects
  • Create a class
  • Embed data and code in a class
  • Create basic behaviours of multiple objects
    moving and interaction with each other

65
Craig Reynolds - flocking
  • The first behavioural simulation
  • Simulates the behaviour of flocks of birds
    (biods), schools of fish or herds of animals
  • Extensively used in films and other applications
  • Flocks, herds and schools a distributed
    behavioural model Craig Reynolds SIGGRAPH 1987

66
Boids
  • Craig Reynolds work was an early aspect of the
    artificial life field
  • He observed the behaviour of real flocks of birds
    and tried to figure out rules of their behaviour
  • The resulting rules are surprisingly simple

67
Boids
  • Separation
  • Alignment
  • Cohesion

68
Boids
  • Separation
  • Steer away from flockmates that are very close

69
Boids
  • Alignment
  • Match direction to the average direction of
    nearby flockmates

70
Boids
  • Cohesion
  • Move towards the centre of mass of nearby
    flockmates

71
Boids Action Selection
  • Need some way of combining the behaviours
  • They have a priority
  • Separation
  • Alignment
  • Cohesion
  • But it isnt absolute

72
Craig Reynolds - flocking
  • Emergent Behaviour
  • These simple rules produce surprising results
  • Here is a flock splitting to avoid an obstacle
  • They recombine afterwards, just like a real flock

73
\\ Assignment
  • In this assignment you will be creating a
    flocking simulation
  • 1. (20 )
  • Create a program in which a number of object move
    around the screen with different velocities and
    bounce of the edges of the window. Use a class to
    represent the objects.

74
\\ Assignment
  • 2. (40 )
  • Make your objects flock using Reynoldss 3 rules
    of flocking
  • Separation
  • Alignment
  • Cohesion

75
\\ Assignment
  • 3. (10 )
  • As well as flocking make the objects move towards
    the mouse pointer so that you can control the
    movement of the flock using the mouse.
  • 4. (10 )
  • 10 further marks will be added for creative
    extensions to the program.

76
\\ Assignment
  • 5. (20 )
  • Submit
  • A written report on your implementation,
    explaining what method you used for each stage.
  • 2 images of your program running for each stage,
    before and after moving the mouse.
  • The code files, with comments, explaining each
    part of the code.

77
\\ More classes
  • We can still do more in terms of classes
  • xPos and yPos are both really two parts of a
    position
  • What we call it is a vector

78
\\ Vectors
  • A vector is a pair of (x, y)
  • v (x,y)
  • It is a powerful mathematical tool for
    representing points in space
  • Treat as a single thing
  • e.g.
  • v1 v2 (v1.x v2.x, v1.y v2.y)
  • v1 - v2 (v1.x - v2.x, v1.y - v2.y)

79
\\ Vectors
v1
v1.y
v1.x
80
\\ Vectors
  • A lot of useful operations on vectors
  • The magnitude of a vector is its length
  • Pythagoras theorem
  • v.mag() sqrt(v.xv.x v.yv.y)

81
\\ Vectors
  • The direction of a vector is the same vector but
    with length 1
  • Divide the vector by its magnitude
  • Called normalising
  • v.normalize v/v.mag()

82
\\ Vectors
  • You subtract one vector from another to get the
    displacement of one vector from another
  • disp v1-v2

83
\\ Vectors
v1
v1 v2
v2
84
\\ Vectors
  • We can therefore work out the distance between
    two points by taking magnitude of their
    displacement
  • disp v1 v2
  • distance disp.mag()

85
\\ Vectors
  • Processing has its own built in class for vectors
  • PVector
  • (version 158 and later)

86
\\ Behaviours
  • We can use vectors to create some more complex
    behaviours
Write a Comment
User Comments (0)
About PowerShow.com