[ Pobierz całość w formacie PDF ]
.Fortunately, games can bemore forgiving, that is, it is okay to fake it.Here s a simple example of that:void CPObject::ResolveCollision( CPObject &other, CPPlane &plane){float VdotN;D3DVECTOR Vn,Vt;// Solve for VnVdotN = D3DRMVectorDotProduct( &plane.vNormal, &other.vVelocity);D3DRMVectorScale( &plane.vNormal, VdotN, &Vn);// Solve for VtD3DRMVectorSubtract(&other->vVelocity, Vn, &Vt);// Scale Vn by Elasticity CoefficientD3DRMVectorScale(&Vn, &Vn, fKr); // fKr is our coefficient of restitution// Calculate Vt - KtVnD3DRMVectorSubtract(vVelocity, &Vt, &Vn);}Note that although we have our own functions for calculating the vector arithmetic, weuse the Direct3D helper functions.There are actually quite a number of helper functionsto support vector arithmetic.We re performing a very simple reflection collision here.We can vary the amount offorce absorbed in the collision by varying our fKr variable, but that won t go far in mod-eling real-world physics.23 1634xCH16 11/13/99 11:15 AM Page 330330 Hour 16SummaryIn this hour, you learned about object hierarchies and how to use matrices, combinedwith those hierarchies, to apply object transformations to complex objects.You learnedabout kinematics and inverse kinematics, and how to calculate object orientations byusing simple algorithms.You also learned about collision detection and reaction, and how to model physicalbehaviors on objects while considering forces and dynamics.Armed with this knowl-edge, you are able to apply a physical model to your game world to simulate real-worldphysics.Q&AQ You said that having fewer than six degrees of freedom can decrease the num-ber of calculations that must be performed.Won t I always want six degreesof freedom?A For some of the objects you are modeling, perhaps.Some objects may have muchless.Consider a person walking on the ground.If that person never gains the abil-ity to fly, he will never have any translation freedom on the y-axis; therefore, hewill have fewer than six degrees, for sure.Q When performing tests for object collision, is it necessary to test all objectsagainst each other?A Not necessarily.Although I didn t cover it (there are only so many minutes in anhour), you can actually perform some additional calculations on your objects afterevery quantum of movement that will help narrow your search.Keeping track ofthe distances between objects will perhaps help you narrow it down, although youmight have to perform some square roots, which might be costly.Investigate themany possible algorithms for collision detection, many of which use space parti-tioning.Q Do I really have to use all the physics calculations discussed today? Thereseems to be an awful lot of calculations involved.Doesn t Direct3D take careof any of the details?A Of course you don t have to use all the physics calculations we discussed.You onlyhave to provide those physics and solutions to model more than just basic objectmovement.Direct3D provides many nonmember functions to help you calculatemost of the matrix math.This will allow you to at least move and scale yourobjects.You ll have to use the trigonometry and calculus functions to model real-world behaviors only when your game design requires it.23 1634xCH16 11/13/99 11:15 AM Page 331Modeling a Complex World Applying Physics and Object Hierarchies 331WorkshopThe Workshop is designed to help you anticipate possible questions, review what you velearned, and begin thinking ahead to put your knowledge into practice.The answers tothe quiz are in Appendix A, Answers.Quiz1.Given that vector a = [7 3 9] and vector b = [8 2 4], calculate the dot product16and cross product for the two vectors.2.What are the difference between kinematics, kinetics, and inverse kinematics?3.What is the difference between an AABB and an OBB?Exercises1.Take a look at the SIGGRAPH proceedings; they deal with computer graphics andanimations and are very informative.You might also want to check out some bookson physics, geometry, and calculus to brush up or improve your knowledge of themath involved in modeling 3D animations.2.Take a look at quaternions, and try to apply the physics and math you ve justlearned using them.Rewrite some of the functions provided here to use quater-nions instead of the matrices we ve used.3.The sample code doesn t provide true collision detection; it stops just short byusing collision spheres.Add the code necessary to find the faces (planes) of eachobject, and implement the algorithm described earlier in the hour.23 1634xCH16 11/13/99 11:15 AM Page 33224 1634xCH17 11/13/99 11:16 AM Page 333HOUR 17IntroducingDirectInput GettingUser InputDirectInput is the component of DirectX that is used to read input devicessuch as the keyboard, the mouse, joysticks, and flight yokes.Unlike themessage-based approach to handling the keyboard and mouse in traditionalWin32 programming, DirectInput provides a high-performance means ofobtaining input from the keyboard and mouse, which results in much betterresponse.Additionally, DirectInput is extensible enough to support veryadvanced input devices such as flight yokes and joysticks with multipleaxes.DirectInput is structured as a suite of COM objects, which is similar to otherparts of DirectX that you ve already learned about.This hour introduces youto DirectInput and shows you how to put it to work handling user input fromthe keyboard, the mouse, and joysticks.24 1634xCH17 11/13/99 11:16 AM Page 334334 Hour 17In this hour, you will learnWhat DirectInput has to offerAbout the main objects used in DirectInputHow to set up DirectInput to read from input devicesHow to read user input from the keyboardHow to read user input from the mouseHow to read user input from joysticksDirectInput BasicsPrior to DirectX, all user input in Windows was retrieved via the Win32 API.AlthoughWin32 offers full support for reading input from the keyboard and mouse, and limitedsupport for joysticks, it proved to be less than adequate for the high-performance inputneeds of gaming.Let s face it, even a slight lag between moving the joystick left andyour character in a game actually moving left could be the difference between life anddeath.Microsoft saw the need to provide a low-latency alternative to user input withDirectX to go along with the high-performance graphics, sound, and networking featuresthey had already developed.DirectInput was created for precisely this purpose: to provide a means of obtaining userinput from devices with an extremely low latency.Just in case you ve forgotten whatlatency means, in this case it refers to the delay between interacting with an input device(moving the joystick left) and the game responding to the interaction (moving your char-acter left)
[ Pobierz całość w formacie PDF ]