问题描述
目前,我正在克隆游戏Flappy Bird进行练习。我将这只鸟表示为RigidBody2D
。
extends RigidBody2D
func _ready():
pass
func _input(event):
if Input.is_action_just_pressed("input_action_flap"):
bird_flap()
func _physics_process(delta):
print(self.rotation_degrees)
self.rotation_degrees = -45
print(self.rotation_degrees)
func bird_flap():
var des_linear_veLocity_x : float = get_linear_veLocity().x
var des_linear_veLocity_y : float = -750
var des_angular_veLocity : float = -3
set_linear_veLocity(Vector2(des_linear_veLocity_x,des_linear_veLocity_y))
set_angular_veLocity(des_angular_veLocity)
我正在研究限制Node可能旋转的方法。但是,我遇到了一些问题。
-135.234329
-45
-137.498474
-45
-139.724884
-45
-141.914169
-45
-144.066986
-45
-146.183914
-45
-148.265564
-45
-150.312515
-45
-152.325363
-45
由于Godot内置的物理引擎,旋转值似乎正在忽然忽悠。并且当我尝试使用方法clamp(...)
时-随后我遇到了角色精灵旋转的剧烈闪烁。
self.rotation = clamp(self.rotation,deg2rad(-45),deg2rad(+45))
有人知道如何解决此问题吗?
解决方法
直接更改物理物体的状态通常需要使用_integrate_forces
方法而非process
方法来完成。
(请参见顶部的注释:RigidBody2D documentation)
在这里,我正在创建并设置一个新的转换。
func _integrate_forces(state):
var rotation_radians = deg2rad(rotation_degrees)
var new_rotation = clamp(rotation_radians,-0.78,0.78)
var new_transform = Transform2D(new_rotation,position)
state.transform = new_transform
请记住,angular_velocity
仍在使用中,这会使您的身体“粘”在最大/最小旋转角度上。您可以通过在编辑器中调整angular_damp
或在每次击中目标angular_velocity
时手动更改代码中的rotation_degrees
来解决此问题。