0% found this document useful (0 votes)
11 views21 pages

GC Lab 4

Uploaded by

alibabaoleola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

GC Lab 4

Uploaded by

alibabaoleola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

MINISTRY OF EDUCATION, CULTURE AND RESEARCH

OF THE REPUBLIC OF MOLDOVA

Technical University of Moldova

Faculty of Computers, Informatics and Microelectronics

Department of Software and Automation Engineering

, FAF-231

Report
Laboratory Work No.4

of Computer Graphics

Checked by:

Olga Grosu, university assistant

DISA, FCIM, UTM

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

// Initialize a light wind for most creatures


wind = new PVector(0.02, 0);

// Initialize creatures with a mix of different


types
creatures = new ArrayList<Creature>();
for (int i = 0; i < numCreatures; i++) {
if (i % 3 == 0) {
creatures.add(new Fish(random(1, 3))); // Assign
random mass to each fish
} else if (i % 3 == 1) {
creatures.add(new Frog(random(1, 3))); // Assign
random mass to each frog
} else {
creatures.add(new Butterfly(random(0.5, 1))); //
Butterflies are lighter and act as attractors
}
}

// Initialize food items and predators


foodItems = new ArrayList<Food>();
for (int i = 0; i < 3; i++) {
foodItems.add(new Food(random(width),
random(height)));
}

predators = new ArrayList<Predator>();


for (int i = 0; i < 2; i++) {
predators.add(new Predator(random(width),
random(height)));
}

// Initialize ambient bubbles and lily pads


bubbles = new ArrayList<Bubble>();
for (int i = 0; i < 15; i++) {
bubbles.add(new Bubble());
}

lilyPads = new ArrayList<LilyPad>();


for (int i = 0; i < 5; i++) {
lilyPads.add(new LilyPad());
}
}

void draw() {
background(85, 170, 230); // Clear, vibrant pond
background

// Display lily pads


for (LilyPad lp : lilyPads) {
lp.display();
}

// Display bubbles for underwater ambiance


for (Bubble b : bubbles) {
b.update();
b.display();
}

// Display and update food and predator items


for (Food f : foodItems) {
f.display();
}
for (Predator p : predators) {
p.display();
}

// Update and display each creature in the pond,


applying forces
for (Creature c : creatures) {
// Apply environmental forces
if (c instanceof Fish) {
// Much stronger wind force specifically for
fish
PVector strongWind = new PVector(2.5, 0); //
Significantly increased wind effect
c.applyForce(strongWind);
} else {
c.applyForce(wind); // Light wind for other
creatures
}

c.applyGravity(0.02); // Gravity pulls downwards


c.applyFriction(0.01); // Friction slows down
movement

// Apply interaction forces with food and


predators
for (Food f : foodItems) {
c.applyAttraction(f.position); // Attraction to
food
}
for (Predator p : predators) {
c.applyRepulsion(p.position); // Repulsion from
predators
}

// Butterflies attract other creatures


if (c instanceof Butterfly) {
for (Creature other : creatures) {
if (other != c) {
other.applyAttraction(c.location); //
Attract other creatures
}
}
}

c.applyAirResistance();
c.update();
c.display();
}
}

// Base Creature class with mass, force application,


and 2D appearance
abstract class Creature {
PVector location, velocity, acceleration;
float mass;

Creature(float mass) {
location = new PVector(random(width),
random(height));
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
this.mass = mass;
}

void applyForce(PVector force) {


PVector f = PVector.div(force, mass);
acceleration.add(f);
}

void applyGravity(float g) {
applyForce(new PVector(0, g * mass));
}

void applyFriction(float frictionCoefficient) {


PVector friction = velocity.copy();
friction.normalize();
friction.mult(-1);
friction.mult(frictionCoefficient);
applyForce(friction);
}

void applyAirResistance() {
PVector airResistance = velocity.copy();
airResistance.normalize();
airResistance.mult(-0.02 * velocity.magSq()); //
Quadratic resistance
applyForce(airResistance);
}

void applyAttraction(PVector target) {


PVector force = PVector.sub(target, location);
float distance = constrain(force.mag(), 5, 50);
force.normalize();
float strength = (0.5 * mass) / (distance *
distance);
force.mult(strength);
applyForce(force);
}

void applyRepulsion(PVector target) {


PVector force = PVector.sub(location, target);
float distance = constrain(force.mag(), 5, 50);
force.normalize();
float strength = (1.5 * mass) / (distance *
distance); // Stronger than attraction
force.mult(strength);
applyForce(force);
}

void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0); // Reset acceleration each
frame

