3D Animation Workshop: Lesson 76: Interactivity in Shout3D | 2
Lesson 76 - Interactivity in Shout3D - Part 3
The function that receives the input is called onDeviceInput, and it can receive both mouse and keyboard events. The Suit demo uses only mouse events. From the input information, the function can determine whether a mouse button has been pressed or released, whether the left or right button was used, whether the mouse is being dragged with the button down, and the current location of the mouse cursor in the applet window. Depending on the specific input, the onDeviceInput function calls other functions, and these make a determination that controls the renderer.
For example, if a mouse button has been pressed, the onMouseDown function is called. This function first determines where the cursor is located when the press occurred. If the cursor is not over the button icons in the applet window, the user wants to begin dragging the mouse to rotate or move the suit. If the left button is pressed, the rotate mode is set. If the right button is pressed, the move mode is set. If the click was in the button areas, the function determines which button was selected, and sets the mode accordingly to rotate the suit in the left or right direction.
As a mouse is dragged with a button down, the mode remains unchanged, but the current location of the cursor is updated. This enables the application to determine how far the mouse has been dragged since the button was depressed. If the mouse button is released, the default mode is set.
The mode is set in a variable called mouseInputState. Every time the renderer is prepared to render, a function named onPreRender is called. This function checks the current mode in the mouseInputState variable and sets the desired orientation or position of the suit so that it renders correctly. Assume that the rotate mode has been set by left clicking in the main area of the applet window. The onPreRender function measures the horizontal distance across the window since the user first began to drag. It then compares this distance with the total width of the window. If the user has dragged across the entire width of the window, the suit will be rotated 360 degrees. Smaller drags produce correspondingly smaller rotations. For example, dragging half the width of the screen rotates the suit 180 degrees. The rotation process appears continuous because the suit's orientation is updated with each render cycle, as the onPreRender function is called with the most recent cursor locations. The same process applies to moving the suit by dragging with the right mouse button, except that the vertical drag distance is used.
The default mode, and the left and right modes established by pressing the arrow buttons, are all essentially similar in that the suit rotates at a constant speed in each case. Here's where we get a good chance to understand the realtime frame rate concept discussed earlier. If you press on the turn buttons, you'll notice that the suit rotates a full turn (360 degrees) in every second. To make sure that the rate is constant, regardless of the frame rate, the rotation for every frame is adjusted by the fraction 360-degrees/ frame rate. For example, assume that the current frame rate is 10 frames per second. The fraction is therefore 360/10, or 36 degrees. Thus, the suit will be rotated 36 degrees each time a frame is rendered. Depending on the direction the suit is turning, each 36 degree change will be either added to or subtracted from the current rotation value. The default slow "autospin" uses the same concept with a speed of about 40 degrees per second.
We'll continue with our preview of Shout3D 1.0 in the next column.
|To Return to Parts 1 and 2, Use Arrow Buttons||
Created: Sept. 26, 1999
Revised: Sept. 26, 1999