180 likes | 299 Views
This assignment focuses on designing and simulating a set of objects such as germs, ants, or cars using programming concepts like "for" statements and array data structures. Students will be tasked with creating interactive behaviors among objects, such as following a target or wandering, while emphasizing the creation of creative object behaviors without the need for graphical representation. The assignment carries a weightage distribution, including simulation (20%), interaction (30%), and creativity (30%).
E N D
Fundamentals of Programming SM1204 Semester A 2010/2011
Assignment 2 Due date 26 Nov Collection via ACS
Requirements • Design and simulate set of objects (20%) • e.g. germs, ants, cars, • Use of "for" statements and “array” data structures (20%) • With interaction among objects (30%) • Creative object behaviors (30%) • Note that graphic is not important in this assignment
Object Following target
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; }
Example – Simple Following 2 Define and initialization data (arrays) 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 (inti=0; i<n; i++) { angle[i] = random(PI*2); x[i] = random(10, width - 10); y[i] = random(10, height - 10); } }
Example – Simple Following 2 How to draw objects void draw() { background(200); for (inti=0; i<n; i++) { pushMatrix(); translate(x[i],y[i]); rotate(angle[i] - PI/2); triangle(0, 10, -3, 0, 3, 0); popMatrix(); } }
Example – Simple Following 2 How to move object to target location void follow (inti, 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); }
Example – Simple Following 2 How to move object I (put the code in the for loop) First object follow mouse cursor Other object follow the next one float tx, ty; if (i == 0){ tx = mouseX; ty = mouseY; } else { tx = x[i-1]; ty = y[i-1]; } follow(i, tx, ty);
Example – Simple Wandering Define and initialization data (arrays) 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 (inti=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); } }
Example – Simple Wandering How to draw objects void draw() { background(200); stroke(0); for (inti=0; i<n; i++) { pushMatrix(); translate(x[i],y[i]); rotate(angle[i] - PI/2); triangle(0, 10, -3, 0, 3, 0); popMatrix(); } }
Example – Simple Wandering How to move object to target location void follow (inti, 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; } }
Example – Simple Wandering How to move object I (put the code in the for loop) 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);
Example – Simple Avoiding How to avoid other objects (put the code in the draw() function) stroke(150); for (inti=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); } } } }
Example – Simple Flock How to simulate flocks (put the code in the draw() function) stroke(150); for (inti=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); } } } }