Collision handling – week 10

To be very honest, I missed a lecture and was just trying to figure out how to use the basics of matter.js. I know for my final, I probably want something to be detected like a collision so I just played around with the different features such as gravity, collisions, ground or body.

I started off with a very basic program that just had the ground and the circles wherever the user clicked the screen.

I then added some code to identify whenever there was a collision or when they are collided.

if (circle.collided) {
      fill(255, 0, 0); // Red if collided
    } else {
      fill(0, 150,0); // Green if not collided
    }
function collisionEvent(event) {
  let pairs = event.pairs;
  for (let i = 0; i < pairs.length; i++) {
    let bodyA = pairs[i].bodyA;
    let bodyB = pairs[i].bodyB;

    // Check for collisions involving circles
    if ((circles.includes(bodyA) || circles.includes(bodyB)) && !isGroundCollision(bodyA, bodyB)) {
      circles.forEach(circle => {
        if ((circle === bodyA || circle === bodyB) && circle.collided !== true) {
          circle.collided = true; // true for collisions 
        }
      });
    }
  }
}

function isGroundCollision(bodyA, bodyB) {
  return (bodyA === ground || bodyB === ground);
}


function collisionEndEvent(event) {
  let pairs = event.pairs;
  for (let i = 0; i < pairs.length; i++) {
    let bodyA = pairs[i].bodyA;
    let bodyB = pairs[i].bodyB;

    // Find collided bodies and reset their state
    circles.forEach(circle => {
      if (circle === bodyA || circle === bodyB) {
        circle.collided = false; // Reset collided state to false
      }
    });
  }
}

The code above is to show if the circles are touching as before, the ball touching the ground would be considered a collision which I did not want. I needed the code to also traceback and see if the circles don’t touch anymore, and if they do not touch anymore, the ball should be green again.

This was my final code.

 

 

Coding Assignment Week #10 – Program+Reading Reflection

Alien Intelligence By Neil Leach: Reflection

In summary, Neil Leach’s lecture at thE FIU School of Architecture mainly revolves around the topics of what is AI, the future of AI, AI image generation, and predictions around the future of AI and architecture. One key concept Leach continues to shed light on is how AI holds this “kind of” intelligence, and it really is not the same as human intelligence at all, but is superior to our capabilities. Leach also brings up how AI could be invisible, and really all around us but we tend to naturally associate AI to physical or tangible things. One thing that leach brings up that I was thinking about before watching the lecture is how terrifying AI can be. Not because it possess capabilities to harm individuals or cause chaos, but more in the sense that it has overly fascinating and shocking capabilities, and I quote Leach’s words “Anything we can do, AI can do better”.

What is AI? Everything to know about artificial intelligence | ZDNET

Coding Assignment: Description 

In this project I mainly use the Matter.js physics engine to create a program  where geometric shapes with random color. The main components consist of two classes: “Boundary” for creating static rectangular boundaries, and “CustomShape” for generating dynamic square objects with random attributes. The Matter.js engine mainly handles collisions and movements. The setup function initializes the canvas and Matter Engine, creating fixed boundaries within the canvas. The draw function continuously updates and displays the boundaries, shapes, and their interactions.

An Introduction to Matter.js

Beginning Sketches:

Final Program:

Reflections and Future improvements:

For this weeks coding assignment, I struggled a bit because I still find that the matter.js library and implementing it is new to me and I definitely need to practice and work on more programs utilizing it.

If I hopefully manage to improve my skills in using the library, since it can create very creative programs that are aesthetically pleasing, I plan to hopefully integrate it in my final project and see how it would work with other methods such as attraction, fleeing, seeking, etc.

Core Principles: Critical Reflection - Center for the Professional  Education of Teachers

 

 

 

 

 

 

Continue reading “Coding Assignment Week #10 – Program+Reading Reflection”

Week #10 Assignment: The Volcano by Abdelrahman Mallasi

Concept

This project aims to simulate a volcanic eruption using  Matter.js for physics. Particles representing lava are ejected with a mouse press. These particles are subject to gravity and collisions. Each particle changes color upon collision, enhancing the visual feedback of the simulation, visually indicating interactions between particles and their environment. There’s also the ability  for the user to add wind forces by pressing the spacebar.

The environment includes a triangular volcano and a ground set as static bodies in a physics world. Particles are initialized with an initial force applied to mimic the explosiveness of a volcanic eruption.

