Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

python - 如何将值更改为零,而不是在循环的第一次迭代中更改(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中实现前轮的转向,目前,我能够使其左转/右转,但是当角度达到零时我无法将其停止(以使汽车直行))

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

(可能有一种更优雅的方法,但这很简单(假设您每隔一帧或在设定的时间段内检查汽车的状态)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...