Join Seb Lee-Delisle for an in-depth discussion in this video Placing the 3D game in an outer space environment, part of Papervision3D 2 Essential Training.
- View Offline
So now we have got our 3D PingPong game all up and running, let's add a reflection to it. Because obviously, all projects are at least 25% better with a reflection, or at least that's what I think. So there is a really handy object in Papervision and it's a type of BasicView. But instead of rendering the scene once, it also renders the scene upside-down as though it was reflected off the surface of the ground. It couldn't be easier to use. You just literally replace BasicView with ReflectionView.
Let's just take a quick look at this. Now obviously this doesn't quite look right. This is because by default the reflection is at Y-position of 0. As the arena lines are actually below 0, it means that the reflection is going upside-down and appearing on top of the reflection. This is an important thing to bear in mind. The ReflectionView isn't smart enough to know what's under the ground and what isn't. So you need to make sure that all of your objects are over the ground. Also, as the surface of the reflection is exactly half-way though the bat and the ball, you don't see any reflections on those either.
The reflection is getting obscured by the actual object itself. So the way to fix this is to change the surface height of the reflection so that it's underneath those objects. I will show you how to do that. So, in the method that initializes the 3D I will scroll down and find it. We are going to move the surfaceHeight of this ReflectionView down to -16. That should be underneath those objects. So let's take a look at our reflection now. So you can see now that our reflection is the exactly where you want it to be and those objects look like they are skidding across an incredibly shiny surface.
There are some other useful features of the ReflectionView. You can actually change how the reflection is adjusted and by default, it just automatically makes it a bit darker, but you can actually adjust that yourself using setReflectionColor. setReflectionColor takes redMultiplier, greenMultiplier, blueMultiplier, and the red, green, and blue Offsets. Those behave in exactly the same way as a traditional Flash colorTansform objecs. But we are not going to do that now. We are happy with the default setting. So let's move on. Now, just for fun, let's try something here.
We are going to replace the ball with our Earth object from before. If we drill down into our package here, I have already made an object called Earth that's designed especially for this use. Notice that as Earth extends Ball, it means that we can freely replace it in our game and we won't get any problems there, because the Earth object will inherit all the functions of the Ball that we need. So here is the same stuff as we had before where we were making the Earth. It creates the shade and the light, it makes the sphere, maps the Earth bitmap material onto it.
There is only one extra thing here from the Earth that we have before and it's this little bit of code here. What this does is it rotates the sphere around the Y-axis in the direction that it's moving and then it adds 24 degrees to the X rotation every frame. This means that no matter which direction the Earth is rolling, it will always look like it's rolling along properly rather than skidding across the floor. So now let's replace the ball in our game with the Earth. We need to find initObjects. Scroll down here.
This is where we are creating the ball. So instead of the Ball, let's make it an Earth. Make sure that's imported. So now we have the Earth rolling around instead of a ball. But of course, the Earth doesn't entirely belong on a table tennis table. So let's go one step further and put it in space. Now I have created here an object called Skybox. Obviously, we covered Skyboxes in the earlier chapter, but this time we are creating a cube object that sets up the Skybox for me. The only difference with this and the Skybox that we used before is that in the constructor for the cube we are specifying that we don't want a bottom side to this cube.
So the bottom of the cube is left open. We are also moving the cube up so that it doesn't go through the floor. Now what we need to do is add this into the game into the initObject method. And we will just add it into the scene here. I will now create a new Skybox. Let's take a look at that. So now we have learned how to add a reflection to our Papervision BasicView very easily. We have also looked at how we can replace the ball in our game with an Earth and add a Skybox into our scene.
Users should have a basic knowledge of Flash Builder and Actionscript 3.
- Creating 3D primitive shapes
- Controlling rotation
- Moving the camera in 3D space
- Creating a particle field with depth-of-field blur
- Importing models from external applications
- Making a simple 3D game
- Using ViewportLayers to selectively render and sort layers
- Placing a custom 3D model into an augmented reality scene
- Creating interactive carousals
- Creating interactive grids
- Working with particles and billboards