Sin和Cos函数在python中不正确 [英] Sin and Cos function incorrect in python

查看:197
本文介绍了Sin和Cos函数在python中不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在pygame中制作自上而下的射击游戏.我在添加代码时使播放器朝着他们所面对的方向移动.玩家会往奇怪的方向移动.这是我正在使用的代码:

I am trying to make a top down shooter in pygame. I when I added the code to make the player move in the direction they are facing. The player would move in weird directions. This is the code I am using:

        if pygame.key.get_pressed()[K_UP]:
            playerX = playerX - math.cos(playerFacing)
            playerY = playerY - math.sin(playerFacing)
        if pygame.key.get_pressed()[K_DOWN]:
            playerX = playerX + math.cos(playerFacing)
            playerY = playerY + math.sin(playerFacing)

我尝试仅输入math.cos(90)等于-0.299515394756,但是我的计算器告诉我它等于0.我可能只是在犯一个愚蠢的错误,但是谁能告诉我我在做什么错.谢谢Xeno

I tried just typing in math.cos(90) and it equaled -0.299515394756 but my calculator is telling me it equals 0. I am probably just making a stupid mistake, but can anyone tell me what I am doing wrong. Thanks Xeno

推荐答案

math.sin math.cos 等.在您可以使用 math.radians 将度数转换为弧度.所以:

You can use math.radians to convert degrees to radians. So:

math.sin(math.radians(90)) == 0

您可以像这样在发布的代码段中对其进行修复:

And you could just fix it in the posted snippet like that:

if pygame.key.get_pressed()[K_UP]:
            playerX = playerX - math.cos(math.radians(playerFacing))
            playerY = playerY - math.sin(math.radians(playerFacing))
        if pygame.key.get_pressed()[K_DOWN]:
            playerX = playerX + math.cos(math.radians(playerFacing))
            playerY = playerY + math.sin(math.radians(playerFacing))

但是我建议到处都使用弧度.

However I advise to just use radians everywhere.

这篇关于Sin和Cos函数在python中不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