I am trying to implement turning of front wheels in PyBox2D, for now, I was able to make it turn left/right but I am not able to stop it when the angle reaches zero (to make the car to go straight)
(我正在尝试在PyBox2D中实现前轮的转向,目前,我能够使其左转/右转,但是当角度达到零时我无法将其停止(以使汽车直行))
My goal is to stop turning when the angle of a wheel reaches zero or value similar to zero, but not on the beginning (sometimes when the angles are zero they do not move at all, and if possible I would like to make it independent from pressing key on a keyboard (moving those two nested if statements out of the if keyboard_is_pressed(hotkey) part did not help
(我的目标是在轮子的角度达到零或近似于零的值时停止转弯,但不要在一开始就停止转动(有时,当角度为零时,它们根本不会移动,如果可能,我想使其独立于按下键盘上的键(将这两个嵌套的if语句移出if keyboard_is_pressed(hotkey)部分没有帮助)
I hope I made myself clear and thank you very much for any help
(我希望我能说清楚一点,非常感谢您的帮助)
EDIT I tried to implement solution given in the answer, it kind of worked but I tried to improve it and now I am stuck again, the wheels turn, but when they return to their initial position they stop moving.
(编辑我尝试实施答案中给出的解决方案,虽然可行,但我尝试进行改进,现在又卡住了,车轮转动了,但是当它们返回初始位置时,它们停止了运动。)
One of problems can be that when I press "a" or "d" key my variable self.ticking changes by more than just one, because I am not able to press the key for such a short period of time. (问题之一可能是,当我按下“ a”或“ d”键时,我的变量self.ticking改变的幅度不止一个,因为我无法在这么短的时间内按下该键。)
variable self.on_the_beginning is equivalent to on_starting_race from the answer below:
(变量self.on_the_beginning等效于以下答案中的on_starting_race:)
def control(self): # This makes control independent from visualisation
#Front left suspension: index 2
#Front right suspension: index 3
print(self.ticking)
if keyboard.is_pressed("a"):
self.suspensions[2].motorSpeed = -5
self.suspensions[3].motorSpeed = -5
self.ticking -= 1
if keyboard.is_pressed("d"):
self.suspensions[2].motorSpeed = 5
self.suspensions[3].motorSpeed = 5
self.ticking += 1
if self.ticking <= -3:
self.ticking = -3
self.on_the_beginning = True
elif self.ticking >= 3:
self.ticking = 3
self.on_the_beginning = True
if np.radians(-5) <= self.tires[2].wheel.angle <= np.radians(5) and self.on_the_beginning == True and self.ticking !=0:
self.suspensions[2].motorSpeed = 0
self.suspensions[3].motorSpeed = 0
self.tires[2].SetAngularVelocity = 0
self.tires[3].SetAngularVelocity = 0
self.ticking = 0
on_the_beginning = False
ask by Mechatrnk translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…