Creative Computing. Comments on Assignments Everyone did very well Report writing needs more work Jumping scale Update first then draw.

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

Creative Computing. \\ aims By the end of the session you will be able to: 1.Explain the difference between various image file formats 2.Load in and display.
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
Flocking and more.  NPC groups can move in cohesive groups not just independently ◦ Meadow of sheep grazing? ◦ Hunting flock of birds? ◦ Ants? Bees?
Lesson Four: More of the Same
Flocks, Herds, and Schools: A Distributed Behavioral Model By: Craig Reynolds Presented by: Stephanie Grosvenor.
OBJECT-ORIENTED THINKING CHAPTER Topics  The Object-Oriented Metaphor  Object-Oriented Flocks of Birds –Boids by Craig W. Reynolds  Modularity.
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
What is iteration? I mean, what is iteration? Seriously, what is iteration?
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Computer Science 1620 Programming & Problem Solving.
1cs426-winter-2008 Notes  Please read: C. Reynolds “Flocks, Herds, and Schools…” SIGGRAPH ‘87
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Dividing Polynomials.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Games and Simulations O-O Programming in Java The Walker School
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.
Classes / Objects An introduction to object-oriented programming.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
© 2012 Adobe Systems Incorporated. All Rights Reserved. Copyright 2012 Adobe Systems Incorporated. All rights reserved. ® INTRODUCTION TO FLASH ANIMATION.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
CIS 3.5 Lecture 2.2 More programming with "Processing"
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
11 Computers, C#, XNA, and You Session 1.1. Session Overview  Find out what computers are all about ...and what makes a great programmer  Discover.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
CSE 190 M Flash Sessions Session 2 Alex Miller, Spring 2011.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Review Objects – data fields – constructors – Methods Classes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Newton’s Lab Games and Simulations O-O Programming in Java.
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
FOP: Multi-Screen Apps
Introduction to Python
Chapter 8 Objects.
Computation as an Expressive Medium
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Organizing Memory in Java
Chapter 10 Algorithms.
Chapter 10 Algorithms.
Game Loop Update & Draw.
Chapter 8 Objects.
Lecture 7: Introduction to Processing
Vectors and Scalars Scalars are quantities which have magnitude only
Chapter 10 Algorithms.
Creating a Simple Game in Scratch
Agenda for Unit 3: Functions
Presentation transcript:

Creative Computing

Comments on Assignments Everyone did very well Report writing needs more work Jumping scale Update first then draw

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

Creative Computing \\ 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?

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

Creative Computing \\ Instructions and data A program consists of 2 things Instructions The commands we give e.g. size(200,200); x = x+y; Data The variables, state of a program e.g. int x = 100;

Creative Computing \\ 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

Creative Computing \\ 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

Creative Computing \\ 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

Creative Computing \\ 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

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

Creative Computing \\ Arrays X[2]

Creative Computing \\ 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

Creative Computing \\ Arrays and Loops int X[] = new int[30]; for (int i = 0; i < X.length; i++) { X[i] = i*3; }

Creative Computing \\ 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?

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

Creative Computing // 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[];

