Hey there! This series of articles will teach you how to use the physics engine in Gandi IDE. You'll get the hang of using the blocks in the physics engine by learning from the best practices.
Try the showcase below, done with Physics. Just click the button to give a little push to the small box. You can tinker with the gravity, force, and direction to witness different effects.
In this article, we'll take it step by step to simulate the magic of gravity using the physics engine.
Step 1: Get the Physics extension installed
Open up Gandi IDE, find Physics in Extensions, and give it a click to install.
Once that's done, you'll find all the blocks right there in the code area.
Step 2: Add a sprite
Now we need a small box. There's already a sprite in the project, so feel free to delete the current costume and draw a little box in its place.
Every time the program starts running, make sure the small square is positioned right at the center of the stage area.
When the green flag is clicked, move it to (0,0).
Step 3: Set up the physics playground
Let's switch to the background and get the physics playground all set up.
First, use this block to define the stage area, and select "boxed stage" from the options.
To see the effects of each option, you can refer to the documentation of the Physics extension.
Next up, set the gravity. In the stage area, gravity should pull downwards along the y direction, so set x to 0 and y to -10 in this block.
Lastly, we want the physics engine to continuously simulate physical effects while the program runs. Use the Forever block to keep the step simulation running repeatedly.
After finishing this code, it should look something like this:
Now head back to the sprite and add this block to enable the physics engine for the sprite.
Give the green flag a click to run it, and you'll see the box gracefully drop from its initial position.
Step 4: Give the small box a little push
Use this block to give the small box a gentle push. You can adjust the value and direction of the force within the block. The direction corresponds to the direction value in Scratch.
Here, we're setting the force to 3 and the direction to 0.
Set the space key as the trigger to push the small box. You can write the code like this: when the space key is pressed, give a force to the small box.
Click the green flag to run it and give it a try.
Feel free to experiment by changing the direction to 45.
Voila! With the completion of these 4 steps, you've successfully created a full-fledged gravity simulation effect.
Now go ahead and give it a try in Gandi IDE. You've got this!