Electronic – arduino – Simulate a bouncing ball on a LED cube

algorithmarduinoledsoftware

I just finished working on my 4x4x4 LED cube. I am trying to write an animation for a bouncing ball in the cube. Basically, a ball either 1 LED big, or 2×2 LEDs big should be moving around within the boundary of the cube. The ball should correctly bounce off the edges and continue travelling till it hits another edge. It will be like an object bouncing off surfaces in a vacuum.

I am not able to work out the algorithm to achieve this. What is the logic for simulating this ?

Best Answer

Like others have said, Google may be your friend here in terms of looking for existing code.

However, beyond that, the basics of the algorithm is pretty simple. You have variables representing the current x, y, z position of the ball, and another set that represents the vector of the ball (also in x, y, and z). With this information, it is fairly simple to do edge detection (when x, y, or z is 0 or 3 in your case).

When the ball hits an edge it should 'bounce' off the wall. This is the part that gets a little more hairy as you have to figure out what the reflection vector is based on the current vector. In order to make it interesting, you probably want to add a bit of randomness to it as well so it doesn't quickly degrade into 'pong' between two or three walls (unless that's what you are going for).

I can go hunt down some code later if you can't find anything appropriate.

--- update ---

This instructable has code for doing very simple bounce logic. Here is the basic bounce code from that project converted to all positive offsets (the original code had negative Z going "into" the screen / away from the observer) and integer values:

// size of the space
int height = 4;
int width = 4;
int depth = 4;

// Starting position of the shape
int xpos = width / 2;
int ypos = height / 2;
int zpos = depth / 2;

// Speed of the shape along each axis
int xspeed = 1;
int yspeed = 2;
int zspeed = 1;

int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom
int zdirection = 1;  // front or back

void loop()
{
  // Turn off the led at the current position: xpos, ypos, zpos
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  zpos = zpos + ( zspeed * zdirection );

  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
  // and adjust the position to be at the edge

  if (xpos >= width || xpos < 0) {
    xpos = constrain(xpos, 0, width-1);
    xdirection *= -1;
  }
  if (ypos >= height || ypos < 0) {
    ypos = constrain(ypos, 0, height-1);
    ydirection *= -1;
  }

  if (zpos >= depth || zpos < 0) {
    zpos = constrain(zpos, 0, depth-1);
    zdirection *= -1;
  }

  // turn on the led at the new position: xpos, ypos, zpos
  // apply some delay based on your update frame rate
}

Standard disclaimer applies to this code (YMMV = your mileage may vary). I don't have a cube so I haven't tried this code out. I don't know if it works and I may have made mistakes in the adjustments I made to it.

As an advanced exercise, you might consider adding code to modify the speed of a particular axis based on how far out of bounds it went, etc. You might also convert back to floating point for speed / position and then use rounding to get the actual LED position.

If you are not familiar with constrain, it is included with the Arduino libraries. The description is "Constrains a number to be within a range."