void Entity::impact(const vec3d& inMomentum) { const vec3d& vec = getMomentum(); mVelocity[0] = (vec[0] + inMomentum[0]) / getMass(); mVelocity[1] = (vec[1] + inMomentum[1]) / getMass(); mVelocity[2] = (vec[2] + inMomentum[2]) / getMass(); }
bool Character::CharacterBody::interact(AstroBody* body, double deltaTime, vector2p& interactPoint, vector2p& impulse) { // FIXME: Way too much happening here. Move some of the code elsewhere. phys_t t1, t2; vector2p posCharacter = getPosition(), posBody = body->getPosition(); vector2p legA = posCharacter - posBody; phys_t hVel = (getVelocity() - body->getVelocityAt(legA)) / legA.unit(); phys_t angle = legA.angle() - PI / 2 - getOrientation(); phys_t accel = clampmag(hVel / 8.0 - parent_->vel_, 1.0); angle = clampmag(remainder(angle, PI * 2) + accel * PI / 8, PI / 2); walkCycle_ = remainder(walkCycle_ - deltaTime * hVel * 5.0, PI * 2); phys_t leg = 0.25 + 0.15 * (1.0 - parent_->powerJump_); vector2p feetOrigin = vector2p::fromAngle(angle - PI / 2) * (leg - 0.05); vector2p feetOffset = vector2p::fromAngle(walkCycle_) * 0.15; parent_->legBack_.setPosition(feetOrigin + feetOffset); parent_->legFront_.setPosition(feetOrigin - feetOffset); angle += PI / 2 + getOrientation(); vector2p legB = legA - vector2p()(cos(angle), sin(angle)); phys_t radius = ((Circle<phys_t>*) (body->getShape()))->getRadius(); phys_t radiusSqr = radius * radius; if (intersectLineCircle<phys_t> (legA, legB, radiusSqr, t1, t2) && t1 > 0 && t1 < leg) { vector2p interactBody = legA * (1 - t1) + legB * t1; interactPoint = interactBody + posBody; vector2p interactCharacter = interactPoint - posCharacter; vector2p n = interactCharacter / t1; impulse = -n * (8000.0 * (leg - t1) + max(0.0, getMomentum() * n * 50.0)) * deltaTime; phys_t da = remainder(getOrientation() - atan2(n.x, -n.y), 2 * PI); phys_t dav = getAngularVelocity() + getVelocity() / legA / legA.squared(); vector2p na = legA.unit(); phys_t in = na * impulse; phys_t balancing = (da * 500 + dav * 200) * deltaTime; phys_t ip = clampmag(~na * impulse + balancing, 0.8 * in); impulse = na * in + ~na * ip; return true; } return false; }
string Solver::convet2CaffeFormat() { string outStr = ""; string netStrStart = "net: \""; string netStrEnd = "\"\n"; string trainNetStrStart = "train_net: \""; string trainNetStrEnd = "\"\n"; string testNetStrStart = "test_net: \""; string testNetStrEnd = "\"\n"; string testIterStrStart = "test_iter: "; string testIterStrEnd = "\n"; string testIntervalStrStart = "test_interval: "; string testIntervalStrEnd = "\n"; string baseLrStrStart = "base_lr: "; string baseLrStrEnd = "\n"; string momentumStrStart = "momentum: "; string momentumStrEnd = "\n"; string weightDecayStrStart = "weight_decay: "; string weightDecayStrEnd = "\n"; string lrPolicyStrStart = "lr_policy: \""; string lrPolicyStrEnd = "\"\n"; string stepSizeStrStart = "stepsize: "; string stepSizeStrEnd = "\n"; string gammaStrStart = "gamma: "; string gammaStrEnd = "\n"; string powerStrStart = "power: "; string powerStrEnd = "\n"; string stepValueStrStart = "stepvalue: "; string stepValueStrEnd = "\n"; string displayStrStart = "display: "; string displayStrEnd = "\n"; string maxIterStrStart = "max_iter: "; string maxIterStrEnd = "\n"; string snapshotStrStart = "snapshot: "; string snapshotStrEnd = "\n"; string snapshotPrefixStrStart = "snapshot_prefix: \""; string snapshotPrefixStrEnd = "\"\n"; string typeStrStart = "type: \""; string typeStrEnd = "\"\n"; string solveModeStrStart = "solver_mode: "; string solveModeStrEnd= "\n"; if(getNet() != "") { outStr += netStrStart + getNet() + netStrEnd; } if(getTainNet() != "") { outStr += trainNetStrStart + getTainNet() + trainNetStrEnd; } if(getTestNet() != "") { outStr += testNetStrStart + getTestNet() + testNetStrEnd; } outStr += testIterStrStart + to_string(getTestIter()) + testIterStrEnd + testIntervalStrStart + to_string(getTestInterval()) + testIntervalStrEnd + baseLrStrStart + to_string(getBaseLr()) + baseLrStrEnd + momentumStrStart + to_string(getMomentum()) + momentumStrEnd + weightDecayStrStart + to_string(getWeightDecay()) + weightDecayStrEnd; switch(getLrPolicy()) { case LrPolicy::LRPOLICY_FIXED: { outStr += lrPolicyStrStart + "fixed" + lrPolicyStrEnd; break; } case LrPolicy::LRPOLICY_STEP: { outStr += lrPolicyStrStart + "step" + lrPolicyStrEnd + stepSizeStrStart + to_string(getStepSize()) + stepSizeStrEnd + gammaStrStart + to_string(getGamma()) + gammaStrEnd; break; } case LrPolicy::LRPOLICY_EXP: { outStr += lrPolicyStrStart + "exp" + lrPolicyStrEnd + gammaStrStart + to_string(getGamma()) + gammaStrEnd; break; } case LrPolicy::LRPOLICY_INV: { outStr += lrPolicyStrStart + "inv" + lrPolicyStrEnd + gammaStrStart + to_string(getGamma()) + gammaStrEnd + powerStrStart + to_string(getPower()) + powerStrEnd; break; } case LrPolicy::LRPOLICY_MULTISTEP: { outStr += lrPolicyStrStart + "multistep" + lrPolicyStrEnd; for(int i = 0 ; i < mParam->mStepValue.size(); i++) { outStr += stepValueStrStart + to_string(getStepValue(i)) + stepValueStrEnd; } outStr += gammaStrStart + to_string(getGamma()) + gammaStrEnd; break; } case LrPolicy::LRPOLICY_POLY: { outStr += lrPolicyStrStart + "poly" + lrPolicyStrEnd + powerStrStart + to_string(getPower()) + powerStrEnd; break; } case LrPolicy::LRPOLICY_SIGMOID: { outStr += lrPolicyStrStart + "sigmoid" + lrPolicyStrEnd + stepSizeStrStart + to_string(getStepSize()) + stepSizeStrEnd + gammaStrStart + to_string(getGamma()) + gammaStrEnd; break; } } outStr += displayStrStart + to_string(getDisplay()) + displayStrEnd + maxIterStrStart + to_string(getMaxIter()) + maxIterStrEnd + snapshotStrStart + to_string(getSnapshot()) + snapshotStrEnd; if(getSnapshotPrefix() != "") { outStr += snapshotPrefixStrStart + getSnapshotPrefix() + snapshotPrefixStrEnd; } switch(getType()) { case SolverType::SGD: { outStr += typeStrStart + "SGD" + typeStrEnd; break; } case SolverType::NESTEROV: { outStr += typeStrStart + "Nesterov" + typeStrEnd; break; } case SolverType::ADAGRAD: { outStr += typeStrStart + "AdaGrad" + typeStrEnd; break; } case SolverType::RMSPROP: { outStr += typeStrStart + "RMSProp" + typeStrEnd; break; } case SolverType::ADADELTA: { outStr += typeStrStart + "AdaDelta" + typeStrEnd; break; } case SolverType::ADAM: { outStr += typeStrStart + "Adam" + typeStrEnd; break; } } switch(getSolverMode()) { case SolverMode::CPU: { outStr += solveModeStrStart + "CPU" + solveModeStrEnd; break; } case SolverMode::GPU: { outStr += solveModeStrStart + "GPU" + solveModeStrEnd; break; } } return outStr; }