Doing something after laser is not in contact?

About advanced scenes, and the Thyme scripting language used in Algodoo.

Doing something after laser is not in contact?

Postby egkteam » Sun Oct 12, 2014 2:13 pm

So for instance I have a red ball and a laser. I want the ball to turn green if the laser hits it.
I made this script:
Code: Select all
onHitByLaser: (e)=>{colorHSVA = [120.0, 1.0, 1.0, 1.0]}


But I can't figure out how to turn it red again after the laser is removed from the ball. Any help? :)
My Algobox account (inactive)
P.S. My real name is Emmanuel so call me that way.
egkteam
 
Posts: 91
Joined: Mon May 21, 2012 3:49 pm
Location: East Java, Indonesia

Re: Doing something after laser is not in contact?

Postby Xray » Sun Oct 12, 2014 5:08 pm

One way to do it is with a timer. When the laser hits the red circle, it changes the color to green, and it also loads a local variable (_timer) with sim.time. As long as the laser keeps hitting the circle, the _timer will continually get set to sim.time. When the laser stops hitting the circle, another script located in postStep checks to see if sim.time is greater than the value in _timer plus 0.1 seconds. If it is, then the color reverts back to red. Here is the code:

First, create a local variable named _timer:
Code: Select all
_timer := sim.time

Next, add this code to postStep:
Code: Select all
    sim.time > _timer + 0.1 ? {
        colorHSVA = [360.0, 1.0, 1.0, 1.0]
    } : {}

And finally, add this code to onHitByLaser:
Code: Select all
  colorHSVA = [120.0, 1.0, 1.0, 1.0];
    _timer = sim.time


There will be a very slight delay (0.1 seconds) from the time the laser stops hitting the circle, to the time that the color actually changes. In most cases that slight delay will not cause any problems. If it does, then you will have to find some other method, without a timer, to do this.
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Doing something after laser is not in contact?

Postby egkteam » Sun Oct 12, 2014 5:15 pm

Okay, thanks! :thumbup: What is the use of the postStep script box?
Also, what is the purpose of underscores on variable names?
My Algobox account (inactive)
P.S. My real name is Emmanuel so call me that way.
egkteam
 
Posts: 91
Joined: Mon May 21, 2012 3:49 pm
Location: East Java, Indonesia

Re: Doing something after laser is not in contact?

Postby Xray » Sun Oct 12, 2014 5:28 pm

postStep executes script at the screen refresh rate, which is dependent on sim.frequency. Also, postStep will only execute the code when Algodoo is running, as opposed to update which will execute code all the time, regardless if Algodoo is running or not!

Here is a tutorial scene that I made which explains the difference between postStep and update. I hope you will have a better understanding of postStep and update after viewing it:

Script Timing Demo
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Doing something after laser is not in contact?

Postby egkteam » Sun Oct 12, 2014 5:33 pm

Thanks :)
My Algobox account (inactive)
P.S. My real name is Emmanuel so call me that way.
egkteam
 
Posts: 91
Joined: Mon May 21, 2012 3:49 pm
Location: East Java, Indonesia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 6 guests