1 / 18

Fundamentals of Programming

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%)

tahir
Download Presentation

Fundamentals of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals of Programming SM1204 Semester A 2010/2011

  2. Assignment 2 Due date 26 Nov Collection via ACS

  3. 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

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

  7. 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(); } }

  8. 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); }

  9. 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);

  10. Wandering

  11. 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); } }

  12. 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(); } }

  13. 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; } }

  14. 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);

  15. Avoiding

  16. 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); } } } }

  17. Flock

  18. 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); } } } }

More Related