|
part f: collisionsPart f is the final section of the project and thus collisions, along with other final touches, have been introduced. The class structure that has been used up to this point has allowed for collision detection and collision reaction to be easily implemented. The PhysicalModel System All objects that are to conform to physics, namely collisions in the case of project 1, are instances of the PhysicalModel class (fig. 1). A PhysicalModel, besides keeping track of the the positional data, holds the Field, Controller, and Collision data for an object existing in the game space. A Field is a physical force that a model experiences. Using fields allows the system to apply many external forces to a model. In the case of this project all models have a Gravity object attributed to them. This keeps the objects from flying up into space by attributing a downward dynamic force. Like gravity. Whooooo. A Controller is the object that governs the optimal motion of the object. Three types of Controllers presently exist: OptiMoveController, ConstantController and the MouseController. The OptiMoveController moves and object based on the track positioning and functions as a rudimentary AI. This is how the opposing plane is guided through the track. The path finding abilities of this model can be found in part b. The ConstantController moves objects at a constant, pre-defined speed. Only by colliding with an object can the straight line trajectory be affected. The MouseController is how the user controls his vessel. The MouseController uses overall mouse positions in the game window to calculate a speed to move the vessel by. This is discussed in detail in part d. A Collision object keeps track of the impulses' a model experiences during each frame of game play. As collisions are detected the resulting velocity vector is calculated for each object and added to the impulse amount in the Collision object. The information produced by these objects is used to calculate the next position on the PhysicalModel. As can be seen in figure 2, velocity vectors are created from the information held in the Field, Controller, and Collision objects of the PhysicalModel. These are then combined to form a final velocity vector that is used to displace the model to the next position. Using this system allows for multiple forces to act on a model seamlessly. Collision Detection and Reaction Collision detection is simply a matter of detecting the distance between two different bodies. In this program all collision hulls are represented as spheres, thus the sole important metric within collision detection is the radius of said sphere. Using this model, collision detection become the simple task of comparing the distances of separate bodies to the sum of their two radii. When the summed radii is greater, the two bodies have collided and have begun to intersect. This is illustrated in by the gray shade area in figure three. Once collisions have been detected proper reactions must be calculated and applied to each PhysicalModel (fig. 3). PhysicalModel's have mass and thus conservation of momentum can be applied to calculate rather realistic collisions (assuming no friction). One model is compared to the rest of the models consecutively If a collision is found the resulting velocity for the focused model is calculated using the following equations:
By using laws of conservation of momentum (upper) and conservation of energy (lower) on can easily derive the following equation for a single bodies final velocity after a elastic collision. It is important to note that all collisions in the game space are purely elastic.
Because the effect of the collision is not taken into account until the next frame of the game, each PhysicalModel's collision response can be calculated one at a time without fear of data distortion. The following is a pseudo code snippet of how basic collision detection/reaction works in the system. The tar detectCollision(PhysicalModels[] models){ This function is fully implemented in the code, in CarSimulation.pde on line 323. Extra Features One of the main extra features is the dynamic nature of the PhysicalModel's in the simulation. By pressing the '+' and '-' keys one can add and subtract balls. By pressing 'i' one can view the information on the balls, namely their mass and size. The jump pad demonstrates the abilities of the Field class. Press 'p' to drop jump pads onto the field. Watch as balls and your vessel are launched into the air when driven over them. A jump pad simply inflicts a collision impulse directly up and the Field affecting the model pulls it back to the ground. Press 'shift + p' to delete pads. |