We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Please edit your post above and format your code so that people can run it and help you!
This is a collision detection question -- please see a basic introduction to collision detection:
Finally, you are talking about sphere / sphere collision detection. That is circle-circle collision detection with an added z axis.
Is the distance between x,y,z and a,b,c less than the sum of their radii? If so, they are colliding, and they should alter direction.
Notice that PVector has some built-in methods that can help -- e.g. for calculating the distance between two 3D points.
There is example for 2D collision with vectors
Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Please explore the examples section in the Processing Foundation's website as there is at least one example that addresses your question.
Kf
Hey guys, I fixed the post, please let me know your thoughts. Thanks again