Build a Simple Avatar

In 3D programming, you can make simple graphics in two ways. We’ll use both in this game—one kind for the Purple Fruit Monster and the other kind for the fruit. The simple graphic technique that we use for the Purple Fruit Monster is called a sprite.

In the addAvatar() function, we create an invisible, physics-enabled box mesh, then we add the sprite to the box mesh. Add this function below the addGround() function.

​ ​function​ addAvatar() {
​  ​var​ shape = ​new​ THREE.CubeGeometry(100, 100, 1);
​  ​var​ cover = ​new​ THREE.MeshBasicMaterial({visible: ​false​});
​  ​var​ avatar = ​new​ Physijs.BoxMesh(shape, cover, 1);
​  scene.add(avatar);
​ 
​  ​var​ image = ​ ...

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.