______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA.

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

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Introduction to Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
A Quick Introduction to Processing
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Translation and Rotation in 2D and 3D David Meredith Aalborg University.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2011.
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2011.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
, Fall 2006IAT 800 Lab 2: Polygons, Transformations, and Arrays.
Oct 26, Fall 2006IAT 4101 Collision Detection. Oct 26, Fall 2006IAT 4102 Collision Detection  Essential for many games –Shooting –Kicking, punching,
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Oct 3, Fall 2005Game Design1 Collision Detection.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
FUNDAMENTALS OF PROGRAMMING SM1204 SEMESTER A 2012.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
Processing Lecture.2 Mouse and Keyboard
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Review of ArT3 Programming Course David Meredith Aalborg University
Introduction to Processing CS 4390/5390 Fall 2014 Shirley Moore, Instructor September 3,
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
FUNDAMENTALS OF PROGRAMMING SM1204 SEMESTER A 2012.
B. RAMAMURTHY Processing and Programming cse
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays (Oh My) Micah.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
What is Computing? What can be Programmed? Creative Computing Processing Downloading Processing Dropbox Primitive Shapes – point – line – triangle – quad.
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
Inscribed and Circumscribed Polygons Inscribed n If all of the vertices of a polygon lie on the circle, then the polygon is inscribed.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
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.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
David Meredith Aalborg University
Chapter 14, Translate & Rotate
Polygons, Transformations, and Arrays
Computation as an Expressive Medium
Point-a location on a plane.
For Net Art Lecture 2 J Parker
Chapter 10 Algorithms.
Mouse Inputs in Processing
Chapter 10 Algorithms.
More programming with "Processing"
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
LCC 6310 Computation as an Expressive Medium
Chapter 10 Algorithms.
Computation as an Expressive Medium
IAT 800 Foundations of Computational Art and Design
Lets shape up! Pages:
Creating a Simple Game in Scratch
Lets shape up! Pages:
Presentation transcript:

______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | IAT 265 Lecture 4 Mouse interaction & Collision detection ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |

 Mouse interactions  Polygons  Nested loops  Collision detection  Case study: Collision among Beatles Topics January 5, 2016 IAT 2652

Mouse variables  mouseX and mouseY – variables that automatically contain the current mouse location –pmouseX and pmouseY hold the previous location  mousePressed – boolean variable that is true if a mouse button is down –mouseButton – value is LEFT, RIGHT or CENTER depending on which button is held down January 5, 2016 IAT 2653

