GODOT 中的 move_and_slide 和 move_and_colide 函数有问题
I am having issues with the move_and_slide and move_and_colide functions in GODOT
我试图创建一个基本的进化模拟器,为此我要求我随机创建的生物向最近的食物移动。我对获取生物方向的代码充满信心,因为我已经通过按预期数量修改位置来测试它(是的,我知道我不应该直接改变位置)。然后,当我使用 move_and_slide
或 move_and_collide
时,我发现它们并没有按预期移动,而是跳到一条线上或沿着 window 的中心聚集,并且似乎随机地上下移动.我不确定是什么导致了这种移动,因为我过去对 move_and_slide
或 move_and_collide
的所有使用都运行良好。
func move(food_positions, delta):
if playing:
direction = choose_direction(food_positions)
if direction != null:
move_and_slide(direction*SPEED)
当生物在 'simulation' 场景中实例化时,此代码被外部调用。 food_positions
是用于计算所需方向的 Vector2 数组。因为它是从另一个场景调用的,所以我需要在使用 move_and_collide
.
时传递增量值
choose_direction
的代码如下:
var closest_food_pos = null
var closest_food_distance = null
# iterate through each food
for food_pos in food_positions:
# get distance betweeen the two positions
var gap = food_pos - position
var distance_food_me = sqrt(pow(gap.x,2) + pow(gap.y,2))
# insert test to within view distance here
# confiming that we are not comparing to null
if closest_food_distance != null and closest_food_pos != null:
# comparing distances
if closest_food_distance > distance_food_me:
# setting to closest food
closest_food_distance = distance_food_me
closest_food_pos = food_pos
else:
#if null need to set to first food to compare
closest_food_distance = distance_food_me
closest_food_pos = food_pos
if closest_food_distance != null and closest_food_pos != null:
var gap = closest_food_pos - position
var go_direction = gap.normalized()
return go_direction.normalized()
else:
# add ai go towards function call here
return null
还没有完全完成,我打算在以后添加一些内容。
请注意,这是生物场景的一部分,在模拟场景中动态实例化,该场景是 pre-instanced/part 的主要场景,用于控制和以后的可用性。
git 中心存储库是:https://github.com/benpotter480/Evolution_simulation_that_doesnt_work
抱歉,如果我的存储库的某些部分没有正常显示,因为我是新手。
感谢任何建议。
好的,我有办法
前言
我首先在move_and_slide()
和运行项目中添加了一个断点,从主场景单步执行代码。没有什么看起来不对劲。正在设置和初始化这些值。我添加了 print 语句来找出 food_positions
、creature_positions
和 direction
随着时间的推移显示的内容,认为第一帧不是罪魁祸首。但是,什么都看不到。
我检查了生物场景。那里一定有东西。代码看起来不错,没有任何东西会导致 move_and_slide()
执行它正在做的事情。我运行 孤立的生物场景。你猜怎么着,它奏效了。所以,至少我知道它不在生物场景中。
这很麻烦。据我所知,它在模拟中无缘无故地失败了。我尝试生成 100 种食物,100 种生物。
然后是 1 个生物。
那个生物没有动。那是什么。当有更多的生物时,它们就像是被迫进入彼此。经过更多的摆弄之后,我无意识地转动 wall_left
静态物体并注意到那个小的 protube运行ce 然后,等等,那不是 RectangleShape2D
。
我删除了所有的墙...
成功了!模拟成功了!
TL;DR/解决方案
墙壁的 LineShape2D
碰撞形状的法线方向错误。将 LineShape2D
的 Normal
从 (0, -1)
更改为 (0, 1)
。
结论
真是让人头疼。希望这对您有所帮助,祝您编码愉快!
我试图创建一个基本的进化模拟器,为此我要求我随机创建的生物向最近的食物移动。我对获取生物方向的代码充满信心,因为我已经通过按预期数量修改位置来测试它(是的,我知道我不应该直接改变位置)。然后,当我使用 move_and_slide
或 move_and_collide
时,我发现它们并没有按预期移动,而是跳到一条线上或沿着 window 的中心聚集,并且似乎随机地上下移动.我不确定是什么导致了这种移动,因为我过去对 move_and_slide
或 move_and_collide
的所有使用都运行良好。
func move(food_positions, delta):
if playing:
direction = choose_direction(food_positions)
if direction != null:
move_and_slide(direction*SPEED)
当生物在 'simulation' 场景中实例化时,此代码被外部调用。 food_positions
是用于计算所需方向的 Vector2 数组。因为它是从另一个场景调用的,所以我需要在使用 move_and_collide
.
choose_direction
的代码如下:
var closest_food_pos = null
var closest_food_distance = null
# iterate through each food
for food_pos in food_positions:
# get distance betweeen the two positions
var gap = food_pos - position
var distance_food_me = sqrt(pow(gap.x,2) + pow(gap.y,2))
# insert test to within view distance here
# confiming that we are not comparing to null
if closest_food_distance != null and closest_food_pos != null:
# comparing distances
if closest_food_distance > distance_food_me:
# setting to closest food
closest_food_distance = distance_food_me
closest_food_pos = food_pos
else:
#if null need to set to first food to compare
closest_food_distance = distance_food_me
closest_food_pos = food_pos
if closest_food_distance != null and closest_food_pos != null:
var gap = closest_food_pos - position
var go_direction = gap.normalized()
return go_direction.normalized()
else:
# add ai go towards function call here
return null
还没有完全完成,我打算在以后添加一些内容。
请注意,这是生物场景的一部分,在模拟场景中动态实例化,该场景是 pre-instanced/part 的主要场景,用于控制和以后的可用性。
git 中心存储库是:https://github.com/benpotter480/Evolution_simulation_that_doesnt_work
抱歉,如果我的存储库的某些部分没有正常显示,因为我是新手。
感谢任何建议。
好的,我有办法
前言
我首先在move_and_slide()
和运行项目中添加了一个断点,从主场景单步执行代码。没有什么看起来不对劲。正在设置和初始化这些值。我添加了 print 语句来找出 food_positions
、creature_positions
和 direction
随着时间的推移显示的内容,认为第一帧不是罪魁祸首。但是,什么都看不到。
我检查了生物场景。那里一定有东西。代码看起来不错,没有任何东西会导致 move_and_slide()
执行它正在做的事情。我运行 孤立的生物场景。你猜怎么着,它奏效了。所以,至少我知道它不在生物场景中。
这很麻烦。据我所知,它在模拟中无缘无故地失败了。我尝试生成 100 种食物,100 种生物。
然后是 1 个生物。
那个生物没有动。那是什么。当有更多的生物时,它们就像是被迫进入彼此。经过更多的摆弄之后,我无意识地转动 wall_left
静态物体并注意到那个小的 protube运行ce 然后,等等,那不是 RectangleShape2D
。
我删除了所有的墙...
成功了!模拟成功了!
TL;DR/解决方案
墙壁的 LineShape2D
碰撞形状的法线方向错误。将 LineShape2D
的 Normal
从 (0, -1)
更改为 (0, 1)
。
结论
真是让人头疼。希望这对您有所帮助,祝您编码愉快!