- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个创建循环的 2d 线渲染,我注意到的一个问题是,当以速度循环时它有时无法检测到,我想知道如何阻止这种情况发生,另一个问题是如何让它更宽容例如,如果这条线真的很接近前一条线,我希望它把它算作一个循环,以及我如何使这条线实际上成为鼠标指针而不是让它在后面,这可能是一个问题 future 我目前让它创建一个区域 2d 来检测它自身内部的物品/对象我想知道是否有更好的方法来准确地检测它们在所述循环中。
我上传了一个视频链接,可以直观地向您展示问题:https://www.youtube.com/watch?v=Jau7YDpZehY
extends Node2D
var points_array = PoolVector2Array()
#var check_array = []
var colision_array = []
var index : int = 0
onready var Line = $Line2D
onready var collision = $Area2D/CollisionPolygon2D
onready var collision_area = $Loop_collider
func _physics_process(delta):
Line.points = points_array # Link the array to the points and polygons
collision.polygon = points_array
if Input.is_action_just_pressed("left_click"): #This sets a position so that the next line can work together
points_array.append(get_global_mouse_position()) # This makes a empty vector and the mouse cords is assigned too it
#points_array.append(Vector2()) #This is the vector that follows the mouse
if Input.is_action_pressed("left_click"): #This checks the distance between last vector and the mouse vector
#points_array[-1] = get_global_mouse_position() # Gets the last position of the array and sets the mouse cords
var mouse_pos = get_global_mouse_position()
var distance = 20
while points_array[-1].distance_to(mouse_pos) > distance:
var last_point = points_array[-1]
var cords = last_point + last_point.direction_to(mouse_pos) * distance
points_array.append(cords)
create_collision()
if points_array.size() > 80: # This adds a length to the circle/line so it wont pass 18 mini lines
points_array.remove(0) #Removes the first array to make it look like it has a length
#check_array = []
colision_array[0].queue_free()
colision_array.remove(0)
if Input.is_action_just_released("left_click"): # This just clears the screen when the player releases the button
points_array = PoolVector2Array()
#check_array = []
for x in colision_array.size():
colision_array[0].queue_free()
colision_array.remove(0)
#index = 0
if points_array.size() > 3: # If the loop is long enough, to detect intersects
if points_array[0].distance_to(get_global_mouse_position()) < 5: # Checks if the end of the loop is near the end, then start new loop
new_loop()
for index in range(0, points_array.size() - 3):
if _segment_collision(
points_array[-1],
points_array[-2],
points_array[index],
points_array[index + 1]
):
new_loop()
break
#if check_array.size() != points_array.size():
# check_array = points_array
#create_collision()
func _segment_collision(a1:Vector2, a2:Vector2, b1:Vector2, b2:Vector2) -> bool:
# if both ends of segment b are to the same side of segment a, they do not intersect
if sign(_wedge_product(a2 - a1, b1 - a1)) == sign(_wedge_product(a2 - a1, b2 - a1)):
return false
# if both ends of segment a are to the same side of segment b, they do not intersect
if sign(_wedge_product(b2 - b1, a1 - b1)) == sign(_wedge_product(b2 - b1, a2 - b1)):
return false
# the segments must intersect
return true
func _wedge_product(a:Vector2, b:Vector2) -> float:
# this is the length of the cross product
# it has the same sign as the sin of the angle between the vectors
return a.x * b.y - a.y * b.x
func new_loop(): # Creates a new loop when holding left click and or loop is complete
var new_start = points_array[-1]
collision.polygon = points_array
points_array = PoolVector2Array()
collision.polygon = []
#check_array = []
points_array.append(new_start)
for x in colision_array.size():
colision_array[0].queue_free()
colision_array.remove(0)
func create_collision(): # Creates collisions to detect when something hits the line renderer
var new_colision = CollisionShape2D.new()
var c_shape = RectangleShape2D.new()
var mid_point = Vector2((points_array[-1].x + points_array[-2].x) / 2,(points_array[-1].y + points_array[-2].y) / 2)
c_shape.set_extents(Vector2(10,2))
new_colision.shape = c_shape
if points_array.size() > 1:
colision_array.append(new_colision)
collision_area.add_child(new_colision)
new_colision.position = mid_point
#new_colision.position = Vector2((points_array[-1].x),(points_array[-1].y))
new_colision.look_at(points_array[-2])
func _on_Area2D_area_entered(area): # Test dummies
print("detect enemy")
func _on_Loop_collider_area_entered(area):
print("square detected")
最佳答案
诊断
症状 1
I have noticed is that when looping at speed it sometimes doesn't detect
var mouse_pos = get_global_mouse_position()
var distance = 20
while points_array[-1].distance_to(mouse_pos) > _distance:
var last_point = points_array[-1]
var cords = last_point + last_point.direction_to(mouse_pos) * distance
points_array.append(cords)
create_collision()
for index in range(0, points_array.size() - 3):
if _segment_collision(
points_array[-1],
points_array[-2],
points_array[index],
points_array[index + 1]
):
new_loop()
break
*请记住[-1]
给出最后一项,[-2]
倒数第二。how to make it more forgiving for example if the line is really close to a previous line I would like it to count that as a loop
how would I make the line actually be the mouse pointer instead of having it ghost behind
CollisionShape2D
.
Node
├_line
├_segments
└_loops
这里
_line
将是
Line2D
,
_segments
将持有多个
Area2D
,每一个段。和
_loops
还将举办
Area2D
,但它们是跟踪的循环的多边形。
_ready
中完成:
var _line:Line2D
var _segments:Node2D
var _loops:Node2D
func _ready() -> void:
_line = Line2D.new()
_line.name = "_line"
add_child(_line)
_segments = Node2D.new()
_segments.name = "_segments"
add_child(_segments)
_loops = Node2D.new()
_loops.name = "_loops"
add_child(_loops)
_physics_process
看起来像这样:
func _physics_process(_delta:float) -> void:
if Input.is_action_pressed("left_click"):
position(get_global_mouse_position())
# TODO
我们还需要在点击释放时进行处理。让我们为此创建一个函数,稍后再考虑:
func _physics_process(_delta:float) -> void:
if Input.is_action_pressed("left_click"):
position(get_global_mouse_position())
if Input.is_action_just_released("left_click"):
total_purge()
上
position
我们将遵循移动最后一个点以匹配最近位置的奇怪技巧。我们需要确保至少有两点。所以第一个点不会移动,我们可以安全地移动最后一个点。
var _points:PoolVector2Array = PoolVector2Array()
var _max_distance = 20
func position(pos:Vector2) -> void:
var point_count = _points.size()
if point_count == 0:
_points.append(pos)
_points.append(pos)
elif point_count == 1:
_points.append(pos)
else:
if _points[-2].distance_to(pos) > _max_distance:
_points.append(pos)
else:
_points[-1] = pos
请注意,我们检查到倒数第二个点的距离。我们无法检查最后一点,因为那是我们要移动的一点。
_max_dinstance
然后我们添加一个新点,否则我们移动最后一个点。
var _points:PoolVector2Array = PoolVector2Array()
var _max_distance = 20
func position(pos:Vector2) -> void:
var point_count = _points.size()
if point_count == 0:
_points.append(pos)
_points.append(pos)
add_segment(pos, pos)
elif point_count == 1:
_points.append(pos)
add_segment(_points[-2], pos)
else:
if _points[-2].distance_to(pos) > _max_distance:
_points.append(pos)
add_segment(_points[-2], pos)
else:
_points[-1] = pos
change_segment(_points[-2], pos)
你知道,我们稍后会担心它是如何工作的。
var _points:PoolVector2Array = PoolVector2Array()
var _max_points = 30
var _max_distance = 20
func position(pos:Vector2) -> void:
var point_count = _points.size()
if point_count == 0:
_points.append(pos)
_points.append(pos)
add_segment(pos, pos)
elif point_count == 1:
_points.append(pos)
add_segment(_points[-2], pos)
elif point_count > _max_points:
purge(point_count - _max_points)
else:
if _points[-2].distance_to(pos) > _max_distance:
_points.append(pos)
add_segment(_points[-2], pos)
else:
_points[-1] = pos
change_segment(_points[-2], pos)
我们需要更新
Line2D
,我们需要处理任何循环:
var _points:PoolVector2Array = PoolVector2Array()
var _max_points = 30
var _max_distance = 20
func position(pos:Vector2) -> void:
var point_count = _points.size()
if point_count == 0:
_points.append(pos)
_points.append(pos)
add_segment(pos, pos)
elif point_count == 1:
_points.append(pos)
add_segment(_points[-2], pos)
elif point_count > _max_points:
purge(point_count - _max_points)
else:
if _points[-2].distance_to(pos) > _max_distance:
_points.append(pos)
add_segment(_points[-2], pos)
else:
_points[-1] = pos
change_segment(_points[-2], pos)
_line.points = _points
process_loop()
好的,让我们谈谈添加和更新段:
var _width = 5
func add_segment(start:Vector2, end:Vector2) -> void:
var points = rotated_rectangle_points(start, end, _width)
var segment = Area2D.new()
var collision = create_collision_polygon(points)
segment.add_child(collision)
_segments.add_child(segment)
func change_segment(start:Vector2, end:Vector2) -> void:
var points = rotated_rectangle_points(start, end, _width)
var segment = (_segments.get_child(_segments.get_child_count() - 1) as Area2D)
var collision = (segment.get_child(0) as CollisionPolygon2D)
collision.set_polygon(points)
这里
_width
是我们想要的碰撞多边形的宽度。
Area2D
带有一个碰撞多边形(通过我们稍后会担心的函数创建),或者我们取最后一个
Area2D
并以相同的方式更新其碰撞多边形。
static func rotated_rectangle_points(start:Vector2, end:Vector2, width:float) -> Array:
var diff = end - start
var normal = diff.rotated(TAU/4).normalized()
var offset = normal * width * 0.5
return [start + offset, start - offset, end - offset, end + offset]
所以你把从线段开始到结束的向量旋转四分之一圈(也就是 90º)。这为您提供了一个与线段垂直(垂直)的向量,我们将使用该向量为其指定宽度。
static func create_collision_polygon(points:Array) -> CollisionPolygon2D:
var result = CollisionPolygon2D.new()
result.set_polygon(points)
return result
func total_purge():
purge(_points.size())
purge_loops()
那很简单。
func purge(index:int) -> void:
var segments = _segments.get_children()
for _index in range(0, index):
_points.remove(0)
if segments.size() > 0:
_segments.remove_child(segments[0])
segments[0].queue_free()
segments.remove(0)
_line.points = _points
检查
if segments.size() > 0
顺便说一下,这是必要的。有时清除会留下没有段的点,这会在以后导致问题。这是更简单的解决方案。
Line2D
.
func purge_loops() -> void:
for loop in _loops.get_children():
if is_instance_valid(loop):
loop.queue_free()
func process_loop() -> void:
var segments = _segments.get_children()
for index in range(segments.size() - 1, 0, -1):
var segment = segments[index]
var candidates = segment.get_overlapping_areas()
for candidate in candidates:
var candidate_index = segments.find(candidate)
if candidate_index == -1:
continue
if abs(candidate_index - index) > 1:
push_loop(candidate_index, index)
purge(index)
return
所以,当一个循环发生时,我们想用它做点什么,对吧?就是这样
push_loop
是为了。我们还想删除属于循环(或循环之前)的点和线段,因此我们调用
purge
.
push_loop
剩下来讨论:
func push_loop(first_index:int, second_index:int) -> void:
purge_loops()
var loop = Area2D.new()
var points = _points
points.resize(second_index)
for point_index in first_index + 1:
points.remove(0)
var collision = create_collision_polygon(points)
loop.add_child(collision)
_loops.add_child(loop)
如您所见,它创建了一个
Area2D
,具有与循环相对应的碰撞多边形。我决定使用
rezise
删除循环之后的点,并使用 for 循环删除之前的点。所以只剩下循环的点。
purge_loops
一开始,这确保一次只有一个循环。
关于godot - 线条渲染有时无法检测相交以及如何在循环时使其更宽容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67633200/
我在 Godot 中制作一个自上而下的游戏,当我将 Camera 2D 节点添加到我的播放器并将其设置为当前时,播放器卡在同一个地方并且无法再移动。我收到此错误消息 modules/gdscript/
我正在构建一个命令解析器,我已经成功地设法将字符串拆分成单独的单词并使其全部正常工作,但我有点难过的是如何从字符串中删除所有标点符号。用户将输入诸如 , 之类的字符。 ! ?通常,但对于那些字符,它无
当我在 godot 中制作预加载脚本并在其中放置一个变量时,如何在我的其他脚本中调用该变量? 有没有一种特殊的方法来调用我不知道的变量,或者有比预加载脚本更好的方法吗? 最佳答案 如果您正在预加载脚本
我正在尝试弄清楚如何在其视口(viewport)(本例中为根视口(viewport))内“偏移”相机以创建偏心相机。为了说明,使用标准 Camera (3D)导致屏幕中心平行线的消失点: 我想要实现的
我是 godot 引擎的新手,我正在尝试制作手机游戏(仅限肖像模式)。我想让背景图像适合屏幕大小。我怎么做?我是否必须导入具有特定尺寸的图像并将它们全部用于各种屏幕?如果我将图像导入大,它只会剪掉不适
我在 Godot 中有一个项目可以在敌人上方渲染广告牌四边形。四边形(网格实例)是敌人节点的子节点。我只想将四边形渲染到视口(viewport)以进行后处理,但四边形需要在屏幕上具有与敌人相同的位置(
我正在使用 GDscript 在 Godot 中编写一个程序,旨在改变骨架中多个骨骼的位置。骨骼通过我希望骨骼移动到的点减去骨骼的当前位置计算得出的值进行转换。 translateValue = en
想一想您可能需要显示按钮列表的角色扮演游戏。用户可能会进入一个房间,他们必须从一系列选项(按钮)中进行选择。是否有一种容器/面板可以水平显示可点击按钮,但在需要时换行? 我能想到的最好的比喻是,想象一
我正在制作 Godot 的 Hello World 场景。 我没有像教程中演示的那样使用“Hello World”,而是尝试了“ Hello World ”之类的东西,但我发现它不起作用。我在标签文本
我想知道如何创建一个脚本,其中包含我想在多个脚本中使用的方法。我不认为我想为它创建一个全局单例,因为我没有存储任何将在多个场景中保存的全局数据。我收集了一些有用的功能,别无他物。 最佳答案 创建自己的
我对我在新 2D 项目中遇到的问题有疑问。我添加了一个 Sprite 背景,当我运行测试时它显示没有问题。我在输入映射中将 esc 键设置为“key_exit”。然后我创建了一个 node2D 作为
我昨天刚刚开始使用 Godot,我正在开始一个游戏。我为它画了一些 Sprite 表。将动画的所有帧打包到一个图像文件中似乎效率更高,对吗? 无论如何,在 Godot 中,我有一个 AnimatedS
请提供 Godot 中跟随鼠标的 Sprite 的最小代码示例。有复杂的和大的示例项目,但我没有发现任何小而清晰的东西。 最佳答案 将 Sprite 节点放入场景中,并附加以下脚本。 const SP
抱歉,问题标题太长了。我是新来的。 所以我的问题就像上面的标题一样。我几乎尝试了所有方法,并在互联网上到处搜索。我尝试过的一些伪线索是 .linear_interpolate("my screen w
我正在尝试确定哪个 Control节点刚刚改变了焦点。通过将节点上的焦点模式更改为 All,已设置另一个 Control 节点以使用焦点。在该节点的脚本中,我可以通过检查 has_focus 来判断它
我无法将 SliceAndCoagulatorScene 链接到脚本以将其可见设置为 false 我已尝试 get_node、$res://... extends Node2D onready var
我有一个创建循环的 2d 线渲染,我注意到的一个问题是,当以速度循环时它有时无法检测到,我想知道如何阻止这种情况发生,另一个问题是如何让它更宽容例如,如果这条线真的很接近前一条线,我希望它把它算作一个
大量的谷歌搜索,我仍然没有掌握可能是一个简单的解决方案。 场景:“主要”。包含一个 TileMap“Grid”,并附有一个脚本“Grid.gd”。 场景:“玩家”。包含一个带有脚本的 Kinemati
我有以下场景:播放器敌人攻击 当攻击与敌人发生碰撞时,敌人会发出“onHit”信号。 播放器监听该信号并反弹回来。 这一切都很好,但是现在如果我复制敌人所以有多个敌人场景,我如何听所有这些场景的信号?
我正在使用 gdscipt,并且正在尝试设置状态机。我正在尝试访问PlayerStateMachine/PlayerStateMachineManager.gd 通过使用 onready var st
我是一名优秀的程序员,十分优秀!