# Genuary2024_Physics

# SpinningShapes

For **Genuary2024 Day 15: Physics Library,** coded geometric physics with **PhysicsJs, Hydra and SonicPi,** Allowing the shapes to spin and spin!

## Poem

```ocaml
Spin left, Spin RIght
Rotating loose and tight
Spinning, even if the lights are dimming
For the physics allow to keep spinning
Across the screen as they pivot
With no Limits
```

## Video

<iframe width="560" height="315" src="https://www.youtube.com/embed/RNghK9tSp4I?si=zhm4Oidh5sdXWsSm"></iframe>

## Code

### PhysicsJs

```javascript
//Furthering the code in the Basic Usage Link: h
//https://wellcaffeinated.net/PhysicsJS/basic-usage
Physics(function(world){
    
 
    
  const d = new Date();
let time4 = d.getTime();

  var viewWidth = 1200;
  var viewHeight = 800;
    
  pick = ['orange','blue','red','black','green','gold']

  var renderer = Physics.renderer('canvas', {
    el: 'viewport',
    width: viewWidth,
    height: viewHeight,
    meta: false, // don't display meta data
    styles: {
        // set colors for the circle bodies
        'circle' : {
            strokeStyle: 'blue',
            lineWidth: time4 % 10 + 1,
            fillStyle: pick[time4 % 6],
            angleIndicator: 'yellow'
        },
          'rectangle' : {
            strokeStyle: pick[time4 % 6],
            lineWidth: time4 % 10 + 1,
            fillStyle: pick[time4 % 6-1],
            angleIndicator: 'yellow'
              
        },'convex-polygon' : {
            strokeStyle: 'white',
            lineWidth: time4 % 10 + 5,
            fillStyle: pick[time4 % 6-2],
            angleIndicator: 'red'
        }
        
     
    }
  });

  // add the renderer
  world.add( renderer );
  // render on each step
  world.on('step', function(){
    world.render();
  });

  // bounds of the window
  var viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight);

  // constrain objects to these bounds
  world.add(Physics.behavior('edge-collision-detection', {
      aabb: viewportBounds,
      restitution: 0.99,
      cof: 0.99
  }));

  // add a circle
  world.add(
      Physics.body('circle', {
        x: 80, // x-coordinate
        y: 30, // y-coordinate
        vx: 0.5, // velocity in x-direction
        vy: 0.11, // velocity in y-direction
        radius: 20
      })
  );
    
         world.add(
      Physics.body('rectangle', {
        x: 180, // x-coordinate
        y: 130, // y-coordinate
        vx: 0.15, // velocity in x-direction
        vy: 0.211, // velocity in y-direction
          width: 150,
          height: 150
      })
  );
    
    
      // add a circle
  world.add(
      Physics.body('circle', {
        x: 80, // x-coordinate
        y: 30, // y-coordinate
        vx: 0.5, // velocity in x-direction
        vy: 0.11, // velocity in y-direction
        radius: 40
      })
  );

    
      world.add(
      Physics.body('circle', {
        x: 180, // x-coordinate
        y: 130, // y-coordinate
        vx: 0.15, // velocity in x-direction
        vy: 0.211, // velocity in y-direction
        radius:  17
      })
  );
        
    
world.add( Physics.body('convex-polygon', {
    x: 400,
    y: 200,
    vx: -0.02,
    vertices: [
        {x: 10, y: 70},
        {x: 80, y: 0},
        {x: 0, y: -80},
        {x: -30, y: -30},
        {x: -30, y: 30},
    ]
}) );
    
        world.add( Physics.body('convex-polygon', {
    x: 400,
    y: 200,
    vx: -0.02,
    vertices: [
        {x: 20, y: 50},
        {x: 80, y: 0},
        {x: 0, y: -80},
        {x: -30, y: -30},
        {x: -30, y: 70},
    ]
}) );


  // ensure objects bounce when edge collision is detected
  world.add( Physics.behavior('body-impulse-response') );

  // add some gravity
  world.add( Physics.behavior('constant-acceleration') );
    
  //add collisions between the shapes
    world.add( Physics.behavior('body-collision-detection') );
world.add( Physics.behavior('sweep-prune') );

  // subscribe to ticker to advance the simulation
  Physics.util.ticker.on(function( time, dt ){

      world.step( time );
      
      
      
  });

  // start the ticker
  Physics.util.ticker.start();

});
```

### SonicPi

```ruby
# Loading previous buffer contents. Please wait...def soundtrack()
live_loop :addition do
  
  use_random_seed Time.now.to_i
  
  
  # recursion call being the function calling a new sub string
  def Addition(arr)
    return 1 if arr.length == 0
    arr[0] + Addition(arr[1..-1])
  end
  
  #variables that will be used in the Method Call below
  #Dice is a randomizer that functions as a dice roll
  a = dice(6)
  b = dice(6)
  c = dice(6)
  d = rrand_i(2,5)
  e = 3
  
  #Calling the Method to aid with sound design
  smacks = Addition([a,b,c,d,e])
  notes = Addition([a,b,d])
  
  #sound elements
  if notes % 2 == 0
    
    with_fx :flanger, mix: rrand(0.3,0.5) do
      with_fx :echo do
        use_random_seed Time.now.to_i / 2
        use_synth [:pretty_bell, :kalimba, :prophet].choose
        play notes * 4, attack: dice(6)
      end
    end
  else
    
    with_fx :whammy, mix: rrand(0.2,0.6) do
      with_fx :gverb do
        use_synth [:chipbass, :kalimba, :prophet].choose
        play notes * 4, attack: dice(6)
      end
    end
  end
  
  if smacks > 10
    
    use_random_seed Time.now.to_i/4
    
    if notes > 8
      
      use_random_seed Time.now.to_i/5
      
      
      with_fx :vowel ,voice: dice(4) do
        sample :drum_bass_hard, decay: smacks / 12 if spread(smacks/4, smacks/3).look
      end
      
    else
      with_fx :ixi_techno, mix: 0.75 do
        sample :drum_bass_soft, decay: smacks / 12 if spread(smacks/4, smacks/3).look
      end
    end
    
  else
    
    if notes > 8
      
      with_fx :vowel do
        sample :tabla_ghe1, decay: smacks / 12 if spread(smacks/4, smacks/3).look
      end
      
    else
      with_fx :ixi_techno, mix: 0.75 do
        sample :tabla_ghe3, decay: smacks / 12 if spread(smacks/4, smacks/3).look
      end
    end
    
  end
  
  
  #print out numbers
  puts Addition([a,b,c,d,e])
  puts notes
  
  sleep [0.25,0.5,1,2].choose
end
```

### Hydra

```javascript

s0.initScreen()

src(s0).scale([0.5,1,2]).pixelate(500,500).out()
```
