How to record if an actor has been still for 1 second?

vikingviking Member, PRO Posts: 322

Hey there :)

I searched the forums but could not find the answer to the following simple problem: I need to know when my actor has stopped moving for one second.

My actor is a ball and I determine the speed of the actor by constraining my game.ballSpeed attribute to magnitude(self.linear velocity.x, self.linear velocity.y). Then I check if game.ballSpeed < 0.3 and if so, change attribute ballStill to true (otherwise change ballStill to false). This works fine 90% of the time. However, sometimes the ball rolls forward to an almost complete stop before gravity then makes the ball roll back the other way. My current logic thinks that the ball stopped even if the ball is still moving and this breaks the experience.

So, to be sure that the ball is indeed permanently still I would like to add a timer where I make sure that the ballSpeed has been less than 0.3 for at least a second before I change the ballStill attribute to true. Any suggestions?

Best Answer

  • meedoingmeedoing Posts: 18
    Accepted Answer

    Using timers is perfect way to control granular things. You are right.

Answers

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    edited May 2014

    Here's a very quick and dirty demo. Press the Space bar on your keyboard to begin the deceleration of the actor. When the speed is less than 10, I manually change the speed to 0.

    You can pick out the parts you need, but hopefully this pushes you in the right direction.

  • vikingviking Member, PRO Posts: 322

    Thanks for the suggestion, but it does not exactly give me what I want. If I do it this way, I would sometimes have a ball that rolls up a hill and slows down enough to force the change speed to 0. That means that the ball would stop half way up the hill and then not roll back down, which breaks the simulation.

  • vikingviking Member, PRO Posts: 322

    In case anyone else encounter the same problem as I did with this logic then here is the solution that I came up with after some experimentation:

    I added a self.ballAlmostStill attribute to the ball actor and if the ball speed is less than 0.3 then I change the self.ballAlmostStill attribute to true. Then I added a 1 sec timer that says that after 1 second, if the ball speed is less than 0.3 and the self.ballAlmostStill attribute is true then change game.ballStill to true and the self.ballAlmostStill attribute to false.

    So far so good. It seems to work perfectly.

Sign In or Register to comment.