# 2D Physics Engine with watchX

Last day, I have decided to design a code that can simulate the gravity acting on an object. My aim was to implement the basic position, velocity and acceleration into a code. Then calculate how an object bounces from surfaces. You may remember my Spirit Level example. My aim is to design an environment that a pixel-sized object can move and bounce off the edges with respect to real-life physics phenomenons.

First, let’s understand the mechanics of a free falling object. A gravitational acceleration G effects the object at all instances, therefore speed and position of the object should be calculated for every cycle.

When the object hits the ground, it should bounce. The bounce action reverses the object’s velocity vector by the means of object elasticity and in the bouncing action, acceleration becomes 0 again.

So far so good. We have made the calculation for only the Y-Axis. Now, let’s put the X-Axis into play too. There is no acceleration in this axis so that the object will move in more like a parabolic trajectory.

Good, but if there is no acceleration the object will continue to move in that axis with constant velocity. That is not realistic at all. So I’ve added friction. And I decided to remove the Y-Axis acceleration so that the object will move in much like a pool table fashion.

I also decided to put together some code that will randomize object position and velocity.

Position, velocity, acceleration, elasticity, bounce and friction. They are all set. Now the fun begins. Let’s tie together the acceleration data in both axis with IMU data.

Let’s have a look at the code,

watchX Program starts, Sets variable values and runs the calculating and drawing code forever. The wait is set to 0.002 sec. This results in 500 FPS. I don’t think that it runs that fast but it is pretty quick and looks really fluent in person. This wait effects acceleration and other parameters too. You can change and play with it to change the feel of the program.

We have Set_Variables. This function sets gravitational acceleration along X and Y Axis. Object elasticity (1 is super elastic bounce 0 is no bounce) Friction (This is better set between 0.9999 and 0.995) The position and velocity for X and Y are randomized for fun.

Calculate Position function calculates the position of the object for every instance. The relations here are derived from the formulas in the beginning. These formulas are taken from the real life equation and that’s why the movement looks so realistic. If you can mathematicly model and put anything into a program you will be amazed by the results.

Randomize function sets random position and velocity data just for fun in every button press.

This function looks for borders and calculates collisions and bounce. This is also derived from real-life formulas.

Here you can find the scratch code and play with the parameters yourself. Let’s see what you will come up with!

5 Likes

I am surprised to see such a powerful work on watchX. The math behind it looks a little frightening though:D

1 Like

Really great work.
That reminds me of these wooden marble labyrinth games.

Just a question, is there no IMU calibration needed or is it automatically done?

1 Like

For some us the math are less frightening than code in some projects

1 Like

Thanks
In my case the IMU worked right out of the box. But we have to ask @mtulu for this.

@francois isn’t it amazing that you put the mathematical model behind a known phenomenon into your code and it just works?

Yes it is. And it’s very helpfull to give us idea of application.

@francois and @ecaldir no calibration made, it works as it is out of the box. The thing is it might a couple of degrees off however above code don’t really need a precise output. When you click the block on Mblock app it gives the Arduino code on the right. It’s always good idea to peak at that code for reference:)

1 Like

Thanks
maybe can do auto wake up clock… …
Flip the wrist to wake up the watch…
My English is weak sorry

1 Like

There is an open source watch firmware that has, tilt to see feature. Please see TapClock for WatchX

Hello,

I find this topic awesome and for me this is the purpose of watchx as a STEM device.
Being able to explain physic laws and write them in code, I love that.

So I came back on the code “Bounce” in watchx blocks.
I added the variables in a menu where you can set them (using the pad) and see direclty the impact of the change on the way the pixel bounces.

Firstly, I found that watchx blocks defines a variable as integer if you set an integer as first value. So you need to add decimals everywhere if you want that the calculation works (multiply with friction and acceleration later) :

Then, as you need an integer to draw the pixel you have to round the result of the multiplication :

And here is how I set the variables. I want to push the B button of the pad to see the menu (you need to keep pressing B) :
(compteur = count in French, this count is the position in the list of variables)

Then, still pressing B, you use the arrows to change the variables :

“facteur accéleration” is the ratio used on the acceleration sensor. The highest it is, more quickly it will react to the move of the watchX

3 Likes

Can’t wait to see the video:))

Awesome work! Please share your .wxb file with us so that we can play with this as well.

1 Like

Bounce 3.0 video

the .ino code :

bounce.ino

(this is my first time on github, tell me if something wrong )

There is actually a significant bug to my latest version. When I saved once, there is no way to save later (on the same name or another name) .
All the buttons “save” doesn’t activate any saving.

So I don’t want to restart the watchx blocks and loose all my program. I will copy it on a second computer and try to save at the end to keep this program.

Edit : I confirm the same bug on my second laptop (windows both). That’s strange because I was able to save 2 or 3 times, but after having modified the code I encountered the same blocage.

Excellent work:) I actually shared the video with a University professor which used to be my colleague teacher. This is a great project to teach physics to students

1 Like