This demo shows our simple "reactive" extension to make Babelsberg a "constraint-reactive language". The way it works is that there is a global system clock bbb.system().time, that can be used to define time-based constraints. In this example, we have a ball with two time-based constraints. The first says that the ball position is equal to its previous position plus its velocity * delta-time * scaling The second constraint says that the velocity is equal to the previous velocity + (acceleration * delta-time * scaling) These two constraints are just the iterative definitions for integrating speed and position of moving objects. The third constraint says the ball must not be below the ground. Here, we do not use a solver but a trigger, a special user-defined solver that simply runs some callback when the constraint becomes unsatisfied (and leaves it up to the user to fix the problem). This is useful for domain specific solving, like here, where the right solution is to revert the velocity (reflect) the ball (whereas some generic numeric solver would probably just set velocity to 0 and be done with it). To run the example, press the run button on the ObjectEditor. You can play with the global clock by executing code in the Javascript Workspace (stop the time, reset it, change the clock resolution (i.e. stepTime)
depth: 1
reset
bbb.system().stop()
bbb.system().time = 0
bbb.system().start()
bbb.system().time
bbb.system().stepTime = 10
bbb.system().singleStep()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
show vars
JavaScript Workspace
X

Menu
// changed at Tue Nov 22 2016 11:11:04 GMT+0100 (W. Europe Standard Time) by timfelgentreff
this.addScript(function reset() {
    this.resetInitialState()
    // ball falling
    var g = 9.81,
        scale = 0.001,
        ground = this.get("Ground"),
        ball = this;
    always: {
        ball.getPosition().equals(ball.getPosition().addPt(pt(
            0, ball.velocity * system.dt * scale
        )))
    }
    always: {
        ball.velocity == ball.velocity + g * system.dt * scale;
    }
    // reflection
    when(function() {
        ground.getPosition().y <= ball.getPosition().y
    }).trigger(this.bounce.bind(this))
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<lively.morphic.Morph#6AB50... - Ball>
Tag:
run
save
Tests

-- ALL --
bounce
reset
resetInitialState
Scripts
-
+
-
Connections
+
-- ALL --
all
ObjectEditor -- Ball>>reset
X

Menu
expand saved source
X