Embedded Code

Link to Sketch

Highlight of Code

Events.on(engine, 'collisionStart', function(event) {
    event.pairs.forEach(pair => {
 
        const { bodyA, bodyB } = pair;

        if (bodyA.render && bodyA.circleRadius) bodyA.render.fillStyle = color(random(255), random(255), random(255));
        if (bodyB.render && bodyB.circleRadius) bodyB.render.fillStyle = color(random(255), random(255), random(255));
    });
});

The above code is under the setup function and is responsible for the colour changing collisions. Let’s break it down:

  • Events.on(engine, ‘collisionStart’, function(event)): Sets up a ‘listener’ on the physics engine to react when two bodies collide
  • event.pairs.forEach(pair =>…): Iterates over each pair of colliding bodies in the collision event
  • const { bodyA, bodyB } = pair: Extracts the two colliding bodies from each pair.
  • if (bodyA.render && bodyA.circleRadius) bodyA.render.fillStyle = color(random(255), random(255), random(255)): bodyA’s color is changed to a new random color.
  • if (bodyB.render && bodyB.circleRadius) bodyB.render.fillStyle = color(random(255), random(255), random(255)): bodyA’s color is changed to a new random color.

Reflections and Future Additions

  • Working with Matter.js made the project both easier and more difficult to implement. It was quicker to use the predefined physics functions rather than hardcoding them from scratch. However, it was difficult getting used to another workspace with a whole new set of functions and elements.
  • Adding acoustic elements: It would exciting to have each collision create a unique sound, turning the simulation into a symphony of sorts. This auditory layer would  provide a more multi-sensory experience.

Coding assignment #10 – Pendulum

Concept:

Inspired by Einstein’s cradle and the vintage clock’s pendulum, I’ve tried to create a collision system using Matter.js. It involves randomly generated pendula that collide with each other as they fall down. The user has the ability to move the pendula up and down, left and right, to create more collisions using mouse drag.

View post on imgur.com

View post on imgur.com

 

Sketch:

Code Walkthrough:

Repo: https://editor.p5js.org/bdr/sketches/vq6UC-qLu

Mouse constraints: Create a mouse constraint for the user to interact with each pendulum up and down, left and right.

// add mouse interaction
    let mouse = Mouse.create(canvas.elt)
    mouseConstraint = MouseConstraint.create(engine,{
      mouse: mouse
    })
    // add the mouse constraint to the world
    World.add(engine.world,mouseConstraint)
    // add the engine
    Matter.Runner.run(engine)

 

newPendulum() function: Generate a new pendulum body with a random position, create a constraint to suspend the pendulum from a fixed point at the top, and add the pendulum body and its constraint to the world.

function newpendulum() {
    let { Bodies, World, Constraint } = Matter
    let pendulum;
    // create new pendulum
    pendulum = Bodies.circle(random(width),random(height/5)-height/5, 40)
    pendulum.color = "#f9cca5"
    pendulums.push(pendulum)
    pendulum.s = 40
    // add line constraint
    let constraint = Constraint.create({
        pointA: { x: width/2, y: 52 },
        bodyB: pendulum,
        length: random(height/2,height*3/4),
        stiffness: 0.1,
    })
    // push to the world
    constraints.push(constraint)
    World.add(engine.world,[pendulum,constraint])
}

draw(): Generate a new pendulum every 20 frames, up to a maximum of 20 pendulums.

// new pendulum every 20 frames, up to a max of 20
if(frameCount % 20==0 && pendulums.length < 20){
     newpendulum(frameCount)
}
Possible Improvements:
  • Add sound to the collisions to make it more realistic
  • Implement a 3D sphere instead of a 2D circle

Week 10 Assignment

Concept:

While brainstorming, I randomly thought of glow-in-the-dark flubber bouncy balls that I used to play with, which look like this (link):

No.1(넘버원) 야광맨 - 야광 얌체 공 탱탱 볼 4.6cm 1pcs

With the different physics elements we learned in class through Matter.js, I wanted to create these bouncy balls that were changing colors once they collided, as well as applying and changing forces on them so that they will float/bounce against each other when prompted by a certain action.

Process/Highlight:

Initially, I made the background dark and made the balls rather size in size with big spacing between them by setting the following values:

