想法:当您放大时,它应该朝鼠标方向缩放。在某些方面,确实如此,但它会卡住,有时会朝错误的方向放大。
我尝试放大和缩小(如链接视频中所示)。例如,放大视口的一角,然后缩小,然后将鼠标移动到另一侧(在视频中,是右侧),它会放大到左侧。
源代码:
extends Node2D
var camera : Camera2D
var zoom_min = null
var zoom_max = Vector2(3, 3)
var zoom_speed = Vector2(0.2, 0.2)
var des_zoom = null
# Called when the node enters the scene tree for the first time.
func _ready():
# Init the Map from the MapData
var Map = await load(MapData.MapFile).instantiate()
Map.name = "Map"
print_rich("[color=green]Map Loaded: [/color]", Map.name, "\n")
# Add the Map node to the scene tree
add_child(Map)
### Map.position = Vector2(-MapData.map_middlepoint[0], -MapData.map_middlepoint[1])
# Create and configure the Camera2D node
camera = Camera2D.new()
# Calculate the required zoom level based on the map's height and the viewport height
var viewport_size = get_viewport_rect().size
var map_height = MapData.map_middlepoint[1] * 2
var zoom_level = viewport_size.y / map_height
# Apply the calculated zoom level to the camera
camera.zoom = Vector2(zoom_level, zoom_level)
zoom_min = Vector2(zoom_level, zoom_level)
des_zoom = Vector2(zoom_level, zoom_level)
# Calculate the aspect ratio of the viewport
var viewport_aspect_ratio = viewport_size.x / viewport_size.y
# Calculate the extra space needed on the left and right sides
var extra_space = (MapData.map_middlepoint[1] * viewport_aspect_ratio)
print_rich("[color=yellow]Extra Space:[/color] ", extra_space)
# Setting camera position to the middle of the map
camera.position = MapData.map_middlepoint # camera starting position
# Setting limits for the camera with extra space on the left and right sides
camera.limit_left = -extra_space + MapData.map_middlepoint[0]
camera.limit_top = 0
camera.limit_right = extra_space + MapData.map_middlepoint[0]
camera.limit_bottom = MapData.map_middlepoint[1] * 2
# Add the Camera2D node to the scene tree
add_child(camera)
camera.make_current() # Make this camera the current camera
# Print the scene tree
print_tree_pretty()
func _process(delta):
camera.zoom = lerp(camera.zoom, des_zoom, 0.1)
# Get the mouse position
### var camera_position = camera.get_global_position()
### print_rich("[color=yellow]Camera Position:[/color] ", camera_position, "\n")
func _input(event):
if event is InputEventMouseButton:
if Input.is_mouse_button_pressed(MOUSE_BUTTON_WHEEL_DOWN):
if des_zoom > zoom_min:
des_zoom -= zoom_speed
var mouse_position = get_global_mouse_position()
print_rich("[color=yellow]Mouse Position (Zoom Out):[/color] ", mouse_position)
var target_position = lerp(camera.position, mouse_position, 0.2)
target_position.x = clamp(target_position.x, camera.limit_left, camera.limit_right)
target_position.y = clamp(target_position.y, camera.limit_top, camera.limit_bottom)
print_rich("[color=yellow]Target Position (Zoom Out):[/color] ", target_position)
camera.position = target_position
if Input.is_mouse_button_pressed(MOUSE_BUTTON_WHEEL_UP):
if des_zoom < zoom_max:
des_zoom += zoom_speed
var mouse_position = get_global_mouse_position()
print_rich("[color=yellow]Mouse Position (Zoom In):[/color] ", mouse_position)
var target_position = lerp(camera.position, mouse_position, 0.2)
target_position.x = clamp(target_position.x, camera.limit_left, camera.limit_right)
target_position.y = clamp(target_position.y, camera.limit_top, camera.limit_bottom)
print_rich("[color=yellow]Target Position (Zoom In):[/color] ", target_position)
camera.position = target_position
问题主要源于从
target_position
计算camera.position
。您假设 camera.position
将在相机的限制范围内(这是一个非常合理的假设),但现实是 camera.position
可以是任何东西。
您在视频中看到的内容的说明:您首先通过在该位置放大和缩小来将
camera.position
移动到左上角。然后,当您放大右上角时,target_position
首先设置为左上角和右上角之间的 20%,然后在下一次输入鼠标滚轮时,再靠近 20%,依此类推。
以下是官方引擎文档对此的说法:
请注意,Camera2D 节点的位置并不代表屏幕的实际位置,屏幕的实际位置可能会因应用的平滑或限制而有所不同。您可以使用 get_screen_center_position 来获取真实位置。
尝试使用
get_screen_center_position
就像文档提到的那样。换句话说,将 var target_position = lerp(camera.position, mouse_position, 0.2)
替换为 var target_position = lerp(camera.get_screen_center_position(), mouse_position, 0.2)
。