Teaching Children 3D Animation Programming With Collision Detection

Teaching Children 3D Animation Programming With Collision Detection

Gamification” is a great technique when teaching coding for kids. In this article I’ll demonstrate how to create a simple 3D animation with collision detection and interaction between two 3D objects in space.
Prerequisites: you should have an Android device with SceneMax 3D programming language for kids installed.
Let’s start by adding a Sphere on the right side of our scene:

S is a Sphere having material="rock2" and pos=(2,1,0)

Now let’s add a Ninja on the left side of the screen:

N is a Ninja having pos=(-1.5,-3,0) and rotate=(0,-90,0)

Next, let’s add some nice “lagoon” background to our scene:

Skybox.show "lagoon"

Now, for the interesting part – let’s add some interaction code when collision between the Sphere and the Ninja is detected:

When S collides with N do
  S.stop
  sys.print "Ninja Hit!", color=yellow, size=5
  N.rotate(y-360) in 0.5 seconds async
  N.move(x-1.5) in 0.1 seconds
  N.move(x+1.5) in 0.5 seconds
  sys.print ""
End do

In the above collision code we stopped the Sphere movement allowing it to move in the opposite direction, then rotated the Ninja 360 degrees while moving it fast back and forward.
We managed to perform rotation and movement at the same time by using the “async” keyword which tells the computer to execute the next command without waiting for the current command to finish (Multi Threading).
Finally let’s add the Sphere movement back and forward 10 times in a loop. While the Sphere is moving we will run an “Idle” animation to the Ninja:

Do 10 times
  N.Idle1 async
  S.move(x-4) in 0.1 seconds
  S.move(x+4) in 0.5 seconds
End do

That’s it! the entire program is about 15 lines of simple to teach code. Here is a list of the whole program:

S is a Sphere having material="rock2" and pos=(2,1,0)
N is a Ninja having pos=(-1.5,-3,0) and rotate=(0,-90,0)
Skybox.show "lagoon"

When S collides with N do
  S.stop
  sys.print "Ninja Hit!", color=yellow, size=5
  N.rotate(y-360) in 0.5 seconds async
  N.move(x-1.5) in 0.1 seconds
  N.move(x+1.5) in 0.5 seconds
  sys.print ""
End do

Do 10 times
  N.Idle1 async
  S.move(x-4) in 0.1 seconds
  S.move(x+4) in 0.5 seconds
End do

And this is what you should expect to see when running the program:

 

Leave a Reply

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

Skip to toolbar