2

这是我的旧算法

def spawn(self):
    self.position_move() #to adjust when the player is spawned
def position_move(self):
    #gets called everytime the player moves
    camera.x = -object.x+Window.size[0]/2.0
    camera.y = -object.y+Window.size[1]/2.0

它运作良好,但当我接近一个角落时,我可以看到房间的“外面”。 在此处输入图像描述

我更新算法的全部目的是防止看到房间的“外面”:

    def spawn(self): #when the player is first spawned
        self.position_move() #to adjust when the player is spawned

    def position_move(self):
            #camera.x = -(top left x corner of camera coordinate)
            #camera.y = -(top left y corner of camera coordinate)
             #room_size= total room (or world) size

        diff=(object.x-Window.size[0]/2)
        if diff<0:
            camera.x = object.x+diff
        else:
            diff=(object.x+Window.size[0]/2)-room_size[0]
            if diff<0:
                camera.x = -object.x+Window.size[0]/2.0
        diff=(object.y-Window.size[1]/2)

        if diff<0:
            camera.y = diff+Window.size[1]/2
        else:
            diff=room_size[1]-(object.y+Window.size[1]/2)

            if diff>0:
                camera.y = -object.y+Window.size[1]/2.0

我对更新算法的想法是将相机转换回房间,它在房间外有多少。尽管出于某种原因我更新的算法有时会变得不稳定(飞走,或者甚至不跟随玩家)。

所以,是的,对于我认为是一项简单的任务来说,这有点复杂。有谁知道我的算法中有错误?

(我使用的框架有这样的坐标系) 在此处输入图像描述

4

1 回答 1

2

不确定您的代码中的错误是什么(我还没有真正看过),但这是将相机绑定到世界的一种简单方法:

# This assumes coordinates refer to the top-left of the desired viewport

# Since this is not the case, first invert the object's y to make it so:
object_y = room_size[1] - object.y

camera.x = max(0, object.x - window.size[0] / 2)
camera.y = max(0, object_y - window.size[1] / 2)

if camera.x + window.size[0] > room_size[0]:
    camera.x = room_size[0]  - window.size[0]
if camera.y + window.size[1] > room_size[1]:
    camera.y = room_size[1]  - window.size[1]

# Again, since you're using bottom-left coordinate system, invert the y:
camera.y = room_size[1] - camera.y
于 2012-11-23T21:50:56.650 回答