changed wagoncolor by speed

This commit is contained in:
interfisch 2018-04-08 19:47:34 +02:00
parent 153a9f0d83
commit 0cc531c7b3
3 changed files with 69 additions and 17 deletions

View File

@ -11,6 +11,8 @@
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
long lastPixelUpdate=0;
#define PIXELUPDATETIME 10
long lastRoutineUpdate=0;
@ -19,6 +21,7 @@ long lastRoutineUpdate=0;
long loopmillis=0;
uint8_t height[NUMPIXELS];
#define MAXHEIGHT 45
std::vector <Wagon> wagon_arr;
uint8_t maxid=0;
@ -77,15 +80,15 @@ void setup() {
previewHeightmap();
spawnWagon();
spawnWagon();
}
void previewHeightmap(){
int maxheight=45;
for (int i=0;i<NUMPIXELS;i++){
//uint32_t c=Wheel(height[i]*255/45);
uint8_t b=height[i]*255/45;
uint8_t b=height[i]*255/MAXHEIGHT;
uint32_t c=strip.Color(255-b,b,0);
strip.setPixelColor(i,c);
}
@ -94,11 +97,14 @@ void previewHeightmap(){
}
void spawnWagon(){
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(30, 200), 6, random(0.2, 10)/10.0,0); //spawn new wagon
wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon");
}
void loop() {
loopmillis=millis();

View File

@ -1,7 +1,6 @@
#include "wagon.h"
Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float wagonlength,float startvel,float startacc)
{
_id = id;
@ -12,6 +11,7 @@ Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,floa
_height=height;
_vel=startvel;
_acc=startacc;
_spawntime=millis();
}
@ -44,8 +44,7 @@ void Wagon::updatePhysics(float updatedelayms)
//rho Massendichte luft 1,2041
//A = 1m^2
float m=50/_wagonlength; //mass of part of a wagon
float m=40/_wagonlength; //mass of part of a wagon
_acc=0;
int cpos=(int)_pos;
@ -68,7 +67,7 @@ void Wagon::updatePhysics(float updatedelayms)
if (_vel<0){
bb*=-1;
}
float cc=AIRRES/m*_vel*_vel; //air resistance
float cc=AIRRES/m*pow(_vel,2); //air resistance
if (_vel<0){
cc*=-1;
}
@ -85,33 +84,55 @@ void Wagon::updatePhysics(float updatedelayms)
_acc*=updatedelayms/1000;
_vel += _acc;
_pos += _vel/PIXELDISTANCE;
/*Serial.print(" Vel=");
Serial.print(" Vel=");
Serial.print(_vel);
Serial.print(" Acc=");
Serial.println(_acc);*/
Serial.println(_acc);
}
float Wagon::getHeight(int p){
if (p<0){
p=0;
//p=0;
return (-p)*10;
}else if(p>=_numpixels){
p=_numpixels-1;
//p=_numpixels-1;
return (p-_numpixels)*10;
}
return _height[p];
}
void Wagon::updateGraphics()
{
for(int i=_pos;i>_pos-_wagonlength;i--){
uint32_t c=_strip->Color(0,255,0);
if (i==int(_pos)){
c=_strip->Color(0,255,100);
float wagonfeathering=2;
for(int i=_pos+wagonfeathering;i>_pos-_wagonlength-wagonfeathering;i--){
float featherbrightness=1;
if (i>_pos){ //in front of wagon
featherbrightness=1 - (i-_pos)/wagonfeathering;
}else if (i<_pos-_wagonlength){ //behind of wagon
featherbrightness=1 - (_pos-_wagonlength -i)/wagonfeathering;
}
if (featherbrightness<=0){ //distpercent between 0 and 1. 1-> full brightness, 0-> feathering distance away
featherbrightness=0;
}
//uint32_t c=_strip->Color(0,255*featherbrightness,0);
//uint32_t c=Wheel(_height[i]/45.0*255,featherbrightness);
//uint8_t b=_height[i]*255/45;
uint8_t b=abs(_vel)*255.0;
uint32_t c=_strip->Color(b*featherbrightness,(255-b)*featherbrightness,0);
_strip->setPixelColor(i,c);
}
//_strip->setPixelColor(10,_strip->Color(255,0,0));
}
int Wagon::pos()
@ -124,11 +145,33 @@ int Wagon::id()
return _id;
}
long Wagon::spawntime()
{
return _spawntime;
}
bool Wagon::alive()
{
if (_pos>_numpixels){
/*if (_pos>_numpixels){
return false;
}*/
if (millis()>_spawntime+30*1000){ //too old
return false;
}
return true;
}
uint32_t Wagon::Wheel(byte WheelPos,float brightness) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return _strip->Color(255 - WheelPos * 3*brightness, 0, WheelPos * 3*brightness);
}
if(WheelPos < 170) {
WheelPos -= 85;
return _strip->Color(0, WheelPos * 3*brightness, 255 - WheelPos * 3*brightness);
}
WheelPos -= 170;
return _strip->Color(WheelPos * 3*brightness, 255 - WheelPos * 3*brightness, 0);
}

View File

@ -13,8 +13,10 @@ class Wagon
void updatePhysics(float updatedelayms);
void updateGraphics();
float getHeight(int p);
uint32_t Wheel(byte WheelPos,float brightness);
int pos();
int id();
long spawntime();
bool alive();
private:
int _numpixels;
@ -24,6 +26,7 @@ class Wagon
float _acc;
float _wagonlength;
uint8_t *_height;
long _spawntime;
};
#endif