[英]How to change value to zero but not in the first iteration of a loop
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 中实现前轮的转动,目前,我能够让它向左/向右转动,但是当角度达到零时我无法停止它(使汽车直行到 go)
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.编辑我尝试实施答案中给出的解决方案,它有点工作,但我试图改进它,现在我再次被卡住,车轮转动,但是当它们返回到最初的 position 时,它们停止移动。 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
If i understand correctly, you can have a variable, say on_starting_race, set to false, then check whenever it is above a set number (say, when it's above 10 you know for a fact that the race has already started and the car moved at least for a few seconds), then change that value to True, now add an if statement to determine whether the value is close to 0 (say val<5) AND on_starting_race is True.如果我理解正确,你可以有一个变量,比如 on_starting_race,设置为 false,然后检查它是否高于设定的数字(比如,当它高于 10 时,你知道比赛已经开始并且汽车移动到至少几秒钟),然后将该值更改为 True,现在添加一条 if 语句以确定该值是否接近 0(例如 val<5)并且 on_starting_race 为 True。 There might be a more elegant way, but this is pretty straight forward(assuming you check the state of the car every frame or a set period of time).
可能有更优雅的方法,但这很简单(假设您每帧或一段时间检查汽车的 state)。
Sorry, because I am not 100% sure of your problem without the whole code.抱歉,因为没有完整代码,我不能 100% 确定您的问题。
I think the solution could be using an input parameter in you function, let's say first_run, that you can control from inside and outside the function.我认为解决方案可能是在 function 中使用输入参数,比如说 first_run,您可以从 function 的内部和外部进行控制。
def control(self, first_run=True):
This way, you may start the race (from your main program) setting first_run to True, so you don't care about the angle.这样,您可以开始比赛(从您的主程序)将 first_run 设置为 True,因此您不必关心角度。 And use this same function setting first_run to False the rest of the times.
并使用同样的 function 设置 first_run 为 False 时代的 rest。
You can also use a self.first_run variable that you may set to True in the init , then setting self.first_run to False if it is True (which is really the first time you use your control() function).您还可以使用 self.first_run 变量,您可以在init中将其设置为 True ,然后将 self.first_run 设置为 False 如果它是 True (这实际上是您第一次使用 control() 函数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.