Feb 11, 2019
6720 Views
0 1

Getting started with physics in Cospaces.edu

Written by

Hopefully you’ve had some experience with creating environments and objects in Cospaces already. In this tutorial, we will go through how to give some physics characteristics to our objects.

Initial scene with jungle environment, elephant, ball, and camera

In my initial scene, I’ve created some objects with the intent to have them interact physically. What I’ll probably do is just have the ball bounce in place until the user taps it, then it will bounce toward the elephant and when it hits the elephant, the elephant will get mad (trigger “angry” animation).

To begin, make sure your “Use in CoBlocks” option is checked on for both objects, the elephant and the ball.

The code menu, double-click on elephant to get here.

Next, you will also want to turn on the “physics engine” for your objects. So double-click on the object like before, click on physics, click the “Physics” slider to on, click the “Precise collision” on, and set the elephant mass to be 1000 kg and the ball to be 2 kg. Then set the ball’s bounciness to be 1.

The physics menu

Now, if you hit the “Play” arrow in the top right corner you should see your scene with the elephant standing still and the ball bouncing in place.

Coding time

Next, we will program the interactions between our two objects. Click on the code symbol in the top right corner. For clarity, double-click on the blue “CoBlocks” button and rename it “Ball”. This block of code will be for the ball.

Create a yellow “event” block that says “when Ball is clicked” and add a pink “Physics” block inside of it that says “push Ball forward with velocity 1”. Depending on where you placed your objects, you may need to change the direction of the push (mine changed to “backward”). Also, change the velocity to 3.

Notice I built a glass wall object to hold the ball. This is to make it easier to click on the ball when I hit Play. Go ahead and test out your project!

(Let me guess, your ball is falling through the glass wall object. Make sure you turn on “Physics” for the object. You could also make it “static” just to make it less computationally heavy.)

The following code block is for programming the interaction once the ball hits the elephant.

I also added an elephant sound to trigger once the ball hits the elephant. You can find a free one here on Freesoundeffects.com. To upload a sound, click on the “Upload” button at the bottom of the page and then click on the “Sound” button. You can record your own sounds here or upload one.

Elephant code

That’s it! Now hit Play and try out your creation!

Article Categories:
Guide


Leave a Comment

Your email address will not be published. Required fields are marked *