Help with spheres bouncing off each other

edited May 2018 in Library Questions

Hey everyone, I created this sketch for a bunch of emojis orbiting around one emoji and I am trying to make them bounce off each other when they touch, can someone please help me on how to do that? Here is the code I am using:

// importing the Peasy Cam library
import peasy.*;
import peasy.org.apache.commons.math.*;
import peasy.org.apache.commons.math.geometry.*;

// declaring the main emoji
Planet sun;
//declairing the Peasy Cam 
PeasyCam cam;
// declaring the PImage function
PImage img;
// declairing the the texture
PImage[] textures = new PImage[7];
// declaring the particle system

void setup() {

  size(displayWidth, displayHeight, P3D);


  img = loadImage("1.jpg");
  textures[0]= loadImage("2.jpg");
  textures[1]= loadImage("3.jpg");
  textures[2]= loadImage("4.jpg");
  textures[3]= loadImage("5.jpg");
  textures[4]= loadImage("6.jpg");
  textures[5]= loadImage("7.jpg");
  cam = new PeasyCam(this, 500);
  sun = new Planet (100, 0, 0, img);
  sun.spawnMoons(10, 4);
}

void draw() {

  background(0);
  lights();
  ambientLight(129, 62, 0);
  //translate(displayWidth, displayHeight);
  noFill();
  stroke(255);
  strokeWeight(1);
  //rotateY(frameCount/200.0);
  box(4000);

  lights();
  ambientLight(129, 62, 0);
  directionalLight(51, 102, 126, 0, -1, 0);
  spotLight(255, 0, 0, width/2, height/2, 400, 0, 0, -1, PI/4, 2);

  sun.show();
  sun.orbit();
}

class Planet {
  float radius;
  float angle;
  float distance;
  Planet [] planets;
  float orbitSpeed;
  PVector v;
  PShape globe;
  float mass=1;


  Planet(float r, float d, float o, PImage img ) {
    v = PVector. random3D();
    radius = r;
    angle = random(20);
    distance= d;
    v.mult(distance);
    orbitSpeed = o;

    noStroke();
    noFill();
    globe = createShape(SPHERE, radius);
    globe.setTexture(img);
  }
  void spawnMoons(int total, int level) {
    planets = new Planet[total];
    for (int  i = 0; i<planets.length; i++) {

      float r= random(level*10);

      float d = random(100, 1000);

      float o = random (-0.01, 0.02);

      int index = int(random (textures.length));
      planets[i]= new Planet (r, d, o, textures[index]);

      if (level<1) {
        int num = int(random(0, 5));
        planets[i].spawnMoons(num, level+1);
      }
    }
  }

  void orbit() {
    angle =  angle + orbitSpeed/mass;
    if (planets !=null) {
      for (int  i = 0; i<planets.length; i++) {
        planets[i].orbit();
      }
    }
  }
  void show() {
    pushMatrix();
    noStroke();
    PVector v2 = new PVector(1, 0, 1);
    PVector p = v.cross(v2);
    rotate (angle, p.x, p.y, p.z);

    translate(v.x, v.y, v.z);
    shape(globe);
    if (planets !=null) {
      for (int  i = 0; i<planets.length; i++) {
        planets[i].show();
      }
    }
    popMatrix();
  }
  void bounce() {
  }
}

I would really appreciate your help Thanks

Answers

Sign In or Register to comment.