static Vector3 CalcNewVelocity(float minSpeed, Vector3 curVel, Vector3 dsrVel, Vector3 defaultVelocity) { //We have to take into account that bird can't change their direction instantly. That's why //dsrVel (desired velocity) influence first of all on flying direction and after that on //velocity magnitude oneself var curVelLen = curVel.magnitude; if (curVelLen > MathTools.epsilon) { curVel /= curVelLen; } else { curVel = defaultVelocity; curVelLen = 1; } var dsrVelLen = dsrVel.magnitude; var resultLen = minSpeed; if (dsrVelLen > MathTools.epsilon) { dsrVel /= dsrVelLen; //We spend a part of velocity magnitude on bird rotation and the rest of it on speed magnitude changing //Map rotation to factor [0..1] var angleFactor = MathTools.AngleToFactor(dsrVel, curVel); //If dsrVel magnitude is twice bigger than curVelLen then bird can rotate on any angle var rotReqLength = 2 * curVelLen * angleFactor; //Velocity magnitude remained after rotation var speedRest = dsrVelLen - rotReqLength; if (speedRest > 0) { curVel = dsrVel; resultLen = speedRest; } else { curVel = Vector3.Slerp(curVel, dsrVel, dsrVelLen / rotReqLength); } if (resultLen < minSpeed) { resultLen = minSpeed; } } return(curVel * resultLen); }
//Force which attracts birds to waypoints static Vector3 CalculateAttractionForce(Settings sts, Vector3 curPos, Vector3 curVelocity) { if (!sts.Trace) { return(Vector3.zero); } var attrPos = sts.Trace.GetAtractionPoint(); var direction = (attrPos - curPos).normalized; //The force have an effect only on direction and shouldn't increase speed if bird flies in the right direction var factor = sts.AttractrionForce * sts.SpeedMultipliyer * MathTools.AngleToFactor(direction, curVelocity); return(factor * direction); }
public bool Calc(Vector3 cur, Vector3 birdDir, Collider cld, out Force force) { var pointOnBounds = MathTools.CalcPointOnBounds(cld, cur); var revDir = cur - pointOnBounds; var dist = revDir.magnitude; if (dist <= MathTools.epsilon) { //Let's setup the direction to outside of colider revDir = (pointOnBounds - cld.transform.position).normalized; //and distance to N percent of OptDistance dist = 0.1f * optDistance; } else { revDir /= dist; } //Force depends on direction of bird: no need to turn a bird if it is flying in opposite direction force.dir = revDir * (CalcImpl(dist) * MathTools.AngleToFactor(revDir, birdDir)); force.pos = pointOnBounds; return(true); }