GC Lab 4
GC Lab 4
, FAF-231
Report
Laboratory Work No.4
of Computer Graphics
Checked by:
Chisinau – 2024
In laboratory No. 4, I'm building an ecosystem in Processing.org that uses forces to
create realistic interactions between creatures and their environment. I'll be
incorporating forces like gravity, friction, wind, and resistance (both air and fluid),
all of which will influence how each creature moves and behaves. Each creature
will also have a defined mass, adding a layer of realism, as heavier creatures will
react differently to forces compared to lighter ones.
To make the ecosystem more dynamic, I’ll add elements like food or a predator.
These will trigger attraction or repulsion in the creatures, depending on whether
they’re drawn to or want to avoid the element. I'll designate one type of creature as
an “attractor,” giving it a magnetic-like pull over others, which will allow me to
explore gravitational interactions within the ecosystem. This lab will give me a
chance to dive deeper into how various forces work together to affect movement,
interaction, and overall behavior in a simulated world.
Condition: Incorporate the concept of forces into your ecosystem. Introduce other
elements, such as food or a predator, for the creature to interact with. Consider
whether the creature experiences attraction or repulsion to these elements.
Add to your ecosystem elements such as wind, creature mass, gravity, friction, air
resistance, and fluid resistance. Designate one type of creature as an attractor.
Code:
int numCreatures = 8;
ArrayList<Creature> creatures;
ArrayList<Bubble> bubbles;
ArrayList<LilyPad> lilyPads;
ArrayList<Food> foodItems;
ArrayList<Predator> predators;
PVector wind; // General wind for all creatures except
fish
void setup() {
size(800, 600);
void draw() {
background(85, 170, 230); // Clear, vibrant pond
background
c.applyAirResistance();
c.update();
c.display();
}
}
Creature(float mass) {
location = new PVector(random(width),
random(height));
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
this.mass = mass;
}
void applyGravity(float g) {
applyForce(new PVector(0, g * mass));
}
void applyAirResistance() {
PVector airResistance = velocity.copy();
airResistance.normalize();
airResistance.mult(-0.02 * velocity.magSq()); //
Quadratic resistance
applyForce(airResistance);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0); // Reset acceleration each
frame
LilyPad() {
position = new PVector(random(width),
random(height));
size = random(60, 80);
}
void display() {
fill(60, 180, 75, 220);
ellipse(position.x, position.y, size, size / 1.5);
// Base pad
Bubble() {
location = new PVector(random(width), height +
random(100));
speed = random(0.2, 0.6);
size = random(5, 12);
}
void update() {
location.y -= speed;
if (location.y < -size) {
location.y = height + random(100);
}
}
void display() {
fill(255, 150);
noStroke();
ellipse(location.x, location.y, size, size);
}
}
// Food class
class Food {
PVector position;
Food(float x, float y) {
position = new PVector(x, y);
}
void display() {
fill(255, 215, 0); // Golden color for food
ellipse(position.x, position.y, 10, 10);
}
}
// Predator class
class Predator {
PVector position;
Predator(float x, float y) {
position = new PVector(x, y);
}
void display() {
fill(255, 0, 0); // Red color for predator
ellipse(position.x, position.y, 15, 15);
}
}
Fish(float mass) {
super(mass);
}
void display() {
fill(100, 200, 255);
ellipse(location.x, location.y, 40, 20); // Body
fill(255, 150, 150);
triangle(location.x - 20, location.y, location.x -
30, location.y - 5, location.x - 30, location.y + 5);
// Tail
fill(255);
ellipse(location.x + 10, location.y - 3, 6, 6); //
Eye
}
}
// Frog class
class Frog extends Creature {
Frog(float mass) {
super(mass);
}
void display() {
fill(50, 200, 50);
ellipse(location.x, location.y, 30, 20); // Main
body
fill(0, 150, 0);
ellipse(location.x - 8, location.y + 10, 10, 5);
// Left leg
ellipse(location.x + 8, location.y + 10, 10, 5);
// Right leg
fill(255);
ellipse(location.x + 6, location.y - 10, 6, 6); //
Left eye
ellipse(location.x - 6, location.y - 10, 6, 6); //
Right eye
}
}
void display() {
pushMatrix();
translate(location.x, location.y);
fill(60, 30, 10);
ellipse(0, 0, 6, 12); // Body
fill(255, 165, 0);
noStroke();
ellipse(-8, -5, 15, 20); // Left upper wing
ellipse(8, -5, 15, 20); // Right upper wing
fill(255, 120, 0);
ellipse(-12, -8, 10, 18); // Left outer wing
ellipse(12, -8, 10, 18); // Right outer wing
popMatrix();
}
}
Code Explained:
Creature Classes:
Frog and Butterfly: These creatures experience lighter wind and regular
forces, maintaining slower, natural movements. Butterflies act as
"attractors," pulling other creatures towards them.
Environmental Forces:
Wind: Applied globally as a light force but increased specifically for fish,
making them move noticeably faster.
Interactions:
Classes:
Screen printing:
One of the most interesting aspects of this lab was the fish’s unique response to
wind. By significantly increasing the wind force for the fish, I was able to create a
noticeable drifting effect, where the fish move rapidly across the pond. This
distinction helped bring out each creature’s individuality and showed how
adjusting force magnitudes can change the experience within a simulation. In
contrast, the frogs and butterflies responded more subtly to wind and other forces,
which allowed them to move at a slower, more controlled pace and gave the
ecosystem a sense of balance and visual variety.