Download presentation
Presentation is loading. Please wait.
1
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2010/2011
2
Due date 26 Nov Collection via ACS Assignment 2
3
Requirements o Design and simulate set of objects (20%) o e.g. germs, ants, cars, o Use of "for" statements and “array” data structures (20%) o With interaction among objects (30%) o Creative object behaviors (30%) o Note that graphic is not important in this assignment
4
Object Following target
5
Example – Simple Following float a = 0; float cx = 80; float cy = 60; void setup() { smooth(); } void draw() { background(200); translate(cx,cy); rotate(a - PI/2); triangle(0, 10, -3, 0, 3, 0); float dx = mouseX - cx; float dy = mouseY - cy; a = atan2(dy, dx); cx += dx * 0.05; cy += dy * 0.05; }
6
Example – Simple Following 2 int n = 10; float[] angle = new float[n]; float[] x = new float[n]; float[] y = new float[n]; void setup() { smooth(); size(200, 200); for (int i=0; i<n; i++) { angle[i] = random(PI*2); x[i] = random(10, width - 10); y[i] = random(10, height - 10); } Define and initialization data (arrays)
7
Example – Simple Following 2 void draw() { background(200); for (int i=0; i<n; i++) { pushMatrix(); translate(x[i],y[i]); rotate(angle[i] - PI/2); triangle(0, 10, -3, 0, 3, 0); popMatrix(); } How to draw objects
8
Example – Simple Following 2 void follow (int i, float targetX, float targetY) { float dx = targetX - x[i]; float dy = targetY - y[i]; x[i] += dx * 0.05; y[i] += dy * 0.05; angle[i] = atan2(dy, dx); } How to move object to target location
9
Example – Simple Following 2 float tx, ty; if (i == 0){ tx = mouseX; ty = mouseY; } else { tx = x[i-1]; ty = y[i-1]; } follow(i, tx, ty); How to move object I (put the code in the for loop) First object follow mouse cursor Other object follow the next one
10
Wandering
11
Example – Simple Wandering int n = 20; float[] angle = new float[n]; float[] x = new float[n]; float[] y = new float[n]; float[] tx = new float[n]; float[] ty = new float[n]; void setup() { smooth(); size(200, 200); for (int i=0; i<n; i++) { angle[i] = random(PI*2); x[i] = random(10, width - 10); y[i] = random(10, height - 10); tx[i] = x[i] + random(-4, 4); ty[i] = y[i] + random(-4, 4); } Define and initialization data (arrays)
12
Example – Simple Wandering void draw() { background(200); stroke(0); for (int i=0; i<n; i++) { pushMatrix(); translate(x[i],y[i]); rotate(angle[i] - PI/2); triangle(0, 10, -3, 0, 3, 0); popMatrix(); } How to draw objects
13
Example – Simple Wandering void follow (int i, float targetX, float targetY, float ratio) { float dx = targetX - x[i]; float dy = targetY - y[i]; angle[i] = atan2(dy, dx); x[i] += dx * ratio; y[i] += dy * ratio; if (x[i] < 0) { x[i] = width; } if (x[i] > width) { x[i] = 0; } if (y[i] < 0) { y[i] = height; } if (y[i] > height) { y[i] = 0; } } How to move object to target location
14
Example – Simple Wandering float dx = tx[i] - x[i]; float dy = ty[i] - y[i]; float d = dist(tx[i], ty[i], x[i], y[i]); tx[i] = x[i] + (dx / d) * 20 + random(-3, 3); ty[i] = y[i] + (dy / d) * 20 + random(-3, 3); //ellipse(tx[i], ty[i], 4, 4); follow(i, tx[i], ty[i], 0.05); How to move object I (put the code in the for loop)
15
Avoiding
16
Example – Simple Avoiding stroke(150); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i != j) { float d = dist(x[i], y[i], x[j], y[j]); if (d < 30) { line(x[i], y[i], x[j], y[j]); tx[i] += constrain(10.0 / (x[i]-x[j]), -5, 5); ty[i] += constrain(10.0 / (y[i]-y[j]), -5, 5); } How to avoid other objects (put the code in the draw() function)
17
Flock
18
Example – Simple Flock stroke(150); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i != j) { float d = dist(tx[i], ty[i], tx[j], ty[j]); if (d < 20) // if close enough { line(tx[i], ty[i], tx[j], ty[j]); tx[i] += constrain(10.0 / (tx[i]-tx[j]), -5, 5); ty[i] += constrain(10.0 / (ty[i]-ty[j]), -5, 5); } How to simulate flocks (put the code in the draw() function)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.