Box2D custom gravity, box2d gravity
You need to add a custom attribute to the rigid body: m_customGravity. In this way, you can dynamically modify the custom gravity of each rigid body, search for the box2d source code in about 5486 lines, and add a red line of code.
B2Island. prototype. solve = function (step, gravity, allowSleep) {var I = 0; var j = 0; var B; var joint; for (I = 0; I <this. m_bodyCount; ++ I) {B = this. m_bodies [I]; if (B. getType ()! = B2Body. b2_dynamicBody) continue;Gravity= B. m_customGravity |Gravity;B. m_linearVelocity.x + = step. dt * (gravity. x + B. m_invMass * B. m_force.x); B. m_linearVelocity.y + = step. dt * (gravity. y + B. m_invMass * B. m_force.y); B. m_angularVelocity + = step. dt * B. m_invI * B. m_torque; B. m_linearVelocity.Multiply (b2Math. clamp (1.0-step. dt * B. m_linearDamping, 0.0, 1.0); B. m_angularVelocity * = b2Math. clamp (1.0-step. dt * B. m_angularDamping, 0.0, 1.0 );}.............. code omitted ...................................... ........................................ .............};
Usage:
// Create a circular Rigid Body
Var circle = new Circle ({x: mousePoint. x, y: mousePoint. y, r: 20, type: SC. dynamicBody });
// Add the m_customGravity attribute to the rigid body and assign the vertical gravity value to 1.
Circle. b2Obj. body. m_customGravity = new xengine. EasyB2D. b2Vec2 (0, 1 );