Lab 3 GC
Lab 3 GC
AL REPUBLICII MOLDOVA
Universitatea Tehnică a Moldovei
Facultatea Calculatoare, Informatică şi Microelectronică
Departamentul Inginerie Software s, i Automatică
REPORT
Laboratory work n.3
of Computer Graphics
Checked by:
Olga Grosu, university assistant
DISA, FCIM, UTM
2. The Condition
Create a simulation of an ecosystem. Imagine a population of computational creatures swimming around a digital
pond, interacting with each other according to various rules.
Develop a set of rules for simulating the real-world behavior of a creature, such as a nervous fly, swimming fish,
hopping bunny, slithering snake, etc. Can you control the object’s motion by only manipulating the acceleration?
Try to give the creature a personality through its behavior (rather than through its visual design).
3 ArrayList<PVector> points;
4 float sunX = 100; // X
5 float sunY = 70; // Y
6 float sunZ = -70; // Z ( )
7 NervousFly nervousFly;
8 Environment environment;
9
10 void setup() {
11 size(800, 600, P3D); // P3D 3D
19 void draw() {
20 background(135, 206, 235); //
21
22 drawSun(); //
23 drawGrass(); //
1
24 drawPath(); //
25 drawPond(); //
26
27 environment.display();
28 drawFlower(); //
29
30 drawAnimal();
31
32 nervousFly.update();
33 nervousFly.checkEdges();
34 nervousFly.display();
35 }
36
37 void drawSun() {
38 // ( )
39 stroke(255, 242, 98);
40 strokeWeight(2);
41 noFill();
42 pushMatrix();
43 translate(sunX, sunY, sunZ); //
44 timedTumble(40); //
45 pointSphereFibonacci(80, 600); //
46 popMatrix();
47 }
48
49 void drawFlower() {
50 //
51 stroke(23, 116, 2);
52 float offsetX = 550;
53 float offsetY = 100;
54
62 float r = random(1);
63 if (r > 0.99) {
64 newPoint.x = 0;
65 newPoint.y = currentPoint.y * 0.16;
66 } else if (r > 0.92) {
67 newPoint.x = currentPoint.x * 0.2 + currentPoint.y * -0.26;
68 newPoint.y = currentPoint.x * 0.23 + currentPoint.y * 0.22 + 1.6;
69 } else if (r > 0.85) {
70 newPoint.x = currentPoint.x * -0.15 + currentPoint.y * 0.28;
2
71 newPoint.y = currentPoint.x * 0.26 + currentPoint.y * 0.24 + 0.44;
72 } else {
73 newPoint.x = currentPoint.x * 0.85 + currentPoint.y * 0.04;
74 newPoint.y = currentPoint.x * -0.04 + currentPoint.y * 0.85 + 1.6;
75 }
76 points.add(newPoint);
77 }
78 }
79
80 //
81 void drawPath() {
82 fill(139, 69, 19); //
83 noStroke();
84
87 beginShape();
88
89 // :
97 // :
105 endShape(CLOSE); //
106 }
107
113 //
114 fill(0, 191, 255); //
115 ellipse(width / 2, height / 1.45, 450, 100); //
3
116 }
117
118
125
126 //// Timed 3D tumble, rotations cycle back to start every ‘duration‘ milliseconds
127 void timedTumble(float duration) {
128 duration = duration * 1000; // milliseconds
129 float spin = TWO_PI * 2 * (millis() % duration) / duration;
130 rotateX(spin);
131 rotateY(spin / 2);
132 rotateZ(spin / 8);
133 }
134
4
163 location.y = yloc;
164 tx += 0.01;
165 ty += 0.01;
166 }
167
173 fill(50); //
174 ellipse(location.x - 4, location.y - 4, 6, 12); //
175 ellipse(location.x + 4, location.y - 4, 6, 12); //
176
177 fill(0); //
178 ellipse(location.x, location.y - 6, 4, 4); //
179 }
180 }
181
191 Mover() {
192 location = new PVector(random(width), random(height));
193 velocity = new PVector(0, 0);
194 acceleration = new PVector(0, 0);
195 topSpeed = 4.0;
196 tx = 0.0;
197 ty = 10000.0;
198 mass = 1.0;
199 G = 0.04;
200 }
201
5
212 } else {
213 return false;
214 }
215 }
216
221 acceleration.add(force);
222 }
223
246
251 Environment() {
252 colorChanger = 0;
253 bubbles = new Bubbles[100];
254 for (int i = 0; i < 100; i++) {
255 float bubbleX = random(width / 2 - 225, width / 2 + 225); //
6
259 }
260
261
271 //
272 class Bubbles {
273 float x, y;
274 float speedX, speedY;
275
292 //
305 //
306 ellipse(412, 315, 25, 25);
7
307 rect(320, 317, 25, 35);
308 rect(345, 317, 25, 35);
309 ellipse(365, 345, 25, 15);
310 ellipse(322, 345, 25, 15);
311
312 //
313 fill(255, 250, 250);
314 ellipse(340, 225, 23, 23);
315 ellipse(364, 225, 23, 23);
316
321 //
322 stroke(0);
323 strokeWeight(4);
324 arc(380, 255, 15, 25, PI / 2, 3 * PI / 2); //
325 arc(354, 252, 11, 20, PI / 2, 3 * PI / 2); //
326 arc(352, 274, 33, 11, 0, PI); //
327
328 //
329 fill(255, 62, 136);
330 ellipse(353, 241, 15, 5);
331 noFill();
332
333 stroke(0);
334 strokeWeight(4);
335 arc(351, 251, 45, 25, 0, PI); //
336
337 //
338 pushMatrix();
339 rotate(radians(-35));
340 noStroke();
341 fill(250, 212, 243);
342 ellipse(214, 367, 50, 30);
343 ellipse(223, 385, 50, 30);
344 popMatrix();
345
346 //
347 stroke(0);
348 strokeWeight(1.2);
349 fill(255, 253, 193);
350 rect(283, 288, 126, 35);
351
352 //
353 stroke(0);
354 strokeWeight(1.4);
355 fill(201, 171, 0);
8
356 rect(282, 309, 137, 15);
357
358 //
359 fill(108, 92, 0);
360 ellipse(349, 299, 8, 11);
361 ellipse(349, 291, 10, 7);
362
363 //
364 fill(250, 212, 243);
365 stroke(0);
366 strokeWeight(0.8);
367 rect(282, 270, 15, 50, 10); //
368 rect(395, 270, 15, 50, 10); //
369
370 //
371 fill(201, 171, 0);
372 rect(320, 323, 25, 15);
373 rect(344, 323, 25, 15);
374 }
9
5. Conclusion
In this lab, I successfully created a simulation of an ecosystem featuring a nervous fly, which exhibits behavior
reminiscent of real-world creatures. The use of the PVector class allowed for effective manipulation of the crea-
ture’s position and movement dynamics through acceleration. The fly’s erratic motion, driven by Perlin noise,
simulates a nervous disposition, making it appear as if it is darting around in response to its environment. Ad-
ditionally, the inclusion of environmental elements, such as the sun, grass, a pond, and bubbles, enhances the
ecosystem’s realism and interactivity.
The simulation illustrates how behavioral rules can be applied to create a dynamic and engaging environment,
allowing the nervous fly to navigate its surroundings while interacting with other elements. This project highlights
the importance of incorporating personality and behavior into computational creatures, transforming the visual
representation into a more lifelike experience. Overall, the lab demonstrates the principles of physics and behavior
in a digital ecosystem, paving the way for more complex simulations in the future.
10