Finally: 3D sphere in a box
in
Share your Work
•
7 months ago
I was dreaming about this all my life:
create a 3D sphere in a box moving there and be reflected by the walls.
Thanks to processing I finally was able to do this. Thank you.
That's maybe the starting point for a 3D pong game (which already exists, I know...).
- //
- // vars ====================================================
- // Creating an array of objects.
- Mover[] movers = new Mover[1];
- // main functions ========================================
- void setup() {
- // automatic init
- size(displayWidth, displayHeight, OPENGL );
- fill(204);
- movers[0] = new Mover();
- //
- sphereDetail(15);
- } // function
- void draw() {
- // automatically loops
- background ( 0, 0, 54 );
- lights();
- // camera
- camera( 110, 150, 430, // eyeX, eyeY, eyeZ
- 150, 150, -100, // centerX, centerY, centerZ
- 0.0, 1.0, 0.0 ); // upX, upY, upZ
- // Big Game Box
- BigGameBox ();
- int i = 0;
- movers[i].update();
- movers[i].checkEdges();
- movers[i].display();
- } // function
- // ------------------------------------------------------------
- void BigGameBox () {
- noStroke();
- fill(122);
- // left wall
- beginShape ();
- vertex (0, 0, 0);
- vertex (0, 0, -300);
- vertex (0, 300, -300);
- vertex (0, 300, 0 );
- endShape(CLOSE);
- fill(22);
- // Wall in the back
- beginShape ();
- vertex (0, 0, -300);
- vertex (0, 300, -300);
- vertex (300, 300, -300 );
- vertex (300, 0, -300);
- endShape(CLOSE);
- fill(222);
- // Right wall
- beginShape ();
- vertex (300, 300, -300 );
- vertex (300, 0, -300);
- vertex (300, 0, 0);
- vertex (300, 300, 0);
- endShape(CLOSE);
- fill(254);
- // floor
- beginShape ();
- vertex (0, 300, 0 );
- vertex (300, 300, 0);
- vertex (300, 300, -300);
- vertex (0, 300, -300);
- endShape(CLOSE);
- fill(2);
- // ceiling
- beginShape ();
- vertex (0, 0, 0 );
- vertex (300, 0, 0);
- vertex (300, 0, -300);
- vertex (0, 0, -300);
- endShape(CLOSE);
- }
- // ------------------------------------------------------------
- class Mover {
- // the sphere
- PVector location;
- PVector velocity;
- // PVector acceleration;
- float topspeed;
- int radius = 30;
- Mover() {
- location = new PVector (100, 60, -100); // (random(width),random(height));
- velocity = new PVector(3, 3, 10);
- topspeed = 4;
- }
- void update() {
- velocity.limit(topspeed);
- location.add(velocity);
- }
- void display() {
- fill(175);
- SphereParameters(location.x, location.y, location.z, radius);
- }
- void checkEdges() {
- // We still sometimes need to refer to the individual components of a PVector
- // and can do so using the dot syntax (location.x, velocity.y, etc.)
- if (location.x+radius > 300) {
- velocity.x = abs(velocity.x) * -1;
- }
- else if (location.x-radius <= 0) {
- velocity.x = abs ( velocity.x );
- }
- //
- if (location.y+radius > 300) {
- velocity.y = abs ( velocity.y ) * -1;
- }
- else if (location.y-radius < 0) {
- velocity.y = abs ( velocity.y );
- }
- //
- if (location.z-radius < -300) {
- velocity.z = abs ( velocity.z ) ;
- }
- else if (location.z+radius > -10) {
- velocity.z = - abs ( velocity.z );
- }
- }
- //
- void SphereParameters ( float x, float y, float z,
- float w ) {
- // Position and size of a sphere
- noStroke();
- pushMatrix();
- translate ( x, y, z );
- sphere ( w );
- popMatrix();
- } // function
- //
- }// class
- //