void ProcessAction( Action a){ // printf("process action, players %d\n",race.players.size()); Race::PlayersIterator pli; race.mutex_on_players.lock(); pli = race.FindPlayer(a.first); race.mutex_on_players.unlock(); Player & pl = (*pli); pl.mutex_on_data.lock(); // reset action state: pl.state &= ~Player::BRAKING; pl.state &= ~Player::BOOSTING; switch(a.second.header){ case COMMAND_INCREASE_SPEED: acc(10,pl); break; case COMMAND_DECREASE_SPEED: acc(-10,pl); break; case COMMAND_TURN_LEFT: steer(0.1f,pl); break; case COMMAND_TURN_RIGHT: steer(-0.1f,pl); break; case COMMAND_BRAKE: brake(pl); break; case COMMAND_RESTART: restart(pl); break; } pl.mutex_on_data.unlock(); }
void moveships(void) { register int n,i,p; int target; target = goodbogey(0); if (target != 0) for (n=0; amtable[n] < MAXSHIPS; n++) if (amtable[n] != player) shiplist[amtable[n]].course = intercept(amtable[n], target -1); target = goodbogey(JAPANESE); if (target != 0) for (n=0; japtable[n] < MAXSHIPS; n++) if (japtable[n] != player) shiplist[japtable[n]].course = intercept(japtable[n], target -1); for (n=0; n < MAXSHIPS; n++) { firedflack[n] = 0; firedguns[n] = 0; if (shiplist[n].torps && shiplist[n].hits && n != player && n != MIDWAY) drdc((shiplist[n].course = shiplist[shiplist[n].flagship].course), &shiplist[n].row, &shiplist[n].col); } if (player != MIDWAY) drdc(shiplist[player].course, &shiplist[player].row, &shiplist[player].col); if (automatic >= 0) plotships(); for (n=0; amtable[n] < MAXSHIPS; n++) { if (shiplist[amtable[n]].hits && shiplist[amtable[n]].torps) { for (i = 0; japtable[i] < MAXSHIPS; i++) { if (shiplist[japtable[i]].hits && shiplist[japtable[i]].torps) { if (range(shiplist[amtable[n]].row, shiplist[amtable[n]].col, shiplist[japtable[i]].row, shiplist[japtable[i]].col) < 25) { /* within sight */ for (p=japtable[i]; p < MAXSHIPS && shiplist[p].flagship == japtable[i]; p++) if (p != player) fireguns(p, amtable[n]); for (p=amtable[n]; p < MAXSHIPS && shiplist[p].flagship == amtable[n]; p++) if (p != player) fireguns(p, japtable[i]); newbogey(japtable[i]); newbogey(amtable[n]); } } } } } fly(american,0,1); fly(japanese,0,0); fly(amscouts,1,1); fly(japscouts,1,0); if (automatic >= 0) plotplanes(); steer(american,0,1); steer(japanese,0,0); steer(amscouts,1,1); steer(japscouts,1,0); }
static void think(void) { float dir; int wantedAngle; if (--self->aiActionTime <= 0) { findAITarget(); } wantedAngle = steer(); if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD) { dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1; dir *= TURN_SPEED; self->angle += dir; self->angle = mod(self->angle, 360); } applyFighterThrust(); }
b2Vec2 Enemy::pursue(b2Vec2 target, b2Vec2 tVel, float dist) { b2Vec2 between = target - getPosition(); float distance = between.Length(); float speed = body_->GetLinearVelocity().Length(); int maxTimePrediction = 1000; int timePrediction = 1000; b2Vec2 steer(0, 0); if (speed <= distance / maxTimePrediction) { timePrediction = maxTimePrediction; steer = arrive(target, dist); } else { timePrediction = distance / speed; b2Vec2 newTarget(tVel); newTarget *= timePrediction; newTarget += target; steer = arrive(newTarget, dist); } return steer; }
//float* Boid2d::cohesion(vector<Boid2d*>* b, float *vec) { float* Boid2d::cohesion( float *vec) { float cohesiondist = flockPtr->distCohesion; // float vec[] = {0.f, 0.f};//new float[2]; int count = 0; for (int i = 0; i < flockPtr->boids.size(); i++) { Boid2d * other = flockPtr->boids.at(i); float dx = other->x - x; float dy = other->y - y; float d = ABS(dx) + ABS(dy); if (d > 0 && d < cohesiondist) { count++; vec[0] += other->x;// dx; vec[1] += other->y;// dy; } } if (count > 0) { float invCount = 1.f / count; vec[0] *= invCount; vec[1] *= invCount; steer(vec, 1); } return vec; }
// Function that checks and modifies the distance // of a boid if it breaks the law of separation. Pvector Boid::Separation(std::vector<Boid> boids) { // If the boid we're looking at is a predator, do not run the separation // algorithm // Distance of field of vision for separation between boids float desiredseparation = 20; Pvector steer(0, 0); int count = 0; // For every boid in the system, check if it's too close for (int i = 0; i < boids.size(); i++) { // Calculate distance from current boid to boid we're looking at float d = location.distance(boids[i].location); // If this is a fellow boid and it's too close, move away from it if ((d > 0) && (d < desiredseparation)) { Pvector diff(0, 0); diff = diff.subTwoVector(location, boids[i].location); diff.normalize(); diff.divScalar(d); // Weight by distance steer.addVector(diff); count++; } // If current boid is a predator and the boid we're looking at is also // a predator, then separate only slightly if ((d > 0) && (d < desiredseparation) && predator == true && boids[i].predator == true) { Pvector pred2pred(0, 0); pred2pred = pred2pred.subTwoVector(location, boids[i].location); pred2pred.normalize(); pred2pred.divScalar(d); steer.addVector(pred2pred); count++; } // If current boid is not a predator, but the boid we're looking at is // a predator, then create a large separation Pvector else if ((d > 0) && (d < desiredseparation + 70) && boids[i].predator == true) { Pvector pred(0, 0); pred = pred.subTwoVector(location, boids[i].location); pred.mulScalar(900); steer.addVector(pred); count++; } } // Adds average difference of location to acceleration if (count > 0) steer.divScalar((float)count); if (steer.magnitude() > 0) { // Steering = Desired - Velocity steer.normalize(); steer.mulScalar(maxSpeed); steer.subVector(velocity); steer.limit(maxForce); } return steer; }
b2Vec2 Enemy::evade(b2Vec2 pursuer, b2Vec2 pVel, float dist) { b2Vec2 between = pursuer - getPosition(); float distance = between.Length(); float speed = body_->GetLinearVelocity().Length(); int maxTimePrediction = 1000; int timePrediction = 1000; b2Vec2 steer(0, 0); if (speed <= distance / maxTimePrediction) { timePrediction = maxTimePrediction; steer = arrive(pursuer, dist); } else { timePrediction = distance / speed; b2Vec2 newPursuer(pVel); newPursuer *= timePrediction; newPursuer += pursuer; steer = flee(newPursuer, dist); } return steer; }
void CarSuspension::SetSteering(const btScalar & value) { btScalar alpha = -value * info.steering_angle * M_PI / 180.0; steering_angle = 0.0; if (alpha != 0.0) { steering_angle = atan(1.0 / (1.0 / tan(alpha) - tan(info.ackermann * M_PI / 180.0))); } btQuaternion steer(steering_axis, steering_angle); orientation = steer * orientation_ext; }
//Idle flocking b2Vec2 Enemy::idleFlock() { b2Vec2 sum(0, 0); //Swarm to shapes, similar or no int count = 0; for (Enemy* v : swarm_) { if (v != this) { b2Vec2 myPos = getPosition(); b2Vec2 theirPos = v->getPosition(); b2Vec2 between = theirPos - myPos; float minDist = v->getSize() + size_; float dist = between.Length(); //Don't flock to cowards, and only those in range if (v->getStateOfMind() != FLEE && between.Length() < flockRange_) { b2Vec2 steer(0, 0); //If we're same, attract if (v->getVertices() == vertices_) { LJVal val(flockSameLJ); val.attrAtten *= flockN_[brainStem_.friendliness]; steer = LJP(theirPos, val); //*//Friendliness } //Otherwise just separate else steer = LJP(theirPos, sepOnlyLJ); if (steer.x != 0 && steer.y != 0) { sum += steer; count++; } } } } //get average if (count > 0) { sum.x /= static_cast<float>(count); sum.y /= static_cast<float>(count); } else sum = b2Vec2_zero; return sum; }
void NawlzBlahParticle :: wander() { wanderTheta += ofRandom( -wanderChange, wanderChange ); // Randomly change wander theta circle = vel; // Start with velocity circle.normalize(); // Normalize to get heading circle *= wanderDistance; // Multiply by distance circle += loc; // Make it relative to boid's location circleOffSet = ofxVec2f( wanderRadius * cos( wanderTheta ), wanderRadius * sin( wanderTheta ) ); circleTarget = circle + circleOffSet; acc += steer( circleTarget ); }
void Boid::wander(){ float wanderR = 16.0f; // Radius for our "wander circle" float wanderD = 60.0f; // Distance for our "wander circle" float change = 0.25f; wanderTheta += ofRandom(-change,change); // Randomly change wander theta // Now we have to calculate the new location to steer towards on the wander circle ofVec3f circleloc = vel; // Start with velocity circleloc.normalize(); // Normalize to get heading circleloc *= wanderD; // Multiply by distance circleloc += *this; // Make it relative to boid's location ofVec3f circleOffSet = ofVec3f(wanderR*cos(wanderTheta),0,wanderR*sin(wanderTheta)); ofVec3f target = circleloc + circleOffSet; acc += steer(target,false); // Steer towards it }
//Flee flocking b2Vec2 Enemy::fleeFlock() { b2Vec2 sum(0, 0); //Swarm to shapes, similar or no int count = 0; for (Enemy* v : swarm_) { if (v != this) { b2Vec2 myPos = getPosition(); b2Vec2 theirPos = v->getPosition(); b2Vec2 between = theirPos - myPos; float minDist = v->getSize() + size_; float dist = between.Length(); if (between.Length() < flockRange_) { b2Vec2 steer(0, 0); //If we're same, attract if (v->getVertices() == vertices_) steer = LJP(theirPos, fleeSameLJ); //*//Friendliness //Otherwise just separate else steer = LJP(theirPos, fleeOtherLJ); //*//personal space? if (steer.x != 0 && steer.y != 0) { sum += steer; count++; } } } } //get average if (count > 0) { sum.x /= static_cast<float>(count); sum.y /= static_cast<float>(count); } else sum = b2Vec2_zero; return sum; }
/* * Function called from main block of code * Returns true (1) if sucessful, 0, if unsucessful */ int Controller() { // Robot will turn this angle in this timestep // DEFAULT = no change in current heading double angleOfTurn = 0; // INITIALIZE VALUES initialize(); // Checks for that remode control option is disabled if (rcDisabled) { return false; } // Set ERROR flag when updating values int ERROR = updateVar(); // Runs linear controller w/Control Law (calculates next step) linearAngle = linearController(); // Checks/updates computer model to see if robot is on // desired trajectory modelAngle = modelController(); //CURRENTLY SET FOR ALL SENSOR INPUT int c1 = 1; int c2 = 0; if (ERROR == true) { //STEER WITHOUT SENSORY INPUT c1 = 0; c2 = 1; } // CONTROL LAW - MODEL VS. SENSOR DATA angleOfTurn = c1 * linearAngle + c2 * modelAngle; // Send commands to steering board (set values of enums) steer(angleOfTurn); // Update robot model of location updateModel(angleOfTurn); return ERROR; }
//Chase flocking b2Vec2 Enemy::chaseFlock() { b2Vec2 sum(0, 0); //Swarm to shapes int count = 0; for (Enemy* v : swarm_) { if (v != this) { b2Vec2 myPos = getPosition(); b2Vec2 theirPos = v->getPosition(); b2Vec2 between = theirPos - myPos; float minDist = v->getSize() + size_; float dist = between.Length(); if (between.Length() < flockRange_) { b2Vec2 steer(0, 0); if (v->getVertices() == vertices_) steer = LJP(theirPos, sepOnlyLJ); //*//friendliness? else steer = LJP(theirPos, sepOnlyLJ); //*// personal space? if (steer.x != 0 && steer.y != 0) { sum += steer; count++; } } } } //get average if (count > 0) { sum.x /= static_cast<float>(count); sum.y /= static_cast<float>(count); } else sum = b2Vec2_zero; return sum; }
/** Updates the player kart, called once each timestep. */ void PlayerController::update(float dt) { // Don't do steering if it's replay. In position only replay it doesn't // matter, but if it's physics replay the gradual steering causes // incorrect results, since the stored values are already adjusted. if (!history->replayHistory()) steer(dt, m_steer_val); if (World::getWorld()->isStartPhase()) { if (m_controls->m_accel || m_controls->m_brake || m_controls->m_fire || m_controls->m_nitro) { // Only give penalty time in SET_PHASE. // Penalty time check makes sure it doesn't get rendered on every // update. if (m_penalty_time == 0.0 && World::getWorld()->getPhase() == WorldStatus::SET_PHASE) { displayPenaltyWarning(); m_penalty_time = stk_config->m_penalty_time; } // if penalty_time = 0 m_controls->m_brake = false; m_controls->m_accel = 0.0f; } // if key pressed return; } // if isStartPhase if (m_penalty_time>0.0) { m_penalty_time-=dt; return; } // Only accept rescue if there is no kart animation is already playing // (e.g. if an explosion happens, wait till the explosion is over before // starting any other animation). if ( m_controls->m_rescue && !m_kart->getKartAnimation() ) { new RescueAnimation(m_kart); m_controls->m_rescue=false; } } // update
motorVels SwerveDrive::doVelTranslation(const geometry_msgs::Twist * velMsg) { motorVels output; double velMagnitude = getVelMagnitude(velMsg); //santity check, if the magnitude is close to zero stop if (velMagnitude < VEL_ERROR) { output = stop(output); //account for the special case where y=0 } else if(fabs(velMsg->linear.y) >= VEL_ERROR) { const double turnAngle = atan2(velMsg->linear.y,velMsg->linear.x); direction = getDir(velMsg->linear.x); output = steer(output, velMagnitude, turnAngle); } else { ROS_INFO("drive straight"); output.frontLeftMotorV = output.backLeftMotorV = output.frontRightMotorV = output.backRightMotorV = velMsg->linear.x; output.frontRightAng = output.frontLeftAng = 0; } return output; }
// Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location Vec2f Boid::cohesion(vector<Boid> &boids) { float neighbordist = 15.0; Vec2f sum; // Start with empty vector to accumulate all locations int count = 0; for (int i = 0 ; i < boids.size(); i++) { Boid &other = boids[i]; float d = loc.distance(other.loc); if ((d > 0) && (d < neighbordist)) { sum += other.loc; // Add location count++; } } if (count > 0) { sum /= (float)count; return steer(sum); // Steer towards the location } return sum; }
// Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location ofPoint Boid::cohesion(vector<Boid*> boids) { float neighbordist = 50.0; ofPoint sum; // Start with empty vector to accumulate all locations int count = 0; for (int i = 0 ; i < boids.size(); i++) { Boid& other = *boids[i]; float d = ofDist(loc.x, loc.y, other.loc.x, other.loc.y); if ((d > 0) && (d < neighbordist)) { sum += other.loc; // Add location count++; } } if (count > 0) { sum /= (float)count; return steer(sum, false); // Steer towards the location } return sum; }
// Cohesion // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location ADDAC_PVector ADDAC_Boid::cohesion(ADDAC_Boid boids[4]) { float neighbordist = cohesionV; ADDAC_PVector sum; // Start with empty vector to accumulate all locations int count = 0; for (int i = 0 ; i < 4; i++) { ADDAC_Boid other = boids[i]; float d = dist(pos, other.pos); if ((d > 0) && (d < neighbordist)) { // Add location sum.add(other.pos); count++; } } if (count > 0) { sum.div((float)count); return steer(sum, false); // Steer towards the location } return sum; }
osg::Vec3 Flake::cohesion() { // for every flake, calc vector to steer to the center of nearby flakes float neighbordist = 100.0; osg::Vec3 sum = osg::Vec3(0,0,0); int count = 0; for (unsigned int i=0; i<_storm->flakes.size(); i++) { Flake* other = _storm->flakes[i]; float d = (_loc-(other->_loc)).length(); //distance if ((d > 0) && (d < neighbordist)) { sum += other->_loc; // Add location count++; } } if (count > 0) { sum /= (float)count; return steer(sum,false); // Steer towards the location } return sum; }
Vector Boid::separate(vector<Boid*>& boids) { float desiredseparation = 50.0f; Vector steer(0, 0); int count = 0; vector<Boid*>::iterator it; for (it = boids.begin(); it!=boids.end(); ++it) { float d = Vector::dist(*m_pos, (*it)->getPos()); // 0 means it's the same boid if ((d > 0) && (d < desiredseparation)) { // Calculate vector pointing away from neighbor Vector diff = Vector::sub(*m_pos, (*it)->getPos()); diff.normalise(); diff.div(d); // Weight by distance steer.add(diff); count++; // Keep track of how many } } // Average -- divide by how many if (count > 0) { steer.div(float(count)); } // As long as the vector is greater than 0 if (steer.mag() > 0) { // Implement Reynolds: Steering = Desired - Velocity steer.normalise(); steer.byScalar(m_maxspeed); steer.sub(*m_velocity); steer.limit(m_maxforce); } return steer; }
//-------------------------------------------------------------- ofPoint vehicle::slopes(ofVec2f* gradient){ ofPoint desired; // Predict location 5 (arbitrary choice) frames ahead ofPoint predict(velocity); predict *= 10; ofPoint futureLocation(location); futureLocation += predict; ofPoint leave(location); if (futureLocation.x < border) leave.x = screenWidth; if (futureLocation.y < border) leave.y = screenHeight; if (futureLocation.x > screenWidth-border) leave.x = 0; if (futureLocation.y > screenHeight-border) leave.y = 0; leave -= location; leave.normalize(); leave *= velocity.length(); leave += velocity; desired.set(leave); desired.normalize(); desired *= topSpeed; ofPoint steer(desired); if (desired.length() != 0) { steer -= velocity; // PVector steer = PVector.sub(desired, velocity); steer.limit(maxForce); } return steer; }
float* Boid2d::flockfull(const float amount, float *vec) { // float * vec = new float[2]; float *sep = new float[2]; float *ali = new float[2]; float *coh = new float[2]; float *attrForce = new float[2]; sep[0] = 0.0f; sep[1] = 0.0f; ali[0] = 0.0f; ali[1] = 0.0f; coh[0] = 0.0f; coh[1] = 0.0f; attrForce[0] = 0.0f; attrForce[1] = 0.0f; // float attrForce[] = {0.f, 0.f}; //new float[2]; int countsep = 0, countali = 0, countcoh = 0; float separatedist = flockPtr->distSeparation; float aligndist = flockPtr->distAlign; float cohesiondist = flockPtr->distCohesion; float invD = 0; // boolean hasAttractionPoints = flock.hasAttractionPoints(); // main full loop track all forces boid other boids for (int i = 0; i < flockPtr->boids.size(); i++) { Boid2d * other = flockPtr->boids.at(i); float dx = other->x - x; float dy = other->y - y; float d = ABS(dx) + ABS(dy); if (d <= 1e-7) continue; // sep if (d < separatedist) { countsep++; invD = 1.f / d; sep[0] -= dx * invD; sep[1] -= dy * invD; } // coh if (d < cohesiondist) { countcoh++; coh[0] += other->x; coh[1] += other->y; } // ali if (d < aligndist) { countali++; ali[0] += other->vx; ali[1] += other->vy; } } if (countsep > 0) { const float invForSep = flockPtr->separate / (float) countsep; sep[0] *= invForSep; sep[1] *= invForSep; } if (countali > 0) { // final float invForAli = 1f / (float) countali; const float invForAli = flockPtr->align / (float) countali; ali[0] *= invForAli; ali[1] *= invForAli; } if (countcoh > 0) { const float invForCoh = flockPtr->cohesion / (float) countcoh; coh[0] *= invForCoh; coh[1] *= invForCoh; coh = steer(coh, 1); } // if using extra forces, place here // sep[0] *= flock.separate; // sep[1] *= flock.separate; // // ali[0] *= flock.align; // ali[1] *= flock.align; // // coh[0] *= flock.cohesion; // coh[1] *= flock.cohesion; // other forces if (flockPtr->hasAttractionPoints()) { for (int i = 0; i < flockPtr->attractionPoints.size(); i++) { AttractionPoint2d * point = flockPtr->attractionPoints.at(i); float dx = point->x - x; float dy = point->y - y; float d = ABS(dx) + ABS(dy); if (d <= 1e-7) continue; if (d > point->sensorDist) continue; // inbounds, calc float invForce = point->force / d * attr;// newww ////flockPtr->attraction ; // neww dx *= invForce; dy *= invForce; attrForce[0] += dx; attrForce[1] += dy; } } vec[0] = sep[0] + ali[0] + coh[0] + attrForce[0]; vec[1] = sep[1] + ali[1] + coh[1] + attrForce[1]; const float d = ABS(vec[0]) + ABS(vec[1]); if (d > 0) { float invDist = amount / d; vec[0] *= invDist; vec[1] *= invDist; } vec[0] *= amount; vec[1] *= amount; delete[] sep; delete[] ali; delete[] coh; delete[] attrForce; return vec; }
void Squid::seek(float $x, float $y) { steer(ofxVec3f($x, $y), false); }
int main(int argc, char** argv) { if( argc<2 ) { std::cout << " storehist_fullmc: " << std::endl ; std::cout << " Author : Hengne Li @ LPSC 2010 " << std::endl ; std::cout << " Functionality: store hists for track match efficiency." << std::endl; std::cout << " " << std::endl; std::cout << " usage: storehist storehist.config" << std::endl ; exit(1) ; } std::cout << " storehist: " << std::endl ; std::cout << " Author : Hengne Li @ LPSC 2010 " << std::endl ; // readme: // open steering file names config steer(std::string((const char*)argv[1])); // output root file name std::string OutRootFile = steer.getString("OutRootFile"); // input file name std::string InRootFile = steer.getString("InRootFile"); // Electron Basis, if true, it will draw twice for each electron bool ElectronBasis = steer.getBool("ElectronBasis"); // Dimension of efficiency dependece vars: e.g. elecPt 1D , SET-Lumi 2D int DepVarDimension = steer.getInt("DepVarDimension"); if (DepVarDimension>3) { std::cout << " Error:: DepVarDimension maximum to be 3 dimintion, no more, modify me if needed! " << std::endl; abort(); } if (DepVarDimension==0) { std::cout << " Warning:: DepVarDimension should at least be 1 -- set it to be 1 " << std::endl; DepVarDimension = 1; } // Dependence Variable is of electrons? // For SET or Lumi these Event based variable, it should be set to false, binning // will be written as "set>0&&set<20" ; // For elecPt these electron based variable, it should be set to true, because the binning should be written as // "elecPt[0]>0&&elecPt[0]<20". bool ElectronDepVar1(false), ElectronDepVar2(false), ElectronDepVar3(false); ElectronDepVar1 = steer.getBool("ElectronDepVar"); // only if DepVarDimension above 1, the following will be read if (DepVarDimension>1) ElectronDepVar2 = steer.getBool("ElectronDepVar2"); // Tree name std::string TreeName = steer.getString("TreeName"); // histogram names : e.g. hnum hdeno or htrk0, htrk1, htrk2 std::vector<std::string> HistNames = steer.getStringArray("HistNames"); // efficiency dependence variable name std::string DepVarName1, DepVarName2, DepVarName3; DepVarName1 = steer.getString("DepVarName"); // only if DepVarDimension above 1, the following will be read if (DepVarDimension>1) DepVarName2 = steer.getString("DepVarName2"); if (DepVarDimension>2) DepVarName3 = steer.getString("DepVarName3"); // dependence variable bins std::vector<double> DepVarBins1, DepVarBins2, DepVarBins3; DepVarBins1 = steer.getDoubleArray("DepVarBins"); // only if DepVarDimension above 1, the following will be read if (DepVarDimension>1) DepVarBins2 = steer.getDoubleArray("DepVarBins2"); if (DepVarDimension>2) DepVarBins3 = steer.getDoubleArray("DepVarBins3"); // base selection string to be applied everywhere std::string BaseSelection = steer.getString("BaseSelection"); // First electron selection array for the corresponding histnames std::vector<std::string> SelectionVecE1 = steer.getStringArray("SelectionVecE1"); // Second electron selection array for the corresponding histnames std::vector<std::string> SelectionVecE2 = steer.getStringArray("SelectionVecE2"); // initialize // root files TFile* finput = new TFile(InRootFile.c_str()); TFile* foutput = new TFile(OutRootFile.c_str(), "recreate"); // tree TTree* tree = (TTree*)finput->Get(TreeName.c_str()); // Dep variable binning int Nbins1 = DepVarBins1.size()-1; int Nbins2(1), Nbins3(1); if (DepVarDimension>1) Nbins2 = DepVarBins2.size()-1; if (DepVarDimension>2) Nbins3 = DepVarBins3.size()-1; double Bins1[ (const int)(Nbins1+1) ]; double Bins2[ (const int)(Nbins2+1) ]; double Bins3[ (const int)(Nbins3+1) ]; for (int i=0; i<(int)DepVarBins1.size(); i++){ Bins1[i] = DepVarBins1.at(i); } if (DepVarDimension>1) { for (int i=0; i<(int)DepVarBins2.size(); i++){ Bins2[i] = DepVarBins2.at(i); } } if (DepVarDimension>2) { for (int i=0; i<(int)DepVarBins3.size(); i++){ Bins3[i] = DepVarBins3.at(i); } } // dependence histograms const int Nhists = HistNames.size(); std::vector<TH1*> Hists; for (int i=0; i<Nhists; i++){ char hname[100]; TH1* hist; sprintf(hname, "%s", HistNames.at(i).c_str()); if (DepVarDimension==1) { hist = new TH1D(hname, hname, Nbins1, Bins1); } else if (DepVarDimension==2) { hist = new TH2D(hname, hname, Nbins1, Bins1, Nbins2, Bins2); } else if (DepVarDimension==3) { hist = new TH3D(hname, hname, Nbins1, Bins1, Nbins2, Bins2, Nbins3, Bins3); } else{ std::cout << "Error:: DepVarDimension can be 1 to 3, no more. Modify me if needed!" << std::endl; abort(); } hist->Sumw2(); Hists.push_back(hist); } // Draw hists for (int i=0; i<Nhists; i++){ char draw[100], sele[500]; // if variables are of electrons char str_elecdep1[5], str_elecdep2[5], str_elecdep3[5]; sprintf(str_elecdep1, ""); sprintf(str_elecdep2, ""); sprintf(str_elecdep3, ""); if (ElectronDepVar1) sprintf(str_elecdep1, "[0]"); if (DepVarDimension>1 && ElectronDepVar2) sprintf(str_elecdep2, "[0]"); if (DepVarDimension>2 && ElectronDepVar3) sprintf(str_elecdep2, "[0]"); // print draw string if (DepVarDimension==1) { sprintf(draw, "%s%s>>%s", DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else if (DepVarDimension==2) { sprintf(draw, "%s%s:%s%s>>%s", DepVarName2.c_str(), str_elecdep2, DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else if (DepVarDimension==3) { sprintf(draw, "%s%s:%s%s:%s%s>>%s", DepVarName3.c_str(), str_elecdep3, DepVarName2.c_str(), str_elecdep2, DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else { std::cout << " Error:: DepVarDimension can be 1 to 3, no more. Modify me if needed!" << std::endl; abort(); } // print sele string sprintf(sele, "(%s)&&(%s)", BaseSelection.c_str(), SelectionVecE1.at(i).c_str()); std::cout << " -- draw = " << draw << std::endl; std::cout << " -- sele = " << sele << std::endl; // draw tree->Draw(draw, sele); // if electron basis, draw for the 2nd electron if (ElectronBasis) { // if variables are of electrons sprintf(str_elecdep1, ""); sprintf(str_elecdep2, ""); sprintf(str_elecdep3, ""); if (ElectronDepVar1) sprintf(str_elecdep1, "[1]"); if (DepVarDimension>1 && ElectronDepVar2) sprintf(str_elecdep2, "[1]"); if (DepVarDimension>2 && ElectronDepVar3) sprintf(str_elecdep2, "[1]"); // print draw string if (DepVarDimension==1) { sprintf(draw, "%s%s>>+%s", DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else if (DepVarDimension==2) { sprintf(draw, "%s%s:%s%s>>+%s", DepVarName2.c_str(), str_elecdep2, DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else if (DepVarDimension==3) { sprintf(draw, "%s%s:%s%s:%s%s>>+%s", DepVarName3.c_str(), str_elecdep3, DepVarName2.c_str(), str_elecdep2, DepVarName1.c_str(), str_elecdep1, HistNames.at(i).c_str()); } else { std::cout << " Error:: DepVarDimension can be 1 to 3, no more. Modify me if needed!" << std::endl; abort(); } // print sele string sprintf(sele, "(%s)&&(%s)", BaseSelection.c_str(), SelectionVecE2.at(i).c_str()); std::cout << " -- draw = " << draw << std::endl; std::cout << " -- sele = " << sele << std::endl; // draw tree->Draw(draw, sele); } } foutput->cd(); for (int i=0; i<Nhists; i++){ Hists.at(i)->Write(); } foutput->Close(); return 0; }
void Boid::seek(ofPoint target) { acc += steer(target, false); }
//Lennard-Jones based AI void Enemy::ljpTest() { b2Vec2 sum(0, 0); b2Vec2 flock(0, 0); b2Vec2 chase(0, 0); b2Vec2 fire(0, 0); //Chase and fire at player? Shape* player = getPlayer_(); if (player) { b2Vec2 myPos = getPosition(); b2Vec2 pPos = player->getPosition(); b2Vec2 between = (pPos - myPos); float dist = between.Length(); if (!chasing_ && dist < chaseRange_[3]) { chasing_ = true; } if (chasing_ && dist < chaseRangeMAX_[4]) { chase = LJP(pPos, 100.f, 200.f, 1.0, 1.8); } else chasing_ = false; //Fire if in visible range if (dist < visRange_[3]) { fire = LJP(pPos, 100, 0, 2.0, 0); } } if (chasing_) orient(chase); //Swarm to shapes int count = 0; for (Enemy* v : swarm_) { if (v != this) { b2Vec2 myPos = getPosition(); b2Vec2 theirPos = v->getPosition(); b2Vec2 between = theirPos - myPos; float minDist = v->getSize() + size_; float dist = between.Length(); if (between.Length() < flockRange_) { b2Vec2 steer(0, 0); if (!chasing_) steer = LJP(theirPos, 75.f, 300.f, 1.0f, 1.8f); else if (v->getVertices() == vertices_) steer = LJP(theirPos, 0, 100.f, 0, 1.8f); else steer = LJP(theirPos, 0, 100.f, 0, 1.8f); if (steer.x != 0 && steer.y != 0) { sum += steer; count++; } } } } //get average if (count > 0) { sum.x /= static_cast<float>(count); sum.y /= static_cast<float>(count); } else sum = b2Vec2_zero; //If we didn't want to fire float seePlayer = fire.Length(); if (seePlayer == 0) { angry_ -= angryMAX_ / triggerSatisfaction_[4]; } else { orient(fire); chill_ = 0; angry_ += seePlayer; } //If we're mad enough float ammo = (float)getWeaponBar() / (float)getWeaponBarMAX(); if (angry_ > angryMAX_ && seePlayer != 0) { if (fireAt(fire, 0.2f)) { angry_ -= triggerSatisfaction_[4]; } else release(); if (ammo< 0.1f) reup(); } angry_ = fmax(angry_, 0); //Chill out when idle if (angry_ == 0 && seePlayer == 0) { chill_ -= 1; if (chill_ <= chillMIN_) { if (ammo < 1.f) reup(); spin(-0.05f); } } chill_ = fmax(chill_, chillMIN_); move(chase + sum); }
void wParticle::applyGravityForce(float _pct) { acc += steer(*this,true)*_pct; // addAttractionForce(*this, 100, _pct); }
void Boid::arrive(ofPoint target) { acc += steer(target, true); }
/** Updates the player kart, called once each timestep. */ void NetworkPlayerController::update(float dt) { if (UserConfigParams::m_gamepad_debug) { // Print a dividing line so that it's easier to see which events // get received in which order in the one frame. Log::debug("PlayerController", "irr_driver", "-------------------------------------"); } // Don't do steering if it's replay. In position only replay it doesn't // matter, but if it's physics replay the gradual steering causes // incorrect results, since the stored values are already adjusted. if (!history->replayHistory()) steer(dt, m_steer_val); if (World::getWorld()->isStartPhase()) { if (m_controls->m_accel || m_controls->m_brake || m_controls->m_fire || m_controls->m_nitro) { // Only give penalty time in SET_PHASE. // Penalty time check makes sure it doesn't get rendered on every // update. if (m_penalty_time == 0.0 && World::getWorld()->getPhase() == WorldStatus::SET_PHASE) { RaceGUIBase* m=World::getWorld()->getRaceGUI(); if (m) { m->addMessage(_("Penalty time!!"), m_kart, 2.0f, video::SColor(255, 255, 128, 0)); m->addMessage(_("Don't accelerate before go"), m_kart, 2.0f, video::SColor(255, 210, 100, 50)); } m_penalty_time = stk_config->m_penalty_time; } // if penalty_time = 0 m_controls->m_brake = false; m_controls->m_accel = 0.0f; } // if key pressed return; } // if isStartPhase if (m_penalty_time>0.0) { m_penalty_time-=dt; return; } // We can't restrict rescue to fulfil isOnGround() (which would be more like // MK), since e.g. in the City track it is possible for the kart to end // up sitting on a brick wall, with all wheels in the air :(( // Only accept rescue if there is no kart animation is already playing // (e.g. if an explosion happens, wait till the explosion is over before // starting any other animation). if ( m_controls->m_rescue && !m_kart->getKartAnimation() ) { new RescueAnimation(m_kart); m_controls->m_rescue=false; } } // update