I tried to make an infinite grid in godot and I don't understand why this isn't working.
When the camera moves, the grid moves in the other direction (which is normal) but doesn't redraw lines to the correct position.
我试着在戈多制作一个无限大的网格,我不明白为什么这样做行不通。当摄影机移动时,栅格向另一个方向移动(这是正常的),但不会将线重新绘制到正确的位置。
this is my code:
这是我的代码:
extends Node2D
const cell_size = 64
func _draw():
var viewport_size = get_viewport_rect().size
var camera_position: Vector2
if has_node("Camera2D"): camera_position = get_viewport().get_camera_2d().position
else: camera_position = viewport_size / 2
var vertical = Vector2i(camera_position.x - viewport_size.x / 2, camera_position.x + viewport_size.x / 2)
var horizontal = Vector2i(camera_position.y - viewport_size.y / 2, camera_position.y + viewport_size.y / 2)
for x in range(vertical.x - cell_size, vertical.y + cell_size, cell_size):
draw_line(Vector2(x, horizontal.x), Vector2(x, horizontal.y), Color.WHITE)
for y in range(horizontal.x - cell_size, horizontal.y + cell_size, cell_size):
draw_line(Vector2(vertical.x, y), Vector2(vertical.y, y), Color.WHITE)
func _process(_delta):
queue_redraw()
and I just want for my grid to be infinite.
我只希望我的网格是无限的。
更多回答
Can't write an answer now, but: you are mixing coordinate systems. The Camera2D
position is on the local coordinates of its parent, the draw instructions work in local coordinates of your Node2D
, plus the Viewport
coordinates are also different. I suggest you use the global_position
of the Camera and use to_local
to get it on Node2D
local coordinates, I don't know of the top of my head what to do with the Viewport
coordinates, but they would be an issue if the Node2D
has rotation or scaling. This might help: docs.godotengine.org/en/stable/tutorials/2d/2d_transforms.html
现在不能写出答案,但是:你在混合坐标系。Camera2D位置位于其父对象的局部坐标上,绘制指令在Node2D的局部坐标下工作,此外,视区坐标也不同。我建议您使用摄影机的GLOBAL_POSITION并使用TO_LOCAL在Node2D局部坐标上获取它,我不知道如何处理Viewport坐标,但如果Node2D具有旋转或缩放,它们将是一个问题。这可能会有帮助:docs.godotengine.org/en/stable/tutorials/2d/2d_transforms.html
I'll try, thanks.
我会尽力的,谢谢。
优秀答案推荐
我是一名优秀的程序员,十分优秀!