2
\$\begingroup\$

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 2x2 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 ?

\$\endgroup\$
6
  • \$\begingroup\$ Is there gravity in your simulation? Or can gravity be ignored? \$\endgroup\$
    – walrii
    Commented Apr 18, 2013 at 11:31
  • \$\begingroup\$ How discrete is the simulation? For a 1 LED ball, is only one LED ever lit at a time or will 2/more LEDs be lit as the ball flows from one position to the next? \$\endgroup\$
    – walrii
    Commented Apr 18, 2013 at 11:36
  • \$\begingroup\$ @walrii No gravity. Only the ball is lit. No paths/tails. \$\endgroup\$
    – asheeshr
    Commented Apr 18, 2013 at 12:04
  • \$\begingroup\$ @AshRj If you want a detailed algorithm, then I can get one done in an hour (I have other stuff to do first) \$\endgroup\$
    – Polar
    Commented Apr 18, 2013 at 16:14
  • \$\begingroup\$ @AshRj I have, don't see how that bears much relevance to this though :( \$\endgroup\$
    – Polar
    Commented Apr 18, 2013 at 16:53

2 Answers 2

3
\$\begingroup\$

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."

\$\endgroup\$
0
\$\begingroup\$

I think Google may be your friend in this:

http://voodoovb.vbgamer.com/bouncing.html

http://www.c.happycodings.com/Games_and_Graphics/code8.html

I think I would start in 2 dimensions and try to get that working before moving to 3 dimensions.

Best of luck. It looks like a challenging thing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.