<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #physics - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23physics</link>
      <pubDate>Sun, 08 Aug 2021 19:30:02 +0000</pubDate>
         <description>Tagged with #physics - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23physics/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>2D Physics engine made with Processing</title>
      <link>https://forum.processing.org/two/discussion/25700/2d-physics-engine-made-with-processing</link>
      <pubDate>Sun, 24 Dec 2017 13:41:16 +0000</pubDate>
      <dc:creator>Janglee123</dc:creator>
      <guid isPermaLink="false">25700@/two/discussions</guid>
      <description><![CDATA[<p>I had like to share my own small , simple and not more powerful 2D Physics engine that made in Processing. one year ago I started to learn processing with zero programing experience and started to work on game making to learn programing.</p>

<p>Actually i want to make a game but that require physics . I tried Box2D but i didn't like conversion between coordinates between world and pixels and  less control on body .then i started to make  physics engine and today I want to share my physics engine . it is not powerful as Box2D and need more work to do like to add friction,bounciness,and other things but collision detection and collision response works good . I also want to add continues collision detection for fast objects.</p>

<p>if anyone want to use it . I like to make Documentation for that.</p>

<p>Code : <a rel="nofollow" href="https://github.com/Janglee123/five">Github</a></p>

<p>video : <span class="VideoWrap"><span class="Video YouTube" id="youtube-meTTxhSbdLQ"><span class="VideoPreview"><a href="http://youtube.com/watch?v=meTTxhSbdLQ"><img src="http://img.youtube.com/vi/meTTxhSbdLQ/0.jpg" width="640" height="385" border="0" /></a></span><span class="VideoPlayer"></span></span></span></p>
]]></description>
   </item>
   <item>
      <title>How to simulate a pendulum with just using forces.</title>
      <link>https://forum.processing.org/two/discussion/23547/how-to-simulate-a-pendulum-with-just-using-forces</link>
      <pubDate>Fri, 21 Jul 2017 15:45:58 +0000</pubDate>
      <dc:creator>aizen</dc:creator>
      <guid isPermaLink="false">23547@/two/discussions</guid>
      <description><![CDATA[<p>I know simulating a pendulum can be done by using polar coordinates and angular velocity etc. But I want to do it, by just using the Tension and Gravity forces acting on the 'bob', the resultant of which will 'supposedly' perform the swinging motion. The code I have is as follows:</p>

<p>The Main Class :</p>

<pre><code>Mover bob;
PVector anchor;
float len;
PVector tension;
PVector gravity;

void setup() {
  size(640, 380);
  anchor = new PVector(width/2, 0);
  bob = new Mover();
  gravity = new PVector(0, 0.4);
  tension = new PVector(0, 0);
  len = dist(bob.pos.x, bob.pos.y, anchor.x, anchor.y);
}

void applyForces() {
  tension = PVector.sub(anchor, bob.pos);
  float str = bob.vel.magSq()/len + gravity.mag()*cos(bob.getAngle(anchor));
  tension.setMag(str);
  bob.applyForce(tension);
  bob.applyForce(gravity);

}

void draw() {
  background(50);
  println(dist(bob.pos.x, bob.pos.y, anchor.x, anchor.y));
  applyForces();
  bob.update();
  bob.render();
}
</code></pre>

<p>The Mover class :</p>

<pre><code>class Mover {

  PVector pos;
  PVector vel;
  PVector acc;
  float mass = 1;

  Mover() {
    pos = new PVector(width/2.2, height/4);
    vel = new PVector();
    acc = new PVector();
  }
  float getAngle(PVector anchor) {
    PVector temp1 = PVector.sub(pos, anchor);
    PVector temp2 = PVector.sub(new PVector(anchor.x, height), anchor);
    float angle = PVector.angleBetween(temp1, temp2);

    return angle;
  }
  void applyForce(PVector force) {
    PVector f = PVector.div(force, mass);
    acc.add(f);
  }

  void update() {
    vel.add(acc);
    pos.add(vel);

    acc.mult(0);
  }

  void render() {
    fill(255);
    ellipse(pos.x, pos.y, 30, 30);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Fixed update function?</title>
      <link>https://forum.processing.org/two/discussion/23960/fixed-update-function</link>
      <pubDate>Tue, 29 Aug 2017 11:39:36 +0000</pubDate>
      <dc:creator>theliquu69</dc:creator>
      <guid isPermaLink="false">23960@/two/discussions</guid>
      <description><![CDATA[<p>I want to make a pixel water physics simulation, and I want to add gravity to the water pixels. This is my code so far:</p>

<pre><code>int[][] map;
int mapWidth = 100;
int mapHeight = 100;
int drawScale = 4;

int tick = 0;
int tickRate = 2;

void settings() {
  size(mapWidth*drawScale, mapHeight*drawScale);
}

void setup() {
  frameRate(240);
  surface.setTitle("Pixel Fluid");
  map = new int[mapWidth][mapHeight];

  for (int i = 0; i &lt; mapWidth; i++) {
    for (int j = 0; j &lt; mapHeight; j++) {
      setMap(i, j, 0);
    }
  }
  thread("physics");
}

void draw() {
  tick++;
  surface.setTitle("Pixel Fluid | " + frameRate + " " + tick % tickRate);
  for (int i = 0; i &lt; mapWidth-1; i++) {
    for (int j = 0; j &lt; mapHeight-1; j++) {
      fill(mapTileColor(i, j)); 
      noStroke();
      rect(i*drawScale, j*drawScale, drawScale, drawScale);
    }
  }

  thread("physics");

  if (mousePressed) {
    switch (mouseButton) {
    case RIGHT:
      setMap(mouseX/drawScale, mouseY/drawScale, 1);
      break;

    case LEFT:
      setMap(mouseX/drawScale, mouseY/drawScale, 2);
      break;
    }
  }
}

void physics() {
  for (int i = 0; i &lt; mapWidth-1; i++) {
    for (int j = 0; j &lt; mapHeight-1; j++) {
      if (getMap(i, j) == 2 &amp;&amp; getMap(i, j+1) == 0 &amp;&amp; tick % tickRate == 0) {
        setMap(i, j+1, 2);
        setMap(i, j, 0);
      }
    }
  }
}

void setMap(int x, int y, int tile) {
  map[x][y] = tile;
}

int getMap(int x, int y) {
  return map[x][y];
}

color mapTileColor(int x, int y) {
  switch(getMap(x, y)) {

  case 1:
    return #000000;

  case 2:
    return #0000FF;

  default:
    return #FFFFFF;
  }
}
</code></pre>

<p>I want to make the gravity work on a water particle every 2 ticks, so it looks like it's falling. Instead, when you left click to place the water it falls instantly. How can I have a "fixed update" function, where the gravity would only occur every a certain amount of frames?</p>
]]></description>
   </item>
   <item>
      <title>Verlet physics glitches</title>
      <link>https://forum.processing.org/two/discussion/17648/verlet-physics-glitches</link>
      <pubDate>Tue, 26 Jul 2016 03:02:33 +0000</pubDate>
      <dc:creator>GNNop</dc:creator>
      <guid isPermaLink="false">17648@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I'm not sure if this is the best spot to ask this, but I thought it might be worth a shot to see if the glitches in my code could be fixed.</p>

<p>I've been making a verlet physics program, but, instead of having actual shapes programmed, I built all the shapes out of constraints and have only point - constraint collisions. Right now I have a way to detect collisions:</p>

<pre><code>void shapeShapeCollision(Atom a, Link b) {//!!!!!!!!!!!!!!!
  if(b.bonk){
    PVector futL = PVector.sub(b.b.pos.copy(),b.a.pos.copy());
    PVector pasL = PVector.sub(b.b.ppos.copy(),b.a.ppos.copy());
    PVector ppoint = PVector.sub(a.ppos.copy(),b.a.ppos.copy());
    PVector point = PVector.sub(a.pos.copy(),b.a.pos.copy());
//this is translating,
//   rotating and rescaling where the constraint was to where it will
 //be and checks if the translated old point and the new point 
//crossed the where the constraint is.
    float mx = futL.x*pasL.x+futL.y*pasL.y;
    float my = futL.y*pasL.x-futL.x*pasL.y;
    float q = pasL.x*pasL.x+pasL.y*pasL.y;
    PVector newppoint = new PVector(ppoint.x*mx-ppoint.y*my,ppoint.x*my+ppoint.y*mx);
    newppoint.div(q);
    if(acrossLine(newppoint,point,new PVector(0,0),futL)) {
      if(acrossLine(new PVector(0,0),futL,newppoint,point)) { 
        b.hit(true);
        collide(a,b);
      }
    }
  }
}

void collide(Atom a, Link b) {
  PVector aToPos = a.pos.copy();//this is from the constraint's left side to the point
  aToPos.sub(b.a.pos);
  PVector aTob = b.b.pos.copy();this is from the line's left to its right side
  aTob.sub(b.a.pos);
  float z = aTob.mag();
  PVector substitute = aTob.copy();
  PVector notNorm = PVector.mult(substitute.normalize(), (aTob.dot(aToPos))/aTob.mag());
  float x = notNorm.mag();
  PVector norm = PVector.sub(aToPos, notNorm);
  float y = norm.mag();
  float t = (z*z)/(2*(z*z-x*z+x*x))+.2;
  a.move(PVector.mult(norm, -t));
  b.moveA(PVector.mult(norm, (1-x/z)*t));
  b.moveB(PVector.mult(norm, (x/z)*t));
}
</code></pre>

<p>These work well with a few shapes. Unfortunately, for more than three shapes composed of points and lines if one shape collides with another it could push that one into a third without detection, and, since I'm only checking collisions with constraints, the point becomes stuck in the square.</p>

<p>Additionally, every once in a while if two shapes collide too hard a 'black hole' forms, sucking everything into it.
Finally, is it possible to make the squares less bouncy? dropping one onto the other really squishes the one on the bottom.</p>

<p>Thanks for your help!</p>

<p>If you need to see the sketch I can post everything later.</p>
]]></description>
   </item>
   <item>
      <title>changing polygon class into shapes</title>
      <link>https://forum.processing.org/two/discussion/14443/changing-polygon-class-into-shapes</link>
      <pubDate>Wed, 13 Jan 2016 23:25:16 +0000</pubDate>
      <dc:creator>mistah_jay</dc:creator>
      <guid isPermaLink="false">14443@/two/discussions</guid>
      <description><![CDATA[<p>hey guys,
I just started coding in processing and want to know if it is it possible to change a code i found. The code I want to use is the CAN kinect physics code. <a href="https://dl.dropboxusercontent.com/u/94122292/CANKinectPhysics.zip" target="_blank" rel="nofollow">https://dl.dropboxusercontent.com/u/94122292/CANKinectPhysics.zip</a>.</p>

<p>It uses the custom shapes class to create different shapes. would it be possible to change the code in such a way that i can change te shapes into images, i.e leaves or snow flakes? and if so, would anyone be so kind to help me in the right direction?</p>

<p>This is the CustomShape class that creates a shape and interacts with the shape of my body.</p>

<pre><code>import java.util.List;
import java.util.Arrays;

class CustomShape {
  // to hold the box2d body
  Body body;
  // to hold the Toxiclibs polygon shape
  Polygon2D toxiPoly;
  // custom color for each shape
  color col;
  // radius (also used to distinguish between circles and polygons in this combi-class
  float r;

  CustomShape(float x, float y, float r, BodyType type) {
    this.r = r;
    // create a body (polygon or circle based on the r)
    makeBody(x, y, type);
    // get a random color
    col = getRandomColor();
  }

  void makeBody(float x, float y, BodyType type) 
  {
    // define a dynamic body positioned at xy in box2d world coordinates,
    // create it and set the initial values for this box2d body's speed and angle
    BodyDef bd = new BodyDef();
    bd.type = type;
    bd.position.set(box2d.coordPixelsToWorld(new Vec2(x, y)));
    body = box2d.createBody(bd);
    body.setLinearVelocity(new Vec2(random(-8, 8), random(2, 8)));
    body.setAngularVelocity(random(-5, 5));

    // box2d polygon shape
    //PolygonShape sd = new PolygonShape();
    /* toxiPoly = new Polygon2D(Arrays.asList(new Vec2D(-r, r*1.5), 
     new Vec2D(r, r*1.5), 
     new Vec2D(r, -r*1.5), 
     new Vec2D(-r, -r*1.5)));*/

    if (r == -1) 
    {
      // box2d polygon shape
      PolygonShape sd = new PolygonShape();
      // toxiclibs polygon creator (triangle, square, etc)
      toxiPoly = new Circle(random(5, 20)).toPolygon2D(int(random(3, 6)));
      // place the toxiclibs polygon's vertices into a vec2d array
      Vec2[] vertices = new Vec2[toxiPoly.getNumPoints()];

      for (int i=0; i&lt;vertices.length; i++) 
      {
        Vec2D v = toxiPoly.vertices.get(i);
        vertices[i] = box2d.vectorPixelsToWorld(new Vec2(v.x, v.y));
      }
      // put the vertices into the box2d shape
      sd.set(vertices, vertices.length);
      // create the fixture from the shape (deflect things based on the actual polygon shape)
      body.createFixture(sd, 1);
    }

    else 
    {
      // box2d circle shape of radius r
      CircleShape cs = new CircleShape();
      cs.m_radius = box2d.scalarPixelsToWorld(r);
      // tweak the circle's fixture def a little bit
      FixtureDef fd = new FixtureDef();
      fd.shape = cs;
      fd.density = 1;
      fd.friction = 0.01;
      fd.restitution = 0.3;
      // create the fixture from the shape's fixture def (deflect things based on the actual circle shape)
      body.createFixture(fd);
    }
  }




  // method to loosely move shapes outside a person's polygon
  // (alternatively you could allow or remove shapes inside a person's polygon)
  void update() 
  {
    // get the screen position from this shape (circle of polygon)
    Vec2 posScreen = box2d.getBodyPixelCoord(body);
    // turn it into a toxiclibs Vec2D
    Vec2D toxiScreen = new Vec2D(posScreen.x, posScreen.y);
    // check if this shape's position is inside the person's polygon
    boolean inBody = poly.containsPoint(toxiScreen);
    // if a shape is inside the person
    if (inBody) {
      // find the closest point on the polygon to the current position
      Vec2D closestPoint = toxiScreen;
      float closestDistance = 9999999;
      for (Vec2D v : poly.vertices) 
      {
        float distance = v.distanceTo(toxiScreen);
        if (distance &lt; closestDistance) 
        {
          closestDistance = distance;
          closestPoint = v;
        }
      }
      // create a box2d position from the closest point on the polygon
      Vec2 contourPos = new Vec2(closestPoint.x, closestPoint.y);
      Vec2 posWorld = box2d.coordPixelsToWorld(contourPos);
      float angle = body.getAngle();
      // set the box2d body's position of this CustomShape to the new position (use the current angle)
      body.setTransform(posWorld, angle);
    }
  }

  // display the customShape
  void display() {
    // get the pixel coordinates of the body
    Vec2 pos = box2d.getBodyPixelCoord(body);
    pushMatrix();
    // translate to the position
    translate(pos.x, pos.y);
    noStroke();
    // use the shape's custom color
    fill(col);

    if (r == -1) {
      // rotate by the body's angle
      float a = body.getAngle();
      rotate(-a); // minus!
      gfx.polygon2D(toxiPoly);
    } 
    else {
      ellipse(0, 0, r*2, r*2);
    }

    popMatrix();
  }

  // if the shape moves off-screen, destroy the box2d body (important!)
  // and return true (which will lead to the removal of this CustomShape object)
  boolean done() {
    Vec2 posScreen = box2d.getBodyPixelCoord(body);
    boolean offscreen = posScreen.y &gt; height;
    if (offscreen) {
      box2d.destroyBody(body);
      return true;
    }
    return false;
  }
}
</code></pre>

<p>EOF</p>

<p>Thanks in advance</p>
]]></description>
   </item>
   <item>
      <title>Help with mass spectrometer program proton. (PVectors)</title>
      <link>https://forum.processing.org/two/discussion/14285/help-with-mass-spectrometer-program-proton-pvectors</link>
      <pubDate>Wed, 06 Jan 2016 03:38:27 +0000</pubDate>
      <dc:creator>AirDhamrait</dc:creator>
      <guid isPermaLink="false">14285@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a program that demonstrates a mass spectrometer but I am having trouble getting the calculations to work and getting the proton to move correctly. Any help would be greatly appreciated.</p>

<p><strong>Main Class:</strong></p>

<pre><code>int alphaVal, alphaDelta;
int rectX, rectY;      // Position of square button
int rect2X, rect2Y;  // Position of rect2 button
int rectSize = 90;     // Diameter of rect
int rect2Size = 93;   // Diameter of rect2
color rectColor, rect2Color, baseColor;
color rectHighlight, rect2Highlight;
color currentColor;
boolean rectOver = false;
boolean rect2Over = false;
PFont f;
String velocity = "Enter Velocity: ";
float iVel;



void setup(){
size(1000, 800);
f = createFont("Arial",16,true); // STEP 2 Create Font
noStroke();
 alphaVal = 255; // initial value
 alphaDelta = 1; // rate of change
 frameRate(100); // slow f.r. down to pulse smoothly

rectColor = color(0);
  rectHighlight = color(51);
  rect2Color = color(255);
  rect2Highlight = color(204);
  baseColor = color(102);
  currentColor = baseColor;
  rect2X = width/2+rect2Size/2+10;
  rect2Y = height/2;
  rectX = width/2-rectSize-10;
  rectY = height/2-rectSize/2;
  ellipseMode(CENTER);

  //partice
   mouse = new PVector(width/2.,height/2);
  stroke(255,255,0);
  strokeWeight(1.2);
}

void mousePressed(){
  initialVel = new PVector(5, 0);
  particle = new Particle(new PVector(0, 114), initialVel, new PVector(), new PVector(), new PVector());
}//end mouse pressed

void keyPressed(){
    if (keyCode == BACKSPACE) {
      if (velocity.length() &gt; 0) {
        velocity = velocity.substring(0, velocity.length()-1);
      }else if (keyCode == DELETE) {
        velocity = "";
      }else if (keyCode != SHIFT &amp;&amp; keyCode != CONTROL &amp;&amp; keyCode != ALT) {
      velocity = velocity + key;
      iVel = Float.valueOf(velocity).floatValue();
      }
    }
}//end key pressed


void draw(){ 
  background(23, 100, 240);

  /*
  Draw the magnet field 
  */
  for(int x = -10; x &lt; width; x++){
    for (int y = -10; y &lt; height; y++){
       //fill(#E0DCDC);
       fill(255, 255, 255, alphaVal);
       //ellipse(x, y, 10, 10);
      // stroke(100);
      // line(x, y, x+5, y+5);
       pushMatrix();
       translate(x, y);
       rotate(radians(65));
       rect(0,0, 13, 2);

       popMatrix();
       //noStroke();
       y = y + 30;
    }
    x = x + 30;
  }

/*
Draw the text on the empty space on the background
*/
fill(23, 100, 250);
rect(0, 230, 450, 620); // cover square
//display board
textFont(f, 16);
fill(0);
text("Radius:", 50, 300);
text("Particle Type: Electron", 50, 350);
text("Magnetic Field: 2T", 50, 400);
String eField = "Electric Field: 220 000 000 N/C";
text(eField, 50, 450);
text(velocity, 50, 500);

/*
Draw the lines containing the magnetic field
*/
stroke(5);
fill(#FA3845);
rect(0, 0, 5, 90); // top top side line 1
rect(0, 135, 5, 90); // top bottom side line 2
rect(430, 225, 5, 574); // detection side line
fill(255, 204, 0);
rect(0, 220, 435, 5); // top bottom line
rect(0, 0, 435, 5); // top top line
noStroke();

if ((alphaVal == 0) || (alphaVal == 255)){
    alphaDelta = -alphaDelta;
}
alphaVal+= alphaDelta; 
 if (particle != null){
  particle.updatePos();
  particle.drawParticle();
  }

}
</code></pre>

<p><strong>Particle Class:</strong></p>

<pre><code>int WIDTH = 700;
int HEIGHT = 700;
PVector mouse;
PVector initialVel;
float g = 9.8/49;
float mass = 1;
float q = -1;
PVector electricForce;
PVector magneticForce;
Particle particle;

class Particle {
  PVector pos;
  PVector vel;
  PVector acc;
  int charge;
  int mass;

  Particle() {
    pos = new PVector();
    vel = new PVector();
    acc = new PVector();
    electricForce = new PVector();
    magneticForce = new PVector();
  }

  Particle(PVector _pos,PVector _vel, PVector _acc, PVector _electricForce, PVector _magneticForce){
    pos = _pos;
    vel = _vel;
    acc = _acc;
    electricForce = _electricForce;
    magneticForce = _magneticForce;
  }

  void updatePos(){

    if(pos.x &gt; 0 &amp;&amp; pos.x &lt; 435){
      acc.set(electricForce);    
     acc.add(vel.cross(magneticForce));
     acc.mult(charge/mass);
    }else{
      electricForce.set(0, 0);
      magneticForce.set(0,0);
    }
    //make net force pvector add all the force to it then add net force to acceleration.
  //  acc.set(0,g);
 //   acc.add(electricForce);
//    acc.add(magneticForce);
    vel.add(acc);
    pos.add(vel);
  }

  void drawParticle (){
    fill (255, 0, 0);
    ellipse (pos.x, pos.y, 5, 5);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Simple simulation of the Doppler effect and wave interferene</title>
      <link>https://forum.processing.org/two/discussion/11044/simple-simulation-of-the-doppler-effect-and-wave-interferene</link>
      <pubDate>Thu, 28 May 2015 15:17:16 +0000</pubDate>
      <dc:creator>nikolaj</dc:creator>
      <guid isPermaLink="false">11044@/two/discussions</guid>
      <description><![CDATA[<p>These are two small simulation, which i made for my physics exam, to demonstrate the doppler effect, and the interference of waves.</p>

<p>This is a simulation the interference of waves. There are two wave generators generating circular waves with the same frequency, the waves all has the same velocity, and thus wavelength. The generators are next to one another, and their waves are interfering with one another, they are creating a pattern of stripes. Left and right arrow keys moves the generators closer two, or farther from one another, changing the number of stripes generated.</p>

<pre><code>float speedOfWaves=1;
int periodOfgenerator=20;


class circle_wave {//a circle with an increasing radius
  float centX;
  float centY;//the center of the wave

  float v;//the speed of the waves
  float r;//the distance the wave has traveled
  color c;//the color reprasents weather it's a top or buttom


  circle_wave(float tempV, float tempCentX, float tempCentY, color tempC) {
    centX = tempCentX;
    centY = tempCentY;
    v = tempV;
    r = 0;
    c = tempC;
  }

  void beWave() {
    r+=v;
    stroke(c);
    noFill();

    ellipse(centX, centY, r*2, r*2);
  }
}

class toneGenerator {
  float x;
  float y;//the position of the generator

  int T;//the period of the generator
  int cycles;

  ArrayList&lt;circle_wave&gt; waves;//an arraylist of the waves emitted

  toneGenerator(float tempX, float tempY, int tempT) {
    T=tempT;
    println(T);
    x=tempX;
    y=tempY;

    waves = new ArrayList&lt;circle_wave&gt;();
//    waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
  }

  void generate() {
    fill(0);//display and move the generator
    ellipse(x, y, 10, 10);


      if (x &gt; width) {
        x=0;
      } else if (x &lt; 0) {
        x=width;
      }


      //display and move the existing waves
      for (int i = waves.size ()-1; i&gt;=0; i--) {
        circle_wave myWave = waves.get(i);
        myWave.beWave();

        if (myWave.r &gt; width/3) {
          waves.remove(i);
        }
      }

      if (mousePressed || keyPressed) {
        waves = new ArrayList&lt;circle_wave&gt;();
//        waves.add(new circle_wave(0.5, x, y, color(255, 255, 255, 100)));
      }

      if (cycles%(T*2)==0) {
        waves.add(new circle_wave(speedOfWaves, x, y, color(255, 255, 255)));
      } else if (cycles%(T*2)==T) {
        waves.add(new circle_wave(speedOfWaves, x, y, color(0, 0, 0)));
      }

      cycles++;
    }
  }

  toneGenerator[] tone;


  void setup() {
    size(1000, 750);
    tone = new toneGenerator[2];
    tone[0] = new toneGenerator(width/2+20, height/2, periodOfgenerator);
    tone[1] = new toneGenerator(width/2-20, height/2, periodOfgenerator);
  }

  void draw() {
    background(122);
    tone[0].generate();
    tone[1].generate();
  }

  void keyPressed() {
    if (key == CODED) {
      if (keyCode == LEFT) {
        tone[0].x+=2.5;
        tone[1].x-=2.5;
      } else if (keyCode == RIGHT) {
        tone[0].x-=2.5;
        tone[1].x+=2.5;
      }
    }
  }
</code></pre>

<p>This simulation is a modified version of the previous one, there is only one wave generator on the screen. It is generating circular waves, with a constant frequency and velocity. Pressing the left and right arrow keys will speed it up, or slow it down, clicking spacebar will freeze it, and move the waves instead, as if the screen was moving with the same speed as the generator.</p>

<p>The waves are moving relative to the background (just like soundwaves is moving relative to the air around it) not the speed of the generator, so the waves in front of the generator will be compressed, and those behind will get stretched out (in the case of soundwaves, whe hear a different tone, depending on whether we are standing before or behind it, in case of light, we see a different color, depending on in what direction, and how fast, the source of light is moving relative to us).</p>

<p>When the generator is moving at the same speed as the waves, the waves are not getting away from it, and a shockwave builds up (in case of sound, the sound barrier). When the generator breaks the speed of the waves, the shockwave becomes cone shaped.</p>

<pre><code>float speedOfWaves=2;
int periodOfgenerator=10;
boolean move=true;

class circle_wave {//a circle with an increasing radius
  float centX;
  float centY;//the center of the wave

  float v;//the speed of the waves
  float r;//the distance the wave has traveled
  color c;//the color reprasents weather it's a top or buttom


  circle_wave(float tempV, float tempCentX, float tempCentY, color tempC) {
    centX = tempCentX;
    centY = tempCentY;
    v = tempV;
    r = 0;
    c = tempC;
  }

  void beWave(float wind) {
    r+=v;
    stroke(c,map(r,0,width/2,255,0));
    noFill();

    centX-=wind;

    ellipse(centX, centY, r*2, r*2);
  }
}

class toneGenerator {
  float x;
  float y;//the position of the generator


  float xV;//the x velocity

  int T;//the period of the generator
  int cycles;

  ArrayList&lt;circle_wave&gt; waves;//an arraylist of the waves emitted

  toneGenerator(float tempXV, float tempX, float tempY, int tempT) {
    T=tempT;
    x=tempX;
    y=tempY;
    xV=tempXV;

    waves = new ArrayList&lt;circle_wave&gt;();
 //   waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
  }

  void generate() {
    fill(0);//display and move the generator
    ellipse(x, y, 10, 10);

    if (move) {
      x+=xV;
    }/* else {
      for (int i = 0; i&lt;waves.size (); i++) {
        circle_wave myWave = waves.get(i);
        myWave.centX-=xV;
      }
    }*/

      if (x &gt; width) {
        x=0;
      } else if (x &lt; 0) {
        x=width;
      }


      //display and move the existing waves
      for (int i = waves.size ()-1; i&gt;=0; i--) {
        circle_wave myWave = waves.get(i);
        if (!move) {
          myWave.beWave(xV);
        }
        else {
          myWave.beWave(0);
        }

        if (myWave.r &gt; width/2) {
          waves.remove(i);
        }
      }

      if (mousePressed) {
        waves = new ArrayList&lt;circle_wave&gt;();
 //       waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
  }

      if (cycles%(T*2)==0) {
        waves.add(new circle_wave(speedOfWaves, x, y, color(255, 255, 255)));
      } else if (cycles%(T*2)==T) {
        waves.add(new circle_wave(speedOfWaves, x, y, color(0, 0, 0)));
      }

      cycles++;
    }
  }

  toneGenerator tone;


  void setup() {
    size(1000, 750);
    tone = new toneGenerator(0, width/2+20, height/2, periodOfgenerator);
  }

  void draw() {
    background(122);
    tone.generate();
  }

  void keyPressed() {
    if (key == CODED) {
      if (keyCode == LEFT) {
        tone.xV-=0.25;
      } else if (keyCode == RIGHT) {
        tone.xV+=0.25;
      }
    } else if (key == ' ') {
      move=!move;
    }
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Fractal tree blowing in the wind</title>
      <link>https://forum.processing.org/two/discussion/11409/fractal-tree-blowing-in-the-wind</link>
      <pubDate>Tue, 23 Jun 2015 06:58:11 +0000</pubDate>
      <dc:creator>nikolaj</dc:creator>
      <guid isPermaLink="false">11409@/two/discussions</guid>
      <description><![CDATA[<p>The mathematically perfect fractal tree, is being bend by the wind.</p>

<p>You can control the angle the wind is comming from, with the left/right arrow keys, and increase/decrease the strength with the up/down arrow keys, press space to hide/unhide wind speed and direction indicator.</p>

<p>Assumptions made:</p>

<ol>
<li><p>The wind is 100% consistant, (always comming from the same angle, with the same force).</p></li>
<li><p>Gravity doesn't effect the tree (or rather the effect can't be seen).</p></li>
<li><p>Shorter branches, are always thinner than longer branches, and therefor easier bend by the wind.</p></li>
<li><p>The effect of the 3. assumption, is so great, that the lever principle can be ignored.</p></li>
<li><p>The branches of the tree can be bend, without ever breaking.</p></li>
<li><p>When not effected by the wind, a branch will move back to its starting position .</p></li>
<li><p>The wind can come from all directions.</p></li>
</ol>

<p>Download code at Githup <a rel="nofollow" href="https://github.com/nikolajRoager/windyTree.git">https://github.com/nikolajRoager/windyTree.git</a></p>

<p><img src="http://forum.processing.org/two/uploads/imageupload/313/E3F3XQVQGKRX.png" alt="windy" title="windy" /></p>
]]></description>
   </item>
   </channel>
</rss>