Reference: http://blog.sina.com.cn/s/blog_61feffe10100msbz.html, http://allenchou.net/2013/12/ game-physics-constraints-sequential-impulse/
Constraint is the restriction of the movement of objects, can be used to deal with collisions, simulating joints and other things. For example, a collision is the minimum distance of two objects is at least 0, the joint is two objects are nailed together by a nail can only be rotated around the nail, the spring is the distance of two objects to remain within a certain range.
For a 3D rigid body there are 6 degrees of freedom, x, y, Z axis of the linear motion of each one, the angle of 3. 2D rigid body has 3 degrees of freedom, X, Y axis two, angle only one. Adding a constraint to a pair of objects limits these degrees of freedom.
For example, if 2D objects A and B are connected by a pole, then they will move up to a distance equal to the length of the rod, so that an equation can be obtained:
(XA-XB) 2 + (ya-yb) 2-l2 = 0
It's actually a two-point distance formula. The above formula can express a constraint.
To express the constraints of each situation, set a formula:
C (XA, YA, RA, XB, YB, RB) = 0
where r is the angle of the object, C is a function that expresses the constraint condition. The case of 3D adds Z and two other angles to the parameter.
But the actual use of the object is to constrain the speed of the object rather than directly change the coordinates of the position, so C with time t derivative:
DC /dt = dc /dx * dx /dt= Dc /dx * v
v is the velocity vector, v = [vxa vya wA vxb vyb wb]t, VXA is the x-axis velocity of object A, WA is the angular velocity of object A. x is a vector function of position.
Then according to the derivative chain rule, DC/dx into an Jacobian matrix J(can simply turn the encyclopedia, do not know what is actually not much relationship), and then become:
DC /dt = J * v
J = [Jvxa jvya jWA jvxb jvyb JWB]
Then the equation for C = 0 above becomes:
J * V + b = 0
This suddenly came out of the B is a "bias" item, although do not know exactly what is a but in some places will be used.
V is also the column vector about the velocity of two objects, in order to meet the constraints of the object speed change before and after the above formula to meet the above, so:
J (v +δv) + b = 0
force F = JT * λ, Jacobian matrix J is the matrix of C 's gradient (the derivative can indicate how quickly the function changes along a certain amount), and the λ vector represents the size of the binding. It is almost by imposing "binding" to make the motion of the object satisfy the constraint condition.
f = Ma, a = f * M-1, V = A * t, V = f * M-1 * t
Thus: ΔV = JT * λ * M-1 *δt
M is a matrix about the mass of two objects:
m = [ma 0 0 0; 0 Ia 0 0; 0 0 MB 0; 0 0 0 Ib]
Where ma = [ma 0; 0 Ma], IA = ia, M is mass, I is the moment of inertia. In case of 3D Ma = [ma 0 0; 0 ma 0; 0 0 ma], I-A aligns 3 angles is also a 3x3 matrix
DC /dt = J * V + J *δt * M-1 * jt * λ
J * M-1/ JT (Δt * λ) = dC /dt- J * V
Impulse p = F * t, change λ from force to impulse to hide that δt,λ= λ *δt, in order to satisfy the constraint DC/dt = 0, then:
J (V + M-1 * JT *λ) + b = 0
And then the following is obtained:
Λ=-(J * V + b)/(J * M-1 * JT)
ΔV = M-1 * JT *λ
So how do we apply constraints?
First of all two objects current speed V is consistent, Jacobian matrix J is its own constraint relationship based on the need to calculate, and then use the code to calculate the Δv, then v +=δv, It will eliminate the motion that the object does not satisfy the constraint.
Bashi are thrown into matlab to calculate, get:
λ= (-JVXA * vxa-jvya * VYA-JWA * WA-JVXB * vxb-jvyb * VYB-JWB * wb-b)/(JVXA * jvxa/ma + Jvya * jvya/ma + JWA * Jwa/ia + jvxb * jvxb/mb + jvyb * jvyb/mb + JWB * jwb/ib)
ΔV = [Jvxa/ma *λ,
Jvya/ma *λ,
Jwa/ia *λ,
JVXB/MB *λ,
JVYB/MB *λ,
Jwb/ib *λ]
The links below have some other people who derive good J and B
Collision constraint: http://allenchou.net/2013/12/game-physics-resolution-contact-constraints/
Point-to-point constraint: http://blog.sina.com.cn/s/blog_61feffe10100mt08.html (http://www.codezealot.org/archives/225)
Distance constraint: http://blog.sina.com.cn/s/blog_61feffe10100mtgp.html (http://www.dyn4j.org/2010/09/distance-constraint/)
(function(P, undefined) {' Use strict '; P.require (' Class '); varC; C=P.class (NULL, function(Obodya, Obodyb, Njvxa, Njvya, Njwa, NJVXB, Njvyb, NJWB, NB) { This. Bodya =Obodya; This. Bodyb =Obodyb; This. Jvxa =Njvxa; This. Jvya =Njvya; This. JWA =Njwa; This. JVXB =NJVXB; This. Jvyb =Njvyb; This. JWB =NJWB; This. B =NB; }, {bodya:NULL, Bodyb:NULL, Jvxa:NULL, Jvya:NULL, JWA:NULL, JVXB:NULL, Jvyb:NULL, JWB:NULL, B:NULL, Solve:function () { varBodya = This. Bodya, Bodyb= This. Bodyb, Vxa=bodya.velocity.x, VYa=bodya.velocity.y, WA=bodya.angularvelocity, Vxb=bodyb.velocity.x, Vyb=bodyb.velocity.y, WB=bodyb.angularvelocity, Invma=Bodya.inversemass, INVMB=Bodyb.inversemass, InvIA=Bodya.inverseinertia, Invib=Bodyb.inverseinertia, Jvxa= This. Jvxa, Jvya= This. Jvya, JWA= This. JWA, Jvxb= This. JVXB, Jvyb= This. Jvyb, JWB= This. JWB, b= This. B, Lambda= (-JVXA * Vxa-jvya * VYA-JWA * WA-JVXB * vxb-jvyb * VYB-JWB * WB-b)/(JVXA * JVXA * invma + jvya * Jvya * invma + JWA * JWA *InvIA+ JVXB * JVXB * invmb + jvyb * jvyb * invmb + JWB * JWB *Invib); Bodya.velocity.x+ = Jvxa * INVMA *Lambda; Bodya.velocity.y+ = Jvya * INVMA *Lambda; Bodya.angularvelocity+ = JWA * InvIA *Lambda; Bodyb.velocity.x+ = JVXB * INVMB *Lambda; Bodyb.velocity.y+ = Jvyb * INVMB *Lambda; Bodyb.angularvelocity+ = JWB * Invib *Lambda; } } ); returnP.constraints =C;}) (PNGX);
An unreliable 2D physics engine (5)-Constraints (Constraints)