We already know a lot about the engine, we know how to load geometry, render it, create some 2D graphics. There is however a very important thing we didn’t do yet. What is a game without input? It’s not really a game right? Well, that’s why this tutorial is there. We’ll make sure we can rotate the camera around a cube, so we can view other areas of the scene we’re in.
What do we need?
- First of all we need the input engine of truevision3d. It provides us with an easy interface to the keyboard.
- We need to rotate around the cube, so we need to do some 3D math. This can be made easy by TV’s math library.
How do we set things up?
- First of all we initialize the inputengine and the math library. We do it this way:
Maths = new TVMathLibrary();
The initialize function of the input engine has 2 boolean parameters, the first for enabling or disabling the keyboard, the second one for the mouse. We’ll not use the mouse in this tutorial, but we’ll enable it nonetheless.
- We set the angle system of truevision to degrees instead of radians. We do this because it’s just easier to grasp.
That’s just everything we need to do to initialize the inputengine and make sure you can read the keyboard state.
How do we render this?
- The title is a little weird here, you don’t render input, but whatever, for consistence i chose this title any way, since it happens in the main loop. We will keep the rotation around the cube as a floating point number, to be able to smoothly rotate around it. If ‘a’ is pressed, we rotate to the left, if ‘d’ is pressed, we rotate to the right. This is done by the following calls:
if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_D)) fHoriz -= 0.03f * fTime;
You can see that there’s a boolean returning function called IsKeyPressed. You tell it which key you want to know is pressed. There’s an enum called CONST_TV_KEY, that contains all of the keys you can choose from.
- The last step is updating the camera to reflect the changes we made to the horizontal angle. We do it by the following call:
Cam.SetCamera(vNew.x, vNew.y, vNew.z, 0, 0, 0);
The first function is the MoveAroudnPoint function. It takes a couple arguments. The first one is a vector which is the point to rotate around. The last three are the radius, horizontal and vertical rotation. We rotate horizontally with fHoriz degrees and keep a distance (radius) of 20.
The second function sets the camera to the new position by the first 3 parameters. The last 3 are the lookat point. This determines where the camera is actually looking at. It’s looking at the 0,0,0 point, which is the center of the cube. What we just did, is calculate a new position for the camera and make it look back at the cube.
Things you can add yourself
- You can try to make the camera rotate around the x axis as well, to make sure you can go around it all the way.