Skip to content

Commit

Permalink
Adding 2 ball track modes
Browse files Browse the repository at this point in the history
With and without collisions
  • Loading branch information
pjhatch committed Jul 12, 2020
1 parent 341d9d1 commit a20358b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
31 changes: 19 additions & 12 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,13 +2528,6 @@ typedef struct Ball {
float impactVelocity;
float height;
} ball;
// modified for balltrack mode
typedef struct Ballt {
unsigned long lastBounceUpdate;
float mass; // could fix this to be = 1. if memory is an issue
float velocity;
float height;
} ballt;
/*
* Bouncing Balls Effect
*/
Expand Down Expand Up @@ -2594,7 +2587,15 @@ uint16_t WS2812FX::mode_bouncing_balls(void) {
/*
* bouncing balls on a track track Effect modified from Air Cookie's bouncing balls
*/
uint16_t WS2812FX::mode_balltrack(void) {
// modified for balltrack mode
typedef struct Ballt {
unsigned long lastBounceUpdate;
float mass; // could fix this to be = 1. if memory is an issue
float velocity;
float height;
} ballt;

uint16_t WS2812FX::ball_track(bool collide) {
//allocate segment data
uint16_t maxNumBalls = 16;
uint16_t dataSize = sizeof(ballt) * maxNumBalls;
Expand Down Expand Up @@ -2641,23 +2642,22 @@ uint16_t WS2812FX::mode_balltrack(void) {
balls[i].height=thisHeight;
}
// check for collisions
bool docol=true;
if(docol){
if(collide){
for(uint8_t j= i+1; j < numBalls; j++){
if(balls[j].velocity != balls[i].velocity) {
// tcollided + balls[j].lastBounceUpdate is acutal time of collision (this keeps precision with long to float conversions)
float tcollided= ( cfac*(balls[i].height-balls[j].height) +
balls[i].velocity*float(balls[j].lastBounceUpdate-balls[i].lastBounceUpdate))/
(balls[j].velocity-balls[i].velocity);

if( (tcollided>2)&&(tcollided<float(time-balls[j].lastBounceUpdate))){ // 2ms minimum to avoid duplicate bounces
if( (tcollided>2)&&(tcollided<float(time-balls[j].lastBounceUpdate))){ // 2ms minimum to avoid duplicate bounces
balls[i].height=balls[i].height + balls[i].velocity*(tcollided+float(balls[j].lastBounceUpdate-balls[i].lastBounceUpdate))/cfac;
balls[j].height=balls[i].height;
balls[i].lastBounceUpdate=(unsigned long)(tcollided+.5)+balls[j].lastBounceUpdate;
balls[j].lastBounceUpdate=balls[i].lastBounceUpdate;
float vtmp=balls[i].velocity;
balls[i].velocity=((balls[i].mass-balls[j].mass)*vtmp + 2.*balls[j].mass*balls[j].velocity)/(balls[i].mass+balls[j].mass);
balls[j].velocity=((balls[j].mass-balls[i].mass)*balls[j].velocity + 2.*balls[i].mass*vtmp) /(balls[i].mass+balls[j].mass);
balls[j].velocity=((balls[j].mass-balls[i].mass)*balls[j].velocity + 2.*balls[i].mass*vtmp) /(balls[i].mass+balls[j].mass);
//balls[i].height=balls[i].height + balls[i].velocity*(time-balls[i].lastBounceUpdate)/cfac;
thisHeight= balls[i].height + balls[i].velocity*(time-balls[i].lastBounceUpdate)/cfac;
}
Expand All @@ -2683,6 +2683,13 @@ uint16_t WS2812FX::mode_balltrack(void) {
return FRAMETIME;
}

uint16_t WS2812FX::mode_balltrack(void) {
return ball_track(false);
}
uint16_t WS2812FX::mode_balltrack_collide(void) {
return ball_track(true);
}


/*
* Sinelon stolen from FASTLED examples
Expand Down
12 changes: 8 additions & 4 deletions wled00/FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
#define IS_REVERSE ((SEGMENT.options & REVERSE ) == REVERSE )
#define IS_SELECTED ((SEGMENT.options & SELECTED ) == SELECTED )

#define MODE_COUNT 113
#define MODE_COUNT 114

#define FX_MODE_STATIC 0
#define FX_MODE_BLINK 1
Expand Down Expand Up @@ -213,6 +213,7 @@
#define FX_MODE_FLOW 110
#define FX_MODE_CHUNCHUN 111
#define FX_MODE_BALLTRACK 112
#define FX_MODE_BALLTRACK_COLLIDE 113
class WS2812FX {
typedef uint16_t (WS2812FX::*mode_ptr)(void);

Expand Down Expand Up @@ -413,6 +414,7 @@ class WS2812FX {
_mode[FX_MODE_FLOW] = &WS2812FX::mode_flow;
_mode[FX_MODE_CHUNCHUN] = &WS2812FX::mode_chunchun;
_mode[FX_MODE_BALLTRACK] = &WS2812FX::mode_balltrack;
_mode[FX_MODE_BALLTRACK_COLLIDE] = &WS2812FX::mode_balltrack_collide;
_brightness = DEFAULT_BRIGHTNESS;
currentPalette = CRGBPalette16(CRGB::Black);
targetPalette = CloudColors_p;
Expand Down Expand Up @@ -608,7 +610,8 @@ class WS2812FX {
mode_phased_noise(void),
mode_flow(void),
mode_chunchun(void),
mode_balltrack(void);
mode_balltrack(void),
mode_balltrack_collide(void);

private:
NeoPixelWrapper *bus;
Expand Down Expand Up @@ -656,7 +659,8 @@ class WS2812FX {
tricolor_chase(uint32_t, uint32_t),
twinklefox_base(bool),
spots_base(uint16_t),
phased_base(uint8_t);
phased_base(uint8_t),
ball_track(bool collide);

CRGB twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat);
CRGB pacifica_one_layer(uint16_t i, CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff);
Expand Down Expand Up @@ -696,7 +700,7 @@ const char JSON_mode_names[] PROGMEM = R"=====([
"Twinklefox","Twinklecat","Halloween Eyes","Solid Pattern","Solid Pattern Tri","Spots","Spots Fade","Glitter","Candle","Fireworks Starburst",
"Fireworks 1D","Bouncing Balls","Sinelon","Sinelon Dual","Sinelon Rainbow","Popcorn","Drip","Plasma","Percent","Ripple Rainbow",
"Heartbeat","Pacifica","Candle Multi", "Solid Glitter","Sunrise","Phased","Twinkleup","Noise Pal", "Sine","Phased Noise",
"Flow","Chunchun","Ball Track"
"Flow","Chunchun","Ball Track","Ball Track Collide"
])=====";


Expand Down
1 change: 1 addition & 0 deletions wled00/wled00.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* WLED Arduino IDE compatibility file.
*
Expand Down

0 comments on commit a20358b

Please sign in to comment.