void Player::getInput() { if(!gameWindow->GetInput().IsKeyDown( sf::Key::Left)) { friction(); } if(gameWindow->GetInput().IsKeyDown( sf::Key::Left)) { moveLeft(); } if(gameWindow->GetInput().IsKeyDown( sf::Key::Right)) { moveRight(); } if(!gameWindow->GetInput().IsKeyDown( sf::Key::Right)) { friction(); } if(gameWindow->GetInput().IsKeyDown(sf::Key::Space)) { if(playerHealth>5) { jump(); collideFloor=false; } } if(gameWindow->GetInput().IsKeyDown( sf::Key::Down)) { } }
void RagdollSetupAnimatedFriction( IPhysicsEnvironment *pPhysEnv, ragdoll_t *ragdoll, int iModelIndex ) { vcollide_t* pCollide = modelinfo->GetVCollide( iModelIndex ); if ( pCollide ) { IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( pCollide->pKeyValues ); while ( !pParse->Finished() ) { const char *pBlock = pParse->GetCurrentBlockName(); if ( !strcmpi( pBlock, "animatedfriction") ) { CRagdollAnimatedFriction friction( ragdoll ); pParse->ParseCustom( (void*)&friction, &friction ); } else { pParse->SkipBlock(); } } physcollision->VPhysicsKeyParserDestroy( pParse ); } }
void GraphComparator::_calculateRasterCost(cv::Mat& mat) { ProbablePathCalculator ppc; ppc.setRandomNoise(0.0); ppc.setRandomPatches(0.0, 1); vector<float> friction(_width * _height, 5.0 * _pixelSize); Tgs::Image<float> cost(_width, _height); for (int y = 0; y < _height; y++) { float* row = mat.ptr<float>(y); for (int x = 0; x < _width; x++) { cost.pixel(x, y) = row[x]; } } ppc.setFriction(_height, _width, friction); cost = ppc.updateCostSurface(cost); for (int y = 0; y < _height; y++) { float* row = mat.ptr<float>(y); for (int x = 0; x < _width; x++) { row[x] = cost.pixel(x, y); } } }
world::world(): _world(NULL), _accum(0), _freq(1.0 / 60.0) { // default to global gravity gravity.enabled = true; _world = NewtonCreate(); const float WORLD_SIZE = 1000; const vec3 A(-WORLD_SIZE, -WORLD_SIZE, -WORLD_SIZE), B(-A); NewtonSetWorldSize(_world, &A.x, &B.x); platform(PLAT_OPTMIZED); solver(3); friction(FRIC_ADAPTIVE); threads(2); clearCache(); int mid = NewtonMaterialGetDefaultGroupID(_world); // allow bodies to be swept-tested NewtonMaterialSetContinuousCollisionMode(_world, mid, mid, 1); NewtonMaterialSetCollisionCallback(_world, mid, mid, NULL, &beginContactCB, &processContactCB); }
//-------------------------------------------------------------- void testApp::updateMovers(){ //ofVec2f wind(ofMap(mouseX, ofGetWidth()/2, ofGetWidth(), 0, 0.01), 0); ofVec2f windCartesian(-sin(ofDegToRad(wind.y)), cos(ofDegToRad(wind.y))); ofVec2f windForce(wind_speed * wind_speed * windCartesian/coefPixelToRealWorld); //ofVec2f gravity(0, 0.1); //generate balls here if (wind_speed > 2 && (ofRandomuf() < 0.00001 * wind_speed * cloudProbability || wind_speed - last_wind_speed > 4)){ //if (wind_speed > 2 && (ofRandomuf() < 0.00001 * 60 * cloudProbability || wind_speed - last_wind_speed > 4)){ movers.push_back(ofPtr<Mover>(new Mover())); movers.back().get()->setup(); //movers.back().get()->setMass(ofRandom(1.1, 4)); movers.back().get()->setMass(1); ofVec2f location; location.y = ofGetHeight()/2.; float width = 2/3.*ofGetHeight(); if (windCartesian.x > 0){ //location.x = 0-movers.back().get()->getDiameter(); location.x = -width; } else { //location.x = ofGetWidth(); location.x = ofGetWidth()+width; } //coefPixelToRealWorld = 1; movers.back().get()->setLocation(location.x, location.y); windCartesian.y = 0; windCartesian.normalize(); movers.back().get()->setVelocity(windCartesian*wind_speed/coefPixelToRealWorld); } last_wind_speed = wind_speed; // update balls for (unsigned int i = 0; i < movers.size(); i++){ //friction float c = frictionCoef; ofVec2f friction(movers[i].get()->getVelocity()); friction *= -1; friction.normalize(); friction *= c; movers[i].get()->applyForce(friction); //movers[i].applyForce(windForce); //movers[i].applyForce(gravity); movers[i].get()->update(); movers[i].get()->checkEdges(); } // delete balls ofRemove(movers, Mover::shouldRemoveOffScreen); }
static void RagdollCreateObjects( IPhysicsEnvironment *pPhysEnv, ragdoll_t &ragdoll, const ragdollparams_t ¶ms ) { ragdoll.listCount = 0; ragdoll.pGroup = NULL; ragdoll.allowStretch = params.allowStretch; memset( ragdoll.list, 0, sizeof(ragdoll.list) ); memset( &ragdoll.animfriction, 0, sizeof(ragdoll.animfriction) ); if ( !params.pCollide || params.pCollide->solidCount > RAGDOLL_MAX_ELEMENTS ) return; constraint_groupparams_t group; group.Defaults(); ragdoll.pGroup = pPhysEnv->CreateConstraintGroup( group ); IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( params.pCollide->pKeyValues ); while ( !pParse->Finished() ) { const char *pBlock = pParse->GetCurrentBlockName(); if ( !strcmpi( pBlock, "solid" ) ) { solid_t solid; pParse->ParseSolid( &solid, &g_SolidSetup ); RagdollAddSolid( pPhysEnv, ragdoll, params, solid ); } else if ( !strcmpi( pBlock, "ragdollconstraint" ) ) { constraint_ragdollparams_t constraint; pParse->ParseRagdollConstraint( &constraint, NULL ); RagdollAddConstraint( pPhysEnv, ragdoll, params, constraint ); } else if ( !strcmpi( pBlock, "collisionrules" ) ) { IPhysicsCollisionSet *pSet = physics->FindOrCreateCollisionSet( params.modelIndex, ragdoll.listCount ); CRagdollCollisionRules rules(pSet); pParse->ParseCustom( (void *)&rules, &rules ); } else if ( !strcmpi( pBlock, "animatedfriction") ) { CRagdollAnimatedFriction friction( &ragdoll ); pParse->ParseCustom( (void*)&friction, &friction ); } else { pParse->SkipBlock(); } } physcollision->VPhysicsKeyParserDestroy( pParse ); }
sf::Vector2f QuantumField::get_accel(const PointMass& caller, const sf::Vector2f& desiredAcceleration) const { sf::Vector2f base_accel{desiredAcceleration}; if (&caller == player_object) { base_accel += user_input; } base_accel += friction(caller); return base_accel; }
bool PlaneObstacle::bounce(Eigen::Vector3d& x, Eigen::Vector3d& v) const { double d = x.dot(mNormal); if (d > mOffset) return false; // Project the position onto the surface of the obstacle. x += (mOffset - d) * mNormal; // Split the velocity into normal and tangential components. double normMag = v.dot(mNormal); Eigen::Vector3d normVel = normMag * mNormal; Eigen::Vector3d tanVel = v - normVel; v = (1.0 - friction()) * tanVel; return true; }
void entity_friction(entity_t* e, int amount) { e->xx = friction(e->xx, amount); e->yy = friction(e->yy, amount); }
void Player::update(float dt) { if (state_ == PlayerState::Launching) { launch_charge_ += launch_charge_speed_ * dt; launch_charge_ = clamp(launch_charge_,0.0f,1.0f); } aimer_->hold( clamp(aimer_->sequence_frame("charge") + int(aimer_->sequence_duration("charge") * launch_charge_), 0, aimer_->sequence_duration("charge") - 1)); sf::Vector2f friction(0.0f,0.0f); if (!state_machine_.state_in_the_air(state_)) { friction.x = -signum(velocity_.x); friction *= friction_constant_; } else { friction.x = -signum(velocity_.x); friction *= air_friction_constant_; } sf::Vector2f gravity_effect = current_gravity_; if (state_ == PlayerState::Winning) { gravity_effect = sf::Vector2f(0.0f,0.0f); } velocity_ += dt * (acceleration_ + gravity_effect); float velocity_after_friction = velocity_.x + dt * friction.x; // apply friction if (signum(velocity_.x) != signum(velocity_after_friction)) { float dv = velocity_after_friction - velocity_.x; float ratio = (0 - velocity_.x) / dv; move(sf::Vector2f(dt * (velocity_.x + ratio * dv),0.0f)); velocity_.x = 0.0f; } else { velocity_.x = velocity_after_friction; } if (state_ == PlayerState::Winning) { if (dt > 0.0f) { sf::Vector2f dampening = velocity_ * dt; velocity_ -= dampening; } } move(dt * (velocity_ + movement_velocity_)); rotate_aim(dt * current_aim_speed_); if (state_ == PlayerState::Flying) { if (velocity_.y >= 0.0f) { switch_to_state(PlayerState::Falling); } } if (state_ == PlayerState::Landing) { if (!animation().playing()) { switch_to_state(PlayerState::Idle); } } animation().update(dt); aimer_->update(dt); }
double TYPE::bottom( double Us, // scalar velocity double H, // flow depth double ka, // von Karman's constant ( = 0.41 ) double vk, // kinematic viscosity double g, // gravity acceleration ( = 9.81 ) double rho, // density of water double rhob, // density of sediment double d50, // 50% diameter of grain double d90 ) // 90% diameter of grain { // ------------------------------------------------------------------------------------ // compute form roughness, test for presence of vegetation double kd = 0.0; double kr = 0.0; if( form > 0 && dp[0] < 1.0e-4 && dp[1] < 1.0e-4 ) { // compute height and length of ripples and dunes ----------------------------------- Dune( Us, H, ka, vk, g, rho, rhob, d50, d90, &_Hd, &_Ld, &_Hr, &_Lr ); if( _Ld > 0.01 ) { kd = duneCoef * _Hd * ( 1.0 - exp(-25.0*_Hd/_Ld) ); // if( _Lr > 0.01 ) kr = 20.0 * 0.7 * _Hr * _Hr / _Lr; if( _Lr > 0.01 ) kr = duneCoef * _Hr * ( 1.0 - exp(-25.0*_Hr/_Lr) ); } else if( _Lr > 0.01 ) { kr = duneCoef * _Hr * ( 1.0 - exp(-25.0*_Hr/_Lr) ); } } // ------------------------------------------------------------------------------------ // compute friction coefficient for bottom roughness double cb = 0.0; // Colebrook-Whites or Nikuradses law chosen for grain roughness ... if( rtype < kCHEZ && kslaw > 0 ) { cb = friction( rtype, ksfact*d90 + kd + kr, Us, H, ka, vk, g ); } // ... Chezys or Mannings law chosen for grain roughness else { cb = friction( rtype, rcoef, Us, H, ka, vk, g ); cb += friction( kNIKU, kd + kr, Us, H, ka, vk, g ); } _cb = cb; // remind cb in static class variable _cb // ------------------------------------------------------------------------------------ // compute friction coefficient for non-submerged vegetation double cp = lindner( dp[0], sp[0], Us, H, cb, vk, g ); if( cp < -0.9 ) // means: cp == -1.0 { // approximately set cwr = 1.5 cp = cb + 0.75 * H * dp[0] / sp[0] / sp[0]; } _cp = cp; // remind cp in static class variable _cp // ------------------------------------------------------------------------------------ // compute friction coefficient for submerged vegetation double cv = velzen( hp, dp[1], sp[1], Us, H, cb, ka, vk, g ); // ------------------------------------------------------------------------------------ // return the assembled friction coefficient for non-submerged and submerged vegetation return cp + cv; }
//================================================================================== // main function (main fucntion of the program) //=================================================================================== void main(void) { //assign variable unsigned char i=0,database; unsigned char data[10]; unsigned int m,j; init(); d_graphic(0); //display cytron logo delay(300000); //delay for pic display // display claibrating messages send_config(0b00110000); //set to function set configuration lcd_clr(); //clear the lcd send_string(cali); //display string lcd_goto (8); //cursor at 3rd character line send_string(mssg1); //display string mssg1 lcd_goto(24); //cursor at 4th character line send_string(mssg2); //display string mssg2 while(SW1); //wait for button pushed while(!SW1); //wait for button release calibrate(); //taking the current force as reference for flat plane lcd_clr(); //clear the lcd lcd_goto(1); //cursor at 3rd character, 1st character line send_string(mssg3); //display string mssg3 lcd_goto(17); //cursor at 3rd character, 2st character line send_string(mssg4); //display string mssg4 lcd_goto(0); //cursor at 1st character, 1st character line send_char(0x10); //display symbol arrow m =0 ; //set the variable 0 while(1) //loop forever { if (!SW1) //if sw1 is pressed, increase mode { while(!SW1); //wait until sw1 is release m++; //increament m if (m>1) m=0; //if m more than 1 , clear m if (m==1) //if m is 1 { lcd_goto(0); //cursor at 1st line send_char(' '); //space 1st and 2nd character , 1st line lcd_goto(16); //cursor at 2nd line send_char(0x10); //display arrow at 2nd charater, 2nd line } else { lcd_goto(0); //cursor at 1st line send_char(0x10); //display arrow at 2nd charater, 1st line lcd_goto(16); //cursor at 2nd line send_char(' '); //space 1st and 2nd character , 2nd line } } else if (!SW2) //if sw is pressed { while(!SW2); //wait until sw2 is released lcd_clr(); //clear all character clr_graphic(); //clear all graphic // 16 bit timer initial status and start timer TMR0H = 194; // value for timer reach overflow in 50 ms for clock 10MIPS TMR0L = 246; T0CONbits.TMR0ON = 1; //start timer while(1) //loop forever { if (INTCONbits.TMR0IF==1) //if timer over flow ( occur every 50 ms) // to update data every 50 ms { INTCONbits.TMR0IF==0; //clear the flag bit TMR0H=194; //value for timer reach overflow in 50 ms for clock 10MIPS TMR0L=246; if (m==1) friction(); //if mode is 1 , the do calculation friction, else do calculation freespace. else freespace(); //update the data ( required for more accurate intergration ( faster, more accurate )) i++; //increment variable i } if (i==10) //if i reach ten ( around 500 ms) update the icon position { if((x_dis/sens >60)||(x_dis/sens<-60)||(y_dis/sens>28)||(y_dis/sens<-28)) //if the icon position not in range of glcd { //mean 'the ball drop out from glcd' d_graphic(1); //display graphic delay(300000); //dealy for display the graphic d_graphic(2); //display another graphic delay(300000); while(1); } d_icon(60-(x_dis/sens),28+(y_dis/sens)); //display icon at updated x,y position i=0; //clear the variable i } } } } }
void Player::update(float deltaTime) { checkMapStatus(); vec3f initPos = pos; //Animation Conditions bool collidingSides = false; bool collidingFloor = false; bool running = false; //PHYSICS // apply forces vec3f dir(0); if(input.getKeyState(InputHandler::PLAYER_LEFT)) { dir += vec3f(-1, 0, 0); running = true; } if(input.getKeyState(InputHandler::PLAYER_RIGHT)) { dir += vec3f(1, 0, 0); running = true; } vec3f friction(0); if(!(fabs(velocity.x) < 0.08f)) friction = FRICTION_COEFF*vec3f(velocity.x > 0 ? -1.0 : 1.0, 0, 0); totalForce += ACCELERATION*dir + vec3f(0, -GRAVITY, 0) + friction; bool saltito = false; // apply impulses if (animState != Player::JUMP && input.getKeyDown(InputHandler::PLAYER_UP)) { velocity.y += JUMP_IMPULSE; saltito = true; } // integration velocity = glm::clamp(velocity + totalForce*deltaTime, vec3f(-MAX_VELOCITY), vec3f(MAX_VELOCITY)); if (fabs(velocity.x) < 0.08f) velocity.x = 0; //Reset totalForce; totalForce = vec3f(0.0f); vec3f disp = velocity*deltaTime; //NOT PHYSICS // collision detection Map* map = (Map*)getGame()->getObjectByName("map"); AABB aabb = modelAabb; mat4f trans = fullTransform*modelOffset; aabb = AABB(vec3f(trans*vec4f(aabb.getMin(), 1.0f)), vec3f(trans*vec4f(aabb.getMax(), 1.0f))); vec3f bbmin = vec3f(trans*vec4f(modelAabb.getMin(), 1.0f)); vec3f bbmax = vec3f(trans*vec4f(modelAabb.getMin() + modelAabb.getDimensions()*vec3f(0.2f, 0.05f, 1.0f), 1.0f)); AABB brushBox(bbmin + disp, bbmax + disp); colliding = false; bool isBrushColliding = false; //Y AABB newboxY(aabb.getMin()+vec3f(0,disp.y,0), aabb.getMax()+vec3f(0,disp.y,0)); Color blockColor; if(map->isColliding(newboxY, blockColor)) { float min = 0; float max = 1; Color foo; isBrushColliding = map->isColliding(brushBox, foo); while(max-min > 0.001) { //search for the maximum distance you can move float m = (max+min)/2; newboxY = AABB(aabb.getMin()+vec3f(0,disp.y*m,0), aabb.getMax()+vec3f(0,disp.y*m,0)); if(map->isColliding(newboxY, foo)) max = m; else min = m; } if(velocity.y < 0) collidingFloor = true; velocity.y = 0; disp.y *= min; colliding = true; isBrushColliding = true; } pos.y += disp.y; aabb = AABB(aabb.getMin()+vec3f(0,disp.y,0), aabb.getMax()+vec3f(0,disp.y,0)); //X bool isRightWall = false; AABB newboxX(aabb.getMin()+vec3f(disp.x,0,0), aabb.getMax()+vec3f(disp.x,0,0)); if(map->isColliding(newboxX, blockColor)) { Color foo; isBrushColliding = isBrushColliding || map->isColliding(brushBox, foo); float min = 0; float max = 1; while(max-min > 0.001) { //search for the maximum distance you can move float m = (max+min)/2; newboxX = AABB(aabb.getMin()+vec3f(disp.x*m,0,0), aabb.getMax()+vec3f(disp.x*m,0,0)); if(map->isColliding(newboxX, foo)) max = m; else min = m; } //WALL JUMP vec3f wallFriction(0); if(velocity.x != 0) { wallFriction = FRICTION_COEFF*vec3f(0, velocity.y > 0 ? -.1 : .4, 0); collidingSides = true; } totalForce += wallFriction; if(velocity.x > 0 ) { if (input.getKeyDown(InputHandler::PLAYER_UP)) { velocity.x -= JUMP_IMPULSE*5; emitter->boomSide(20, -1); saltito = false; } isRightWall = true; } else if(velocity.x < 0 ) { if (input.getKeyDown(InputHandler::PLAYER_UP)) { velocity.x += JUMP_IMPULSE*5; emitter->boomSide(20, 1); saltito = false; } } else velocity.x = 0; disp.x *= min; colliding = true; } if(saltito) emitter->boom(20); pos.x += disp.x; //ANIMATION if(collidingSides) { animState = Player::WALL; anim = velocity.y > 0 ? "wallu" : "walld"; } else if(collidingFloor) { if(running) { animState = Player::RUN; anim = fabs(velocity.x) >= MAX_VELOCITY/2 ? "runb" : "runa"; } else { animState = Player::IDLE; anim = "idle"; } } else { animState = Player::JUMP; anim = fabs(velocity.x) > MAX_VELOCITY/2 ? "jumpb" : "jumpa"; } animCount += deltaTime; if(animCount >= animTime) { animCount -= animTime; animTime = randomFloat(0.1f, 0.2f); animIter = 1 - animIter; } std::string s = "brush" + anim + toString(animIter); model.mesh = Meshes.get(s); // TRAILS if(collidingFloor && !prevOnfloor) emitter->boom(20); if (collidingFloor && isBrushColliding && (blockColor == Color::WHITE || blockColor == color) ) { Trails* trails = (Trails*)getGame()->getObjectByName("trails"); trails->addTrailSegment(color, Trails::HORIZONTAL, pos.x, initPos.x, int(pos.y - 0.1), 1.5f*modelAabb.getDimensions().x); } if (collidingSides && isBrushColliding && (blockColor == Color::WHITE || blockColor == color) ) { Trails* trails = (Trails*)getGame()->getObjectByName("trails"); Trails::Direction dir; if (isRightWall) dir = Trails::VERTICAL_RIGHT; else dir = Trails::VERTICAL_LEFT; float off = 0.21; trails->addTrailSegment(color, dir, pos.y-off, initPos.y-off, int(pos.x+(isRightWall?1:0)), 1.5f*modelAabb.getDimensions().x); } prevOnfloor = collidingFloor; prevOnside = collidingSides; //transform stuff for(int i = 0; i < 3; ++i) { if(rot[i] < 0) rot[i] = rot[i]+360; else if(rot[i] >= 360.0f) rot[i] = rot[i]-360; } transform = glm::translate(mat4f(1), pos); transform = glm::scale(transform, scale); if(pos.y < -2) die(); }