// Screen wrapping to keep creatures within bounds


if (location.x > width) location.x = 0;
if (location.x < 0) location.x = width;
if (location.y > height) location.y = 0;
if (location.y < 0) location.y = height;
}

abstract void display();


}

// LilyPad class with realistic 2D appearance


class LilyPad {
PVector position;
float size;

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

// Flower with multiple petals


fill(255, 204, 0, 180);
for (int i = 0; i < 6; i++) {
float angle = PI / 3 * i;
float petalX = position.x + cos(angle) * size /
6;
float petalY = position.y + sin(angle) * size /
6;
ellipse(petalX, petalY, size / 8, size / 8);
}
}
}
// Bubble class for underwater ambiance
class Bubble {
PVector location;
float speed;
float size;

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 class with strong wind effect


class Fish extends Creature {

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

// Butterfly class with realistic wings


class Butterfly extends Creature {
Butterfly(float mass) {
super(mass);
}

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:

Fish: Moves rapidly due to an exceptionally strong wind force (PVector(2.5,


0)), creating a fast horizontal drift effect.

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.

Gravity: Pulls all creatures downward, adding weight and realism.

Friction and Air Resistance: Simulate resistance, slowing down creatures


over time.

Interactions:

Attraction: Creatures are attracted to food items, mimicking a goal-oriented


behavior.

Repulsion: Creatures avoid predators to simulate evasive movement.

Classes:

Creature (Abstract): Defines core properties (location, velocity, acceleration,


mass) and methods for force application and movement.

Fish, Frog, Butterfly: Subclasses with unique appearances. Fish has


enhanced wind responsiveness.
LilyPad, Bubble, Food, Predator: Additional pond elements that enhance
visual realism and create interaction points.

Screen printing:

Figure 1. The Pond


Conclusion:

In Lab 4, I successfully enhanced my digital pond ecosystem by introducing


complex interactions and environmental forces, creating a more dynamic and
lifelike environment. Using Processing, I incorporated forces like gravity, wind,
friction, and air resistance, which gave each creature a realistic sense of movement
influenced by natural forces. This approach not only added depth to the simulation
but also allowed me to explore how different forces combine to create distinct,
nuanced behaviors for each creature.

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.

In addition to environmental forces, I introduced interactive elements like Food


and Predators. By making creatures attracted to food, I simulated a natural feeding
behavior, while adding a repulsion from predators created a sense of caution or
self-preservation. This allowed each creature to act in ways that seemed
purposeful, enhancing the lifelike feel of the ecosystem. I also designated
butterflies as attractors, giving other creatures a reason to subtly follow them,
which added another layer of interaction and movement coherence across the pond.
To maintain a continuous and engaging environment, I implemented screen
wrapping, allowing creatures to loop back to the opposite side when they reached
the edges of the screen. This choice kept the ecosystem visually complete and
ensured that no creature would disappear, maintaining an uninterrupted simulation
experience.

Overall, Lab 4 allowed me to strike a balance between realism and visual


engagement in my digital ecosystem. By introducing forces, goal-driven
interactions, and screen wrapping, I was able to create an immersive pond
environment that mimics natural behaviors and dynamics. This lab deepened my
understanding of applying forces in simulations and showed me how to influence
creature behavior to achieve a balanced, interactive digital ecosystem.

You might also like