10 Jul 2013 23:15

## Netwire bouncing ball

```Hello,

I'm trying to get a grasp of netwire by implementing a bouncing ball
simulation and I'm failing.
The ball starts from the ground with a given velocity and when hitting
the ground the wire inhibits successfully. Now I'm kinda stuck.

How can I make the ball bounce?

Here is the code:

{-# LANGUAGE Arrows #-}

module Main where

import Control.Wire
import Prelude hiding ((.), id)
import Control.Concurrent

type Pos = Double
type Vel = Double
type ObjState = (Pos, Vel)

testApp :: Pos -> Vel -> WireP () ObjState
testApp p0 v0 = proc _ -> do
v <- integral_ v0 -< -9.81
p <- integral1_ p0 -< v
when (>= 0) -< p
returnA -< (p, v)

```

10 Jul 2013 23:32

### Re: Netwire bouncing ball

```On Wed, Jul 10, 2013 at 2:15 PM, Just <haskell <at> justnothing.org> wrote:
> Hello,
>
> I'm trying to get a grasp of netwire by implementing a bouncing ball
> simulation and I'm failing.
> The ball starts from the ground with a given velocity and when hitting the
> ground the wire inhibits successfully. Now I'm kinda stuck.

I've never used netwire (although I've used yampa and
recative-banana), so I can't give you help with the code, but maybe I
can help with the concepts.

I think I see what is wrong. You need to keep applying forces to the
ball. Right now, the code says, once the ball falls below a certain
point, stop applying the force (eg., clamp the output of the
integral). Instead, you could apply an upward force at the point of
impact. You can get this from newton's third law (equal and opposite
reaction). The easiest way to see a bounce would be to simply negate
the velocity when you detect a collision with the ground. A more
accurate way might involve some calculations to figure out the
impulse, but then you'll need more things like the mass of the ball.

I hope that helps,
Jason

>
> How can I make the ball bounce?
>
>
> Here is the code:
```

10 Jul 2013 23:44

### Re: Netwire bouncing ball

```Just <haskell <at> justnothing.org> wrote:

> I'm trying to get a grasp of netwire by implementing a bouncing ball
> simulation and I'm failing.
> The ball starts from the ground with a given velocity and when hitting
> the ground the wire inhibits successfully. Now I'm kinda stuck.
>
> How can I make the ball bounce?

A very simple way to do this is to use integralLim_ instead of
integral_.  It allows the ball itself to handle the bouncing.  A less
invasive way (i.e. you can add it to your example) is to use the (-->)
combinator:

ball = integral_ 0 . integral_ 40 . (-9.8)

aboveGround = require (>= 0)

bouncingBall = aboveGround . ball --> bouncingBall

While this gives you a bouncing ball, the ball will not follow real
physics.  Once the ball hits the ground, it will just start over with
its original velocity.  integralLim_ is the correct solution.

Greets,
Ertugrul

--

--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
```

11 Jul 2013 18:52

### Re: Netwire bouncing ball

```On 07/10/2013 11:44 PM, Ertugrul Söylemez wrote:
> A very simple way to do this is to use integralLim_ instead of
> integral_.  It allows the ball itself to handle the bouncing.  A less
> invasive way (i.e. you can add it to your example) is to use the (-->)
> combinator:
>
>      ball = integral_ 0 . integral_ 40 . (-9.8)
>
>      aboveGround = require (>= 0)
>
>      bouncingBall = aboveGround . ball --> bouncingBall
>
> While this gives you a bouncing ball, the ball will not follow real
> physics.  Once the ball hits the ground, it will just start over with
> its original velocity.  integralLim_ is the correct solution.

Thank you very much, this works as expected and is easy to understand.
However a complete example of a bouncing ball would be super awesome
since I have trouble to get it work with integralLim_.

My first try was to use object_ from Control.Wire.Prefab.Move but got
stuck very quickly.

I think this would be a good addition to the quickstart tutorial.

_______________________________________________
```

12 Jul 2013 00:02

### Re: Netwire bouncing ball

```On 07/11/2013 05:52 PM, Just wrote:
> On 07/10/2013 11:44 PM, Ertugrul Söylemez wrote:
>> A very simple way to do this is to use integralLim_ instead of
>> integral_.  It allows the ball itself to handle the bouncing.  A less
>> invasive way (i.e. you can add it to your example) is to use the (-->)
>> combinator:
>>
>>      ball = integral_ 0 . integral_ 40 . (-9.8)
>>
>>      aboveGround = require (>= 0)
>>
>>      bouncingBall = aboveGround . ball --> bouncingBall
>>
>> While this gives you a bouncing ball, the ball will not follow real
>> physics.  Once the ball hits the ground, it will just start over with
>> its original velocity.  integralLim_ is the correct solution.
>
> Thank you very much, this works as expected and is easy to understand.
> However a complete example of a bouncing ball would be super awesome
> since I have trouble to get it work with integralLim_.
>
> My first try was to use object_ from Control.Wire.Prefab.Move but got
> stuck very quickly.
>
> I think this would be a good addition to the quickstart tutorial.

It would indeed be a fantastic addition - this is almost exactly what I
was trying to do as my example netwire project. For a bit more support,
I was trying to move a point from x=0 to x=10, and then back to x=0 -
and so on. I got as far as getting to x=10, but then had no idea how to
```

12 Jul 2013 02:15

### Re: Netwire bouncing ball

```Just <haskell <at> justnothing.org> wrote:

> Thank you very much, this works as expected and is easy to understand.
> However a complete example of a bouncing ball would be super awesome
> since I have trouble to get it work with integralLim_.
>
> My first try was to use object_ from Control.Wire.Prefab.Move but got
> stuck very quickly.

object_ is a generalization of integralLim_.  It pretty much allows you
to encode any moving behavior you want.  In general I recommend going
with the integral* wires as far as possible.

> I think this would be a good addition to the quickstart tutorial.

Thanks for your feedback.  I should write a complete example application
in the next revision.

Greets,
Ertugrul

--

--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
```
```_______________________________________________