The physical simulation engine is designed to simulate the Basic Motion of objects in the real world and between objects. The introduction of physical engines in the game can improve the game's authenticity and playability, such as the parabolic movement popped up by the angry birds and the cut-on movement in the rope. Since the physical engine is introduced into the game, the interaction between the physical engine and the game engine is designed. See the figure in the book:
The interaction between users, rendering world (game engine), and physical world (physical engine) is reflected. The user renders the elements in the world by operating the genie and menus in the game engine. The game engine then delivers the relevant data to the physical world, the results of simulated computing in the physical world pass a rendering world to re-draw objects such as genie. In this process, the two worlds will run independently according to their respective operation cycles, and in the physical world, this important operation cycle is called: time step.
Key components
The physical engine simplifies and abstracts the real world based on the actual needs.
The physical world b2world is composed of objects with rigid attributes and frame shapes on objects. It also contains the gravity vectors of the physical world;
B2body: a rigid body composed of locations, angles, damping, and other attributes. According to the motion status, it can be divided:
Static object (b2_staticbody): an object with zero speed and mass, generally not involved in collision detection;
Platform object (b2_kinematicbody): an object with a fixed trajectory and zero quality is generally not involved in collision detection;
Dynamic Object (b2_dynamicbody): an object that normally moves based on gravity and the applied external force (such as torques, impulse impulses.
Frame b2fixture: Connects objects and shapes, and has important data such as object quality, density, and friction coefficient. With such data, the movement of objects can be effectively simulated;
Shape: shape is a simplified representation of an object, indicating the boundary of collision detection. It is attached to the frame. A frame can only be connected to one shape. Box2d provides a circle (b2circleshape) and a polygon (b2polygonshape );
Joint b2joint: used to connect two objects and restrict the movement of objects. The joint has two important attributes:
Joint limit: Specifies the range of motion;
Joint motor (joint motor): driving the motion of a connected object.
Provides user data attributes in objects, frames, and joints. Its type is void pointer. That is, this void pointer provides a channel for data interaction between the physical world and the rendering world.