Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Help with spheres bouncing off each other

$
0
0

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.*;

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

int particles=700; int [] x = new int[particles]; int [] y = new int[particles]; int [] z = new int[particles];

void setup() {

size(displayWidth, displayHeight, P3D); for (int i = 0; i<particles; i++) { x[i] =int(random(-1500, 1500)); y[i]= int (random(-1500, 1500)); z[i] =int (random(-1500, 1500)); }

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); //box(6000); //box(12000); //box(24000); //box(240002); //box(240004); for (int i=0; i<particles; i++) { stroke(random(255)); strokeWeight(3); point(x[i], y[i], z[i]); } lights(); ambientLight(129, 62, 0); directionalLight(51, 102, 126, 0, -1, 0);

sun.show(); sun.orbit(); } class Planet { float radius; float angle; float distance; Planet [] planets; float orbitSpeed; PVector v; PShape globe;

Planet(float r, float d, float o, PImage img ) { v = PVector. random3D(); radius = r; angle = random(200); 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 ((radius+r)*5, (radius+r)*10);

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

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

}

void orbit() { angle = angle + orbitSpeed; 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); stroke(255); translate(v.x, v.y, v.z); noStroke(); 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


Viewing all articles
Browse latest Browse all 2896

Trending Articles