Bounce inside a Pyramid or a Sphere
in
Programming Questions
•
5 months ago
how can i make a ball bouncing inside a pyramid?
how can i make a ball bouncing inside a circle?
//Bouncing Ball in 3D Canvas
//Mohamed Naeim
// Press 'e' to end the code
// Press 's' to capture a pdf
// to navigate, Press rightMouseButton to zoom
// to navigate, Press LeftMouseButton to Rotate
// to navigate, Press ScrollButton to Pan
import processing.opengl.*;
import processing.pdf.*;
import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
Unit[] a = new Unit[1];
Unit[] b;
Unit[] c;
int n;
int d;
PeasyCam cam;
Whitebox whitebox;
boolean saveToggle = false;
void setup() {
size(400, 400, OPENGL);
n = 10;
d = 20;
b = new Unit[n];
a[0]= new Unit(random(-100, 100), random(-100, 100), random(-100, 100), random(-5, 5), random(-5, 5), random(-5, 5), random(10, 50));
for ( int i = 0; i < n; i++) {
b[i] = new Unit(random(-100, 100), random(-100, 100), random(-100, 100), random(-5, 5), random(-5, 5), random(-5, 5),random(10, 50));
a = (Unit[])append(a, b[i]);
}
whitebox = new Whitebox();
cam = new PeasyCam(this, 1000);
cam.setMinimumDistance(50);
cam.setMaximumDistance(1500);
frameRate(30);
}
void draw() {
if (saveToggle) {
PGraphicsPDF file = (PGraphicsPDF)beginRaw(PDF, "picture3D.pdf");
file.noStroke();
file.fill(0);
file.rect(0, 0, width, height);
}
background(100);
whitebox.display();
whitebox.canvasaxis();
for (int j = 0; j < a.length; j++) {
pushMatrix();
a[j].move();
a[j].display();
popMatrix();
}
if (saveToggle) {
endRaw();
saveToggle = false;
}
}
void keyPressed() {
if (key == 'e') {
println(" got to go");
exit();
}
if ( key == 's') {
saveToggle = true;
}
}
class Unit {
PVector loc;
PVector spd;
float r;
float x, y, z;
float v, u, w;
Unit ( float v_, float u_, float w_, float x_, float y_, float z_, float r_) {
x = x_;
y = y_;
z = z_;
v = v_;
u = u_;
w = w_;
r = r_;
loc = new PVector(v, u, w);
spd = new PVector(x, y, z);
//r = 50;
}
void display() {
stroke(150,0,0);
strokeWeight(1);
noFill();
//fill(255, 100);
translate(loc.x, loc.y, loc.z);
sphere(r);
}
void move() {
loc.add(spd);
if ((loc.x < (-200+(r)))&&(spd.x < 0)) {
spd.x *= -1;
}
if ((loc.y < (-200+(r)))&&(spd.y < 0)) {
spd.y *= -1;
}
if ((loc.z < (-200+(r)))&&(spd.z < 0)) {
spd.z *= -1;
}
if ((loc.x > (200-(r)))&&(spd.x > 0)) {
spd.x *= -1;
}
if ((loc.y > (200-(r)))&&(spd.y > 0)) {
spd.y *= -1;
}
if ((loc.z > (200-(r)))&&(spd.z > 0)) {
spd.z *= -1;
}
}
}
class Whitebox{
Whitebox(){
}
void display() {
stroke(255);
strokeWeight(2);
noFill();
box(400, 400, 400);
}
void canvasaxis(){
// Y-Axis Blue Line
stroke(0, 0, 255);
noFill();
rectMode(CENTER);
//rect(0, 0, 400, 400);
strokeWeight(5);
line(-500, 250, -250,
-500, 0, -250);
text("Y - Axis", -500, 0, -250);
//Z-Zxis Green Line
stroke(0, 255, 0);
noFill();
rectMode(CENTER);
rotateY(PI/2);
//rect(0, 0, 400, 400);
strokeWeight(5);
rotateY(-PI/2);
line(-500, 250, -250,
-500, 250, 0);
text("Z - Axis", -500, 250, 0);
//x-Axis Red Line
stroke(255, 0, 0);
noFill();
rectMode(CENTER);
rotateX(PI/2);
//rect(0, 0, 400, 400);
rotateX(-PI/2);
strokeWeight(5);
line(-500, 250, -250,
-250, 250, -250);
text("X - Axis", -250, 250, -250);
}
}
1