光线投射精灵在屏幕周围传送

问题描述 投票:0回答:1

我正在为我的编程课制作一个光线投射器(比如《毁灭战士》/《德军总部》),我已经成功地让主光线投射器工作了,但是当尝试实现该实体时精灵,它们在屏幕上传送,而不是显示应该在的位置。

(“m。”是“数学。”,因为我使用了“将数学导入为 m”)

问题的

.gif:https://drive.google.com/file/d/1pZvUxjHU7V4LD6hGaFsUSGZOlxLXsNXu/view

def drawEntity(entity):
    
    dir = plr.rotateAngle # camera directoin
    x = (entity.centerX - plr.centerX) # entity's relative position to player
    y = (entity.centerY - plr.centerY)
    
    # not used # # dis = m.sqrt((plr.centerX - entity.centerX)**2 + (plr.centerY - entity.centerY)**2)
    
    vx = x * m.cos(dir) - y * m.sin(dir) # is supposed to rotate the location of the sprite
    vy = x * m.sin(dir) + y * m.cos(dir) # but doesnt seem to work
    
    if vy > 0: 
        
        sprite = Image(entities.animations[m.floor(entity.animation)], 200, 200)
        
        height = 30 * app.maxDistance / vy # where app.maxDistance = 200 / m.tan(45)
        
        sprite.width, sprite.height = abs(320 * height * 0.01), abs(320 * height * 0.01) # 320 is the image's width / height
        sprite.centerX, sprite.centerY = vx * (app.maxDistance / vy)+200, 200 # x-pos is messed up some how
        
        sprites.add(sprite) # draws sprite

程序显示使用x/y坐标0-400

我期望当相机旋转时精灵能够在 3D 空间中显示正确的位置 相反,当相机旋转时,精灵会传送

我认为这可能与“vx”和“vy”变量有关

python raycasting
1个回答
0
投票

问题是在 cos() sin() 和 atan() 函数中使用度数而不是弧度。感谢 Michael Butscher 帮助我,我花了 5 个小时试图找出问题所在。 (在此之前我不知道弧度是什么)

通过将所有度数乘以 (m.pi/180) 解决了这个问题

© www.soinside.com 2019 - 2024. All rights reserved.