Creative Computing void setup() { size(500, 500); // create all the arrays xPos = new float[ballNumber]; yPos = new float[ballNumber]; xVel = new float[ballNumber]; yVel = new float[ballNumber]; // put data in them // create a random position and velocity for each for (int i = 0; i < ballNumber; i++) { // create a random position on the screen // make sure all positions are at least // ballSize from the edge of the screen xPos[i] = random(ballSize, width -ballSize); yPos[i] = random(ballSize, height-ballSize); // random small velocities between -2 and 2 xVel[i] = random(-2, 2); yVel[i] = random(-2, 2); }

Creative Computing void draw() { background(255); // update all the balls for (int i = 0; i < ballNumber; i++) { // update the position of the ball // by adding on the velocity xPos[i] += xVel[i]; yPos[i] += yVel[i]; // draw the ball strokeWeight(4); ellipse(xPos[i], yPos[i], ballSize, ballSize); }

Creative Computing \\ 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.

Creative Computing \\ 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

Creative Computing \\ 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

Creative Computing \\ 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

Creative Computing // 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;

Creative Computing \\ 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.

Creative Computing // 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;

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

Creative Computing // 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 Ball[ballNumber]; // Add new items to the array for (int i = 0; i < balls.length; i++) { balls[i] = new Ball(); // set up the data for the ball balls[i].Size = 10; // create a random position on the screen // make sure all positions are at least // ballSize from the edge of the screen balls[i].xPos = random(balls[i].Size, width -balls[i].Size); balls[i].yPos = random(balls[i].Size, height-balls[i].Size); // random small velocities between -2 and 2 balls[i].xVel = random(-2, 2); balls[i].yVel = random(-2, 2); }

Creative Computing void draw() { background(255); // update an draw the balls for (int i = 0; i < balls.length; i++) { balls[i].xPos += balls[i].xVel; balls[i].yPos += balls[i].yVel; // draw the ball strokeWeight(4); ellipse(balls[i].xPos, balls[i].yPos, balls[i].Size, balls[i].Size); } }

Creative Computing 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? \\ Exercises

Creative Computing 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 \\ Classes

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

Creative Computing if (i < 100) { // this is one block } else { // this is another } for (int i = 0; i < 100; i++) { // this is a third block }

Creative Computing 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 \\ Functions

Creative Computing int ballx = 10; int bally = 10; // a function void drawBall() { ellipse(ballx, bally, 10, 10); } // another function void setup() { for (int i = 0; i < 100; i++) { ballx += 10; drawBall(); }

Creative Computing 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 \\ Methods

Creative Computing 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() { } }

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

Creative Computing 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 \\ Constructors

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

Creative Computing 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); } }

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

Creative Computing // 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 Ball[ballNumber]; // Add new items to the array for (int i = 0; i < balls.length; i++) { balls[i] = new Ball(); }

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

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

Creative Computing 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 \\ Simulating behaviour

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

Creative Computing 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() { strokeWeight(4); ellipse(xPos, yPos, Size, Size); }

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

Creative Computing void draw() { background(255); // update and draw the balls for (int i = 0; i < balls.length; i++) { balls[i].update(); balls[i].draw(); }

Creative Computing Take your existing code and rewrite it using methods \\ Exercises

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

Creative Computing \\ 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

Creative Computing \\ 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

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

Creative Computing \\ 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

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

Creative Computing \\ 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

Creative Computing \\ Obstacle Avoidance How do we work out the distance Pythagoras Theorem x y h h 2 = x 2 + y 2

Creative Computing \\ Obstacle Avoidance h 2 = x 2 + y 2 Distance h = sqrt(x 2 + y 2 ) Direction x/h, y/h

Creative Computing \\ Obstacle Avoidance dirx = obstacle.posx – ball.posx h = sqrt(dirx*dirx + diry*diry) dirx = dirx/h dirx = disp/h ball.velx = ball.velx – (dirx)/(h*h)

Creative Computing \\ Obstacle Avoidance Lots of other ways of doing it

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

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

Creative Computing 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

Creative Computing 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

Creative Computing Boids Separation Alignment Cohesion

Creative Computing Boids Separation Steer away from flockmates that are very close

Creative Computing Boids Alignment: Match direction to the average direction of nearby flockmates

Creative Computing Boids Cohesion: Move towards the centre of mass of nearby flockmates

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

Creative Computing 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

Creative Computing \\ 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.

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

Creative Computing \\ 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.

Creative Computing \\ 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.

Creative Computing \\ 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

Creative Computing \\ 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)

Creative Computing \\ Vectors v1 v1.x v1.y

Creative Computing \\ Vectors A lot of useful operations on vectors The magnitude of a vector is its length Pythagoras theorem: v.mag() = sqrt(v.x*v.x + v.y*v.y)

Creative Computing \\ 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()

Creative Computing \\ Vectors You subtract one vector from another to get the displacement of one vector from another disp = v1-v2

Creative Computing \\ Vectors v1 v2 v1 – v2

Creative Computing \\ Vectors We can therefore work out the distance between two points by taking magnitude of their displacement disp = v1 – v2 distance = disp.mag()

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

Creative Computing \\ Behaviours We can use vectors to create some more complex behaviours