int MomentumConn::updateWeights(int arborId){ if(timeBatchIdx != timeBatchPeriod - 1){ return PV_SUCCESS; } //Add momentum right before updateWeights for(int kArbor = 0; kArbor < this->numberOfAxonalArborLists(); kArbor++){ applyMomentum(arborId); } //Saved to prevweights assert(prev_dwDataStart); std::memcpy(*prev_dwDataStart, *get_dwDataStart(), sizeof(pvwdata_t) * numberOfAxonalArborLists() * nxp * nyp * nfp * getNumDataPatches()); // add dw to w for(int kArbor = 0; kArbor < this->numberOfAxonalArborLists(); kArbor++){ pvwdata_t * w_data_start = get_wDataStart(kArbor); for( long int k=0; k<patchStartIndex(getNumDataPatches()); k++ ) { w_data_start[k] += get_dwDataStart(kArbor)[k]; } } return PV_BREAK; }
void Player::update(ALLEGRO_EVENT ev, InputManagement input) { // Update the current player based on input, momentum, whether // jumping is currently happening, and whether gravity is // currently pulling the player down if(otherTick) otherTick = false; else otherTick = true; input.update(); applyMomentum(); movePlayer(ev); jumpTick(); gravityTick(); // Update values accordingly playerDimensions.updateValues(); // Set the currentFrame to the direction and animate playerAnimation.modifiableCurrentFrame().second = playerDimensions.getCurrentDirection(); ssAnimation.update(playerAnimation); }
void shipSimulate(struct GameObject *go, double dt) { if (go->type == SHIP) { struct Ship *ship = (struct Ship *)(go->objData); struct ParticleSystem *eps=(struct ParticleSystem *)(ship->engines->objData); struct ParticleSystem *rps=(struct ParticleSystem *)(ship->rengines->objData); struct Pair targVel; double dotProduct, worstDotProduct, bestDotProduct, fthrust, rthrust; bestDotProduct = (ship->warpLimit)*(ship->warpLimit); worstDotProduct = -bestDotProduct; targVel.x = (ship->warpLimit)*cos(CONVERT_TO_RAD*go->angle); targVel.y = (ship->warpLimit)*sin(CONVERT_TO_RAD*go->angle); dotProduct = targVel.x*go->vel.x + targVel.y*go->vel.y; rthrust = (dotProduct-worstDotProduct)/(bestDotProduct-worstDotProduct); fthrust = 1-rthrust; if (ship->thrusters) { eps->overallFade = 1.0; eps->intensity=0.9*pow( fthrust, 0.5 ); go->force.x+=fthrust*(ship->speed)*cos(CONVERT_TO_RAD*go->angle)*go->mass; go->force.y+=fthrust*(ship->speed)*sin(CONVERT_TO_RAD*go->angle)*go->mass; } else { eps->overallFade=0.75; } if (ship->retro) { rps->overallFade = 1.0; rps->intensity=0.9*pow( rthrust, 0.5 ); go->force.x-=rthrust*(ship->speed)*cos(CONVERT_TO_RAD*go->angle)*go->mass; go->force.y-=rthrust*(ship->speed)*sin(CONVERT_TO_RAD*go->angle)*go->mass; } else { rps->overallFade=0.75; } double targetAngVel = 0; ship->currentTorque = 0; if (!ship->tempInvince) { if (ship->turnLeft) targetAngVel += ship->turnSpeed; if (ship->turnRight) targetAngVel += -ship->turnSpeed; ship->currentTorque = (targetAngVel - go->angVel) * go->moi/dt; go->torque += ship->currentTorque; } if (ship->firingBullet) { if (ship->heat == 0) { struct GameObject *bo = initBullet(); struct Bullet *bullet = (struct Bullet *)bo->objData; double firingXVel = (bullet->speed)*cos(CONVERT_TO_RAD*go->angle); double firingYVel = (bullet->speed)*sin(CONVERT_TO_RAD*go->angle); bo->vel.x = go->vel.x + firingXVel; bo->vel.y = go->vel.y + firingYVel; bo->pos.x = go->pos.x + SHIP_SIZE*cos(CONVERT_TO_RAD*go->angle); bo->pos.y = go->pos.y + SHIP_SIZE*sin(CONVERT_TO_RAD*go->angle); go->force.x += (go->vel.x-bo->vel.x)*bo->mass/dt; go->force.y += (go->vel.y-bo->vel.y)*bo->mass/dt; ship->heat+=5; hashAdd(gameObjects,(void*)bo); } } double vel = sqrt((go->vel.x)*(go->vel.x)+(go->vel.y)*(go->vel.y)); if (vel > ship->warpLimit) { go->vel.x*=ship->warpLimit/vel; go->vel.y*=ship->warpLimit/vel; } ship->engines->angle = go->angle; ship->rengines->angle = 180+go->angle; ship->engines->pos = go->pos; ship->engines->pos.x-=cos(CONVERT_TO_RAD*go->angle)*SHIP_SIZE/2; ship->engines->pos.y-=sin(CONVERT_TO_RAD*go->angle)*SHIP_SIZE/2; ship->rengines->pos = go->pos; ship->rengines->pos.x+=cos(CONVERT_TO_RAD*go->angle)*SHIP_SIZE; ship->rengines->pos.y+=sin(CONVERT_TO_RAD*go->angle)*SHIP_SIZE; ship->engines->vel = go->vel; ship->rengines->vel = go->vel; ship->tractorBeam->pos = go->pos; if (ship->engageTractor) { if (!ship->tractor) { struct GameObject *other, *bestOther; bestOther = NULL; struct HashElement *cursor = NULL; int i; struct Pair vect; double dist, bestDist; bestDist = RAND_MAX; for (i=0; i<gameObjects->numBuckets; i++) { cursor = gameObjects->buckets[i].head; while(cursor!=NULL) { other = ((GP)(cursor->obj)); if ((other->type != BULLET) && other->phased<=0) { vect.x = other->pos.x - go->pos.x; vect.y = other->pos.y - go->pos.y; dist = getLength(vect); if (dist<bestDist && dist > 2*SHIP_SIZE) { bestDist = dist; bestOther = other; } } cursor = cursor->next; } } ship->tractorLength = bestDist; ship->tractoredObject = bestOther; ship->tractor = bestOther!=NULL; } } struct ParticleSystem *tps=(struct ParticleSystem *)(ship->tractorBeam->objData); if (ship->releaseTractor) { ship->tractor = 0; ship->tractoredObject = NULL; tps->overallFade = 0.75; } if (ship->tractoredObject!=NULL) { struct Pair vect, nvect, fvect; double dist; vect.x = ship->tractoredObject->pos.x-go->pos.x; vect.y = ship->tractoredObject->pos.y-go->pos.y; dist = getLength(vect); if (dist<1.0) { ship->tractorBeam->angle = 180+atan2(vect.y,vect.x)/(CONVERT_TO_RAD); tps->overallFade = 1.0; tps->intensity = 0.5; tps->speed = 4*dist; nvect.x = vect.x/dist; nvect.y = vect.y/dist; fvect.x = 10*(ship->tractorLength-dist)*nvect.x; fvect.y = 10*(ship->tractorLength-dist)*nvect.y; ship->tractoredObject->force.x+=fvect.x; ship->tractoredObject->force.y+=fvect.y; go->force.x-=fvect.x; go->force.y-=fvect.y; } else { ship->tractoredObject = NULL; tps->overallFade = 0.75; } } else { tps->overallFade = 0.75; } applyMomentum(go,dt); if (toroidalWrap(go,aspectRatio*2,2)) { ship->tractoredObject = NULL; } applyCooldown(&(ship->heat)); applyCooldown(&(ship->tempInvince)); } }