这是我以前的算法
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我对更新算法的想法是,根据摄像头在房间外的程度,将摄像头转换回房间。虽然我的更新算法由于某些原因有时会变得不稳定(飞走了,或者甚至不跟随玩家)。
所以,是的,对于我认为是一项简单的任务来说,这有点复杂。有人知道错误出在我的算法里吗?
(我使用的框架有一个这样的坐标系)

发布于 2012-11-24 05:50:57
不确定你的代码中的错误是什么(我还没有真正看过),但这里有一个简单的方法来将相机绑定到世界上:
# 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.yhttps://stackoverflow.com/questions/13535865
复制相似问题