// Add some circles in a square configuration from the center
const numRows = 7;
const numCols = 7;
const circleSize = 20;
const spacing = 1;
  // Draw the circles
  fill(0, 150, 255);
  for (let circle of circles) {
    push();
    translate(circle.position.x, circle.position.y);

    if (circle.colorChanging) {
      fill(random(255), random(255), random(255));
      circle.colorChanging = false; // reset the flag
    }

    ellipse(0, 0, circle.circleRadius * 2); // this is the code i modified to adjust the circle size
    pop();
  }
}

At this point, my sketch looked something like this:

However, while playing around with the values for these code snippets, I accidentally produced a sketch that I was much more pleased with, which I achieved by increasing the size of the circles themselves while decreasing the spacing between them. I also increased the number of circles significantly, and I was pretty happy with the result because it gave an illusion that the “background” is not really a background but is actually composed of multiple circles that eventually crash to the ground; and in the process of them hitting each other, they shine in different colors, which reminded me of the spangles.

There are two elements that I’m particularly proud of, which are:

  • The addition of force via click of a mouse, which was done in this code:
function mousePressed() {
  // Apply a random force to each circle when the mouse is pressed
  for (let circle of circles) {
    let force = Matter.Vector.create(random(-0.05, 0.05), random(-0.1, 0));
    Matter.Body.applyForce(circle, circle.position, force);
  }
}
  • The result of collisions being that the circles will change colors, which can be shown below:
// Collision event handler function
function handleCollision(event) {
  let pairs = event.pairs;

  for (let i = 0; i < pairs.length; i++) {
    let pair = pairs[i];

    // Change color on collision
    if (pair.bodyA.label === "Circle" && pair.bodyB.label === "Circle") {
      pair.bodyA.colorChanging = true;
      pair.bodyB.colorChanging = true;
    }
  }
}

Final Sketch:

(Try clicking the canvas to apply additional force to the circles!)

Reflection:

I had so much fun exploring with Matter.js because it opened up many ways for me to try applying force, gravity, etc. compared to the methods that we’ve already learned so far in the class. Next time, I’d like to experiment with making a game where a specific circle tries to go through an obstacle course with different forces being applied to it!

Week Assignment 10: Embracing Chaos

Inspiration

In the realm of creative coding, chaos often leads to awe-inspiring discoveries. Inspired by the unpredictable beauty of chaotic systems, we embarked on a journey to explore and visualize chaos using the p5.js library, particularly its physics engine. This endeavor aimed not only to unravel the mysteries of chaos but also to appreciate its inherent allure in creative expression.

Challenge





These script tags import p5.js and its addon libraries from the CDN, allowing the code to access functions and utilities provided by these libraries to create interactive visual elements and simulate physics behaviors.

Week 9: Flock System


Inspiration

The allure of motion and interaction in coding has always captivated creative minds. Inspired by the elegance of pursuing entities within a simulated environment, I embarked on a journey to create a scenario where two ships engage in a chase, using the p5.js library to bring this dynamic scenario to life.

Description

In my canvas, I introduce two protagonists: the main ship and the chasing ship. The main ship, stationed at the center, exudes confidence with its robust speed capabilities. On the other hand, the chasing ship, eager and agile, strives to emulate the movements of the main ship.

The code orchestrates this chase by employing principles of physics and steering behaviors. The main ship moves freely while the chasing ship dynamically adjusts its course to follow the main ship’s trajectory. It utilizes the update() method, considering the position of the main ship, and then employs acceleration and steering vectors to mimic its movements.

Challenges

The core challenge was harmonizing the pursuit behavior of the chasing ship. Calculating the optimal steering force to smoothly follow the main ship without overshooting or lagging required precision and fine-tuning. Managing the ship’s speed and acceleration while ensuring a balanced chase added complexity to the code.

Additionally, synchronizing the rotation of the ships to simulate their heading accurately while maintaining a sleek and responsive visual representation demanded meticulous handling of angles and shapes.

Alien Intelligence Reflection Xiaozao