void draw() { line(pmouseX, pmouseY, mouseX, mouseY); if(mousePressed) { noLoop(); } Example: Mouse variables January 5, 2016 IAT 2654

Mouse callback methods  There are several built-in methods you can fill in to respond to mouse events mousePressed() mouseReleased() mouseMoved() mouseDragged() Example: void mousePressed() { if( mouseButton == LEFT ){ println( “Left Mouse Button was pressed” ); loop(); // activate drawing again } January 5, 2016 IAT 2655

January 5, 2016 IAT 265 Building Special Shapes  The beginShape() and endShape() functions allow us to draw irregular shapes from any number of points we define  beginShape(MODE) accepts the following arguments for MODE: –POINTS, LINES, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN, QUADS, QUAD_STRIP, POLYGON … 6

January 5, 2016 IAT 265 Building Polygons  beginShape( POLYGON ); –Tells the program to start the polygon  vertex(x, y); –Make as many calls to this as you have vertices in your polygon.  endShape( CLOSE ); –Finishes the shape, connecting the last vertex to the first vertex to close the polygon, then colors it with the current fill() color 7

January 5, 2016 IAT 265 Building Polygons beginShape(); vertex(10, 50); (starts a new polygon, and begins at point (10, 50).) 8

January 5, 2016 IAT 265 Building Polygons vertex(20, 10); vertex(30, 40); vertex(80, 60); vertex(40, 80); (adds 4 more points to the polygon, and connects them in the order they are called.) 9

January 5, 2016 IAT 265 Building Polygons endShape(CLOSE); (connects the last point to the first point, and fills the polygon.) 10

January 5, 2016 IAT 265 Let’s Use Arrays  Let’s store the points that we’re drawing. int[] xvals = {10, 20, 30, 80, 40}; int[] yvals = {50, 10, 40, 60, 80}; beginShape(); for(int i = 0; i < xvals.length; i++) { vertex( xvals[i], yvals[i] ); } endShape(CLOSE); 11

fill(200, 60, 0); beginShape(); curveVertex(84, 91); curveVertex(68, 19); curveVertex(21, 17); curveVertex(32, 95); endShape(); Draw Curves using curveVertex() January 5, 2016 IAT Control points: Curve points

January 5, 2016 IAT 265 Nested Loops  Nesting of loops refers to putting one loop inside the braces of another for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); }  For each i, the inside j loop will run through;  then i will increment;  then the inside j loop will run through again… 13

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Sets i to 10. First step. 14

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Sets j to 10. That’s all for now. 15

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j i = 10, j = 10. Draws a point at (10, 10). 16

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Now we jump to the final statement of inner loop, and increment j by 10, and then check if it’s greater or equal to 100, which it’s not, so we continue. 17

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j i is still 10, but j is now 20. We draw the new point, Then go to loop again. 18

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j We keep looping in this j loop until it passes 100. Then what? 19

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Now that the program has exited from our j loop, It sees that it’s still inside our i loop, and increments i by 10, and then check if it’s <=100, which it’s not, so we continue … 20

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j The program comes to j loop again, so it loops All the way through drawing dots. Only this time, i is 20, so the dots are further to the right. 21

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Again, we keep looping in this j loop until it passes

January 5, 2016 IAT 265 Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j The i loop keeps incrementing in this way, drawing columns of dots, until i exceeds 100. The program then exits the i loop and therefore the nested loops 23

January 5, 2016 IAT 265 Nested Loops  Nested Loops are especially useful for –drawing in grid formations in a 2-D space –Running through a 2-D matrix to process data (e.g. image processing) –Testing among a list of objects for collision detection 24

January 5, 2016 IAT Collision Detection  Essential for many games –Shooting –Kicking, punching, beating, whacking, smashing, hitting, chopping, dicing, slicing, … –Car crashes

January 5, 2016 IAT Collision Detection for Complex Objects  For each object i containing the set of polygons p –Need to test for intersection with object j containing the set of polygons q Use bounding boxes/spheres Hierarchies of bounding boxes/spheres

January 5, 2016 IAT Distance calculation  Approximation: Manhattan distance - Shortest side/2 –Example of approximation:

 dist(x1, y1, x2, y2) –Calculates the distance between two points –Example: draw a circle using the distance of the mouse from the origin void draw() { background(255); float d = dist(0, 0, mouseX, mouseY); ellipse(50, 50, d, d); if(mousePressed) { noLoop(); } } Processing: dist() method January 5, 2016 IAT 26528

 Check the sketch ballCollisions.pde in WebCT: Using dist() for collision detection January 5, 2016 IAT dist(x i, y i, x k, y k ) < d i /2+d k /2 - d i, d k – diameters of the circles circle i circle k (x i, y i ) (x k, y k )

//This loop will loop over every ball so we can draw & move them around for (int i = 0; i < amount; i++) { … //This loop will loop over every ball to check for collisions with any other ball for (int k = 0; k < amount; k++) { if ( dist(x[i], y[i], x[k], y[k]) < (bSize[i]/2 + bSize[k]/2) && i != k) { //We get the angle of ball "k" facing ball "i" float angle = atan2( y[i] - y[k], x[i] - x[k]); //We send ball "i" away in the direction of our angle xVel[i] = 2 * cos(angle); yVel[i] = 2 * sin(angle); //and send ball "k" away in the opposite direction of our angle xVel[k] = 2 * cos(angle - PI); yVel[k] = 2 * sin(angle - PI); } Collision detection among a list of balls January 5, 2016 IAT 26530

Case study: Collision among Beatles January 5, 2016 IAT abs(bugX[i]-bugX[k]) < bSize[i] + bSize[k] bug i bug k As I’m using ellipseMode(CORNER); && abs(bugX[i]-bugX[k]) < bSize[i] + bSize[k]

Kill beetles with mouse January 5, 2016 IAT boolean[] bugAlive = new boolean[count];

… //in setup() for(int i=0; i<bugX.length; i++) { … bugAlive[i] = true; } //in draw() //Loop through the array to draw and move the bugs one by one for(int i=0; i<bugX.length; i++) { if (mousePressed && dist(mouseX, mouseY, bugX[i], bugY[i])<bSize[i]) { changeX[i] = 0; changeY[i] = 0; bugAlive[i]=false; } Track on bugs’ status of being alive (or killed) January 5, 2016 IAT 26533

… //This loop will loop over every bug to check for collision with other bug for (int k = 0; k < count; k++) { if(bugAlive[i] && bugAlive[k] && i != k) { if ( abs(bugX[i]-bugX[k]) < (bSize[i]+bSize[k]) && abs(bugY[i]-bugY[k]) < (bSize[i]+bSize[k])) { … } … //Drawing the bug 'i‘ based on its status (alive or not) if(bugAlive[i]) { //if the bug is alive draw it as a normal bug … } else { //if the bug was killed draw it as a dark red circle fill(60, 0, 0); ellipse(0, 0, bSize[i], bSize[i]); } popMatrix(); } // end of for-loop Use bugAlive for bugs’ collision and drawing January 5, 2016 IAT 26534

 Mouse interactions  Polygons  Nested loops  Collision detection  Case study: Collision among Beatles Summary January 5, 2016 IAT 26535