Recently in the writing of some simple physics engine, in the writing of static collisions encountered a small problem, after the guidance of people finally understand, now write to share.
The physics engine is mainly used to simulate the forces of nature. In my opinion, there are two main mechanisms of the physics engine:
- Force Generator. is to simulate common Newton forces such as gravity, resistance, spring elasticity, and so on.
- Collision. The collision here is very broad, and basically the force generator can not simulate the situation will be able to use collision simulation.
Here is a digression, in the computer animation, especially the physical engine is mainly dealing with the position and speed of the particle, many of nature's physics is approximate to be composed of a plurality of particles. So, the main problem of collision is to solve three kinds of problems:
- The velocity of the particle changes during the collision, which is simple and can be simulated by the momentum conservation formula (i.e. m1v1+m2v2=m1 ' v1 ' +m2 ' v2 ').
- The position change of the particle in the collision process, this belongs to the collision detection problem, there are many mature algorithms, not to do in detail here.
- Static collision problem. is how to use collisions to simulate the process of physical stillness. Let's discuss the problem today.
In computer graphics, we can use OpenGL and other graphics API to draw some three-dimensional graphics in three-dimensional scene, such as ground, table, small ball. But the computer itself does not know what the ground is, what is a table, so when we add a stationary ball to the ground that is subject to gravity, we see only the ball falling, not resting on the ground. Of course we can add some geometric constraints (y<0, y=0), forcing the ball to be on the ground, but we can't do it with physics engine simulations, and we can simulate this as a collision, a type of collision called a static collision . 1, you can see the 2nd frame when the ball is located below the ground, we can think of the small ball is the intersection with the ground, the formation of collisions.
Figure 1
We certainly do not want our program to be stationary on the ground, but to continue to fall through the ground. We need to detect the collision behavior. As we can see in the 1th frame state, the position of the ball is no problem, but to the 2nd frame, the ball intersects with the ground. So we are looking for a two-frame collision information to determine whether the ball is stationary in the 1th frame state . We combine the analysis, because the ball only by gravity, that is, the speed of the ball is only caused by its gravity, it is easy to multiply the force of the time between the 1th and the 2nd frame of the interval (duration), and then get based on the current force of velocity data, this speed we write VG. Since we are dealing with the ball as a collision, the actual velocity of the ball at the 2nd frame can be obtained by the collision Mechanism v. We compare VG and V, if the actual speed V is less than VG, then the small ball in the 1th frame is in a stationary state.
So the question is, why compare these two speeds to make sure the ball is stationary in the previous frame? Let's analyze it in conjunction with the diagram. The 1th frame of the ball is stationary on the ground v=0, but we calculate that if the collision is handled, the ball will certainly produce a direction upward speed of V '. Then we compare with the Vg, V ' <VG, indicating that the ball does not move upward trend, so the initial first frame state of the ball is either stationary or very very small, can be approximated as static collision processing.
Figure 2
Finally, put a piece of code to handle this process.
Double newsepvelocity =-separatingvelocity * restitution; CVector3 acccausedvelocity = Particle[0]->getacceleration (); if (particle[1]) acccausedvelocity-= particle[1]-> Getacceleration ();d ouble acccausedsepvelocity = acccausedvelocity * Contactnormal * duration;/* Compare the speed generated by the external force and the speed of the object after the collision */ if (Acccausedsepvelocity < 0) {/*newsepvelocity is positive */newsepvelocity + = restitution * acccausedsepvelocity; if (newsepvelocity < 0) newsepvelocity = 0;}
Static collision of game physics engine