How to convert a Vec2D example to Vec3D

Hello -

I have been exploring the attraction 2D example in the verletphysics library. I would like to try and expand this example to a Vec3D environment.

I have managed to convert the sketch so that it runs without errors but the forces are acting differently to how they were in the 2D environment.

I have a feeling its something very simple that I'm missing. Feedback would be much appreciated.

Below is the original Vec2D example and my current Vec3D environment.

import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
int countNeighbours=0;
float framecountScale = 0.1;


int NUM_PARTICLES = 29;

VerletPhysics2D physics;
AttractionBehavior mouseAttractor;

Vec2D mousePos;
Vec2D target = new Vec2D(300, 300);
Vec2D randomTarget;
int count =0;
int count2= 0;

void setup() {
  size(600, 600, P3D);
  // setup physics with 10% drag
  physics = new VerletPhysics2D();
  physics.setDrag(0.05f);
  physics.setWorldBounds(new Rect(0, 0, width, height));
  // the NEW way to add gravity to the simulation, using behaviors
  //physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15f)));

  randomTarget = new Vec2D(random(200, 400), random(200, 400));
  // create a new positive attraction force field around the mouse position (radius=250px)
  mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
  physics.addBehavior(mouseAttractor);
}

void addParticle() {
  VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(random(0, width / 2), random(0, height/2)));
  physics.addParticle(p);
  // add a negative attraction force field around the new particle
  physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));
}

void draw() {
  background(255);
  noStroke();

  fill(0);

  if (physics.particles.size() < NUM_PARTICLES) {
    addParticle();
  }
  physics.update();
  for (VerletParticle2D p : physics.particles) {



    float dist = randomTarget.distanceTo(p);
    float red = map(dist, 0, 50, 225, 0);
        color c = color (0,0,150+(count/1000));
    float blueValue = blue (c);    



    if (dist<=5) {
      fill(red+(count), 0, 150-(count/100),100+(count/200));
      count++;
    }

    if (dist>5 && dist<=22) {
      fill(red+(count/1000), 0, 150,100+(count/200));
      count++;
    }

    if (dist>22) {
      fill(red-(count/10000), 0, blueValue,100+(count/200));
      count++;
    }
//    println(frameCount);




    if (blueValue >200) {

          println("BLUE BLUE BLUE");

//      physics = new VerletPhysics2D();
//      physics.setDrag(0.05f);
//      physics.setWorldBounds(new Rect(0, 0, width, height));
//      physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15f)));
//
//      Vec2D p2 = new Vec2D (100, 100);
//      mouseAttractor = new AttractionBehavior(randomTarget, width, 0.9f);
//      physics.addBehavior(mouseAttractor);
    }


    ellipse(p.x, p.y, 8, 8);
  }
}

//void mousePressed() {
//  mousePos = new Vec2D(mouseX, mouseY);
//  target = new Vec2D(300, 300);
//  randomTarget = new Vec2D(random(0, width), random(0, height));
//  // create a new positive attraction force field around the mouse position (radius=250px)
//  mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
//  physics.addBehavior(mouseAttractor);
//}
//
////void mouseDragged() {
////  // update mouse attraction focal point
////  mousePos.set(mouseX, mouseY);
////}
////
//void mouseReleased() {
//  // remove the mouse attraction when button has been released
//  mouseAttractor = new AttractionBehavior(mousePos, width, 0f);
//  physics.addBehavior(mouseAttractor);
//}

and

import toxi.geom.*;
import toxi.physics.*;
import toxi.physics.behaviors.*;
int countNeighbours=0;
float framecountScale = 0.1;


int NUM_PARTICLES = 10;

VerletPhysics physics;
AttractionBehavior mouseAttractor;

Vec3D mousePos;
Vec3D target = new Vec3D(300, 300, 300);
Vec3D randomTarget;
int count =0;
int count2= 0;

void setup() {
  size(600, 600, P3D);
  // setup physics with 10% drag
  physics = new VerletPhysics();
  physics.setDrag(0.5f);
  physics.setWorldBounds(new AABB(new Vec3D(), new Vec3D(600, 600, 600)));
  // the NEW way to add gravity to the simulation, using behaviors
  //physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15f)));

  randomTarget = new Vec3D(random(200, 400), random(200, 400), 0);
  // create a new positive attraction force field around the mouse position (radius=250px)
  mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
  physics.addBehavior(mouseAttractor);
}

void addParticle() {
  VerletParticle p = new VerletParticle(Vec3D.randomVector().scale(5).addSelf(random(0, width / 2), random(0, height/2), 0));
  physics.addParticle(p);
  // add a negative attraction force field around the new particle
  physics.addBehavior(new AttractionBehavior(p, 20,-1.2f, -0.01f));
}

void draw() {
  background(255);
  noStroke();

  fill(0);

  if (physics.particles.size() < NUM_PARTICLES) {
    addParticle();
  }
  physics.update();
  for (VerletParticle p : physics.particles) {



    float dist = randomTarget.distanceTo(p);
    float red = map(dist, 0, 50, 225, 0);
    color c = color (0, 0, 150+(count/1000));
    float blueValue = blue (c);    



    if (dist<=5) {
      fill(red+(count), 0, 150-(count/100), 100+(count/200));
      count++;
    }

    if (dist>5 && dist<=22) {
      fill(red+(count/1000), 0, 150, 100+(count/200));
      count++;
    }

    if (dist>22) {
      fill(red-(count/10000), 0, blueValue, 100+(count/200));
      count++;
    }
    //    println(frameCount);




    if (blueValue >200) {

      println("BLUE BLUE BLUE");

      //      physics = new VerletPhysics2D();
      //      physics.setDrag(0.05f);
      //      physics.setWorldBounds(new Rect(0, 0, width, height));
      //      physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15f)));
      //
      //      Vec2D p2 = new Vec2D (100, 100);
      //      mouseAttractor = new AttractionBehavior(randomTarget, width, 0.9f);
      //      physics.addBehavior(mouseAttractor);
    }


    ellipse(p.x, p.y, 8, 8);
  }
}

//void mousePressed() {
//  mousePos = new Vec2D(mouseX, mouseY);
//  target = new Vec2D(300, 300);
//  randomTarget = new Vec2D(random(0, width), random(0, height));
//  // create a new positive attraction force field around the mouse position (radius=250px)
//  mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
//  physics.addBehavior(mouseAttractor);
//}
//
////void mouseDragged() {
////  // update mouse attraction focal point
////  mousePos.set(mouseX, mouseY);
////}
////
//void mouseReleased() {
//  // remove the mouse attraction when button has been released
//  mouseAttractor = new AttractionBehavior(mousePos, width, 0f);
//  physics.addBehavior(mouseAttractor);
//}

Thanks!

Sign In or Register to comment.