The idea of comparing AI to “alien” is mindblowing. With the development of AI, it is no longer seen as a tool, but rather a creature that is better than humans in some aspects and even taking over humans’ self-given role as the center of evolution. A quote I really liked from the lecture is “AI has hacked the operating system of human intelligence.” In front of AI, we are more doubtful than ever about the most acknowledged “truth” that we have been believing for centuries. For example, what is the meaning of “think”? What is the meaning of “being creative”? I used to believe that even if AI can take over a lot of jobs, the one thing that it can’t replace should be creativity. However, the lecture and the performance of AIs today make us doubt: What is creativity anyway? Even humans themselves are not fully conscious of being creative. So, it is possible that AI is actually redefining the world from a very low-level perspective.

Xiaozao Week10 Assignment

Jellyfish

Link: https://editor.p5js.org/Xiaozao/sketches/XI0c1CLwj

The physics and constraint properties of the matter.js library give us a lot of opportunities to construct an environment with unique properties such as gravity, air friction, force field, and complicated linked objects. Therefore, I wanted to create a jellyfish with flexible tentacles consisting of many nodes linked with constraints, and this jellyfish is floating in a nearly zero-gravity environment.

However, due to limited time, this is still a work in progress and I think it really has a lot of space to improve.

Coding:

The first challenge is how to create a tentacle that is a chain of nodes. I referred to the coding train’s tutorial to create a series of nodes and added a constraint between each pair of them.

for (let i = 0; i < 40; i += 1) {
      let r = 1;
      let new_node = new Circle(200+n+n*i, i, r);
      circles.push(new_node);

      if (i != 0) {
        let constraint_options = {
          bodyA: circles[circles.length - 1].body,
          bodyB: circles[circles.length - 2].body,
          length: 2 + r,
          stiffness: 0.1-i/400,
        };
        

        let constraint = Constraint.create(constraint_options);
        Composite.add(world, constraint);
      }
    }
    
    
    
  }

However, the nodes are moving too vibrantly and they are just bouncing back and forth on the whole canvas. I figured out several ways to slow them down:

/* methods to reduce crazy movements:
1. add r to constraint length, otherwise, the constraint will intend to shrink and cause too vibrant movement
2. decrease stiffness of the constraint
3. decrease gravity scale
4. increase the air friction*/

The second challenge is to connect the leading nodes of all the tentacles to a mutual point: the jellyfish’s head. I used a revolute constraint whose position is updated every frame to be mouse position. However, I don’t know how to “update”, so I could only create a new constraint every frame. I will figure this out in the future!

let base_options = {
    bodyA: circles[0].body,
    pointB: { x:mouseX+n, y:mouseY },
    length: 0,
    stiffness: 0.1,
  };
  base_constraint = Constraint.create(base_options);
  Composite.add(world, base_constraint);
    
  tentacles.push(circles);

At last, I set the gravity scale to zero.

Future improvements:

The first thing is to organize the code, I should create a Tentacle class to better organize the system of objects. Also, I can incorporate the steering force we learned earlier into the movement of the jellyfish head to create a more organic feeling.

Coding Assignment – Week #10

INSPIRATION & CONCEPT

Matter matter, it all around us. What comes to my mind when I think of matter? My childhood games! Ever made those lego towers or stacked building blocks only to knock them off? Or played that pyramid game at the children’s fair where you throw a ball at a stacked pyramid of paper cups? Those were a few of my favourite games and and this assignment is inspired by that. As I was going through matter.js, I was feeling playful and I have tried to capture that in this assignment.

IMPLEMENTATION

I employed the matter.js library to make building blocks – the colorful boxes. They randomly fall at the start on a static platform to kind of mirror the platform in the pyramid game. There is a pendulum that I provided to simulate the action of throwing a ball – only you swing this! The idea is for users to build their own building blocks and destroy them using the swinging ball in this playful simulation (see video below). All the blocks and and the ball is controlled by the user using the mouse.

I added a floor and a right wall so that the blocks do not all spill off screen. The left and above wall are deliberately not there so the the pendulum has room to swing.

Without the boundaries sketch version:


FINAL SKETCH

Link to full code: https://editor.p5js.org/shreyagoel81601/sketches/KnOeUyDwg

FURTHER DEVELOPMENT

Matter.js is an amazing library and has a lot of potential. For this project in particular, it would be nice to experiment with different density of medium instead of the default one and see how the blocks and the ball interact then. If lets say I set the density to be that of glycerine 1.3 g/cc, then I might be harder for the blocks to even sit on each other. I could also go ahead try out different shapes. I deliberately kept squares here because of the building block ideas, but I could try stars, triangles and stuff too.