官术网_书友最值得收藏!

Consider using distance-squared over distance

It is safe to say that CPUs are relatively good at multiplying floating-point numbers together, but relatively dreadful at calculating square-roots from them. Every time we ask a Vector3 to calculate a distance with the magnitude property or with the Distance() method, we're asking it to perform a square-root calculation (as per Pythagorean theorem), which can cost a lot of CPU overhead compared to many other types of vector math calculations.

However, the Vector3 class also offers a sqrMagnitude property, which provides the same result as distance, only the value is squared. This means that if we also square the value we wish to compare distance against, then we can perform essentially the same comparison without the cost of an expensive square-root calculation.

For example, consider the following code:

float distance = (transform.position – other.transform.position).Distance();
if (distance < targetDistance) {
// do stuff
}

This can be replaced with the following and achieve a nearly identical result:

float distanceSqrd = (transform.position – other.transform.position).sqrMagnitude;
if (distanceSqrd < (targetDistance * targetDistance)) {
// do stuff
}

The reason the result is nearly identical is because of the floating-point precision. We're likely to lose some of the precision that we would have had from using the square-root values, since the value will be adjusted to an area with a different density of representable numbers; it can land exactly on, or closer to, a more accurate representable number, or, more likely, it will land on a number with less accuracy. As a result, the comparison is not exactly the same, but, in most cases, it is close enough to be unnoticeable, and the performance gain can be quite significant for each instruction we replace in this manner.

If this minor precision loss is not important, then this performance trick should be considered. However, if precision is very important (such as running an accurate large-scale galactic space simulation), then you may want to give this tip a pass.

Note that this technique can be used for any square-root calculations, not just for distance. This is simply the most common example you might run across, and it brings to light the important sqrMagnitude property of the Vector3 class. This is a property which Unity Technologies intentionally exposed for us to make use of in this manner.

主站蜘蛛池模板: 抚州市| 鞍山市| 阿克陶县| 广昌县| 南昌县| 兴国县| 卢氏县| 徐水县| 昌都县| 新化县| 调兵山市| 车险| 慈利县| 沧源| 淳安县| 青铜峡市| 桑植县| 安宁市| 伊吾县| 临猗县| 广昌县| 将乐县| 南江县| 黄大仙区| 嫩江县| 临沭县| 新蔡县| 乳源| 疏勒县| 南川市| 山东省| 虹口区| 周至县| 花垣县| 静海县| 留坝县| 吉水县| 鲜城| 扶沟县| 屯门区| 汝阳县|