Limiting Trig Function Return Values Python

Viewed 53

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?

0 Answers
Related