限制TRIG函数返回值Python
我正在Python制作游戏,其中涉及很多触发。我正在尝试优化它以尽可能减少滞后,我认为在触发功能中具有较低的精度将有助于。例如,Math.sin(PI/3)返回类似于0.866025403784444444464676372317075294。对此进行更多操作意味着计算机必须执行很多不必要的数字处理,而且我实际上只需要2或3个精度的小数点(例如0.866)。对于上下文,这里是使用TRIG的代码:
xRel = x-camX
yRel = y-camY
zRel = z-camZ
yp = yRel * math.cos(math.radians(pitch))
zp = yRel * math.sin(math.radians(pitch))
hy = math.sqrt(xRel*xRel+zRel*zRel) * mult(zRel)
if zRel == 0:
thetaYaw = 1.5708
else:
thetaYaw = math.atan(xRel/zRel) + math.radians(yaw)
a = hy * math.cos(thetaYaw)
thetaPitch = math.radians(pitch) + 1.5708
xRel = round(hy * math.sin(thetaYaw), 3)
yRel = round(a * math.cos(thetaPitch) + yp, 3)
zRel = round(a * math.sin(thetaPitch) + zp, 3)
在每个程序周期中为每个实体执行这些计算。
计算比率后,我尝试使用圆()方法,但这只会使滞后更糟,这可能是因为代码必须运行的额外步骤。有没有办法/另一个数学库,我可以告诉触发函数在一定数量的十进制位置后停止计算?
I'm making a game in python that involves quite a bit of trig. I'm trying to optimize it to reduce lag as much as possible, and I think that it would help to have lower precision in the trig functions. For example, math.sin(pi/3) returns something like 0.86602540378443864676372317075294. Doing more operations on this means that the computer has to do a lot of unnecessary number crunching, and I really only need 2 or 3 decimal places of precision (like 0.866). For context, here is the code that uses trig:
xRel = x-camX
yRel = y-camY
zRel = z-camZ
yp = yRel * math.cos(math.radians(pitch))
zp = yRel * math.sin(math.radians(pitch))
hy = math.sqrt(xRel*xRel+zRel*zRel) * mult(zRel)
if zRel == 0:
thetaYaw = 1.5708
else:
thetaYaw = math.atan(xRel/zRel) + math.radians(yaw)
a = hy * math.cos(thetaYaw)
thetaPitch = math.radians(pitch) + 1.5708
xRel = round(hy * math.sin(thetaYaw), 3)
yRel = round(a * math.cos(thetaPitch) + yp, 3)
zRel = round(a * math.sin(thetaPitch) + zp, 3)
These calculations are performed for each entity at every program cycle.
I've tried using the round() method after calculating the ratio, but this only makes the lag worse, probably because of the extra steps the code has to run. Is there a way/another math library where I can tell a trig function to stop calculating after a certain number of decimal places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论