Creator Physics engine

Source: Internet
Author: User
Tags abs mul

1 Applyforce/tocenter (). How this force is calculated (Newton's second Law F=ma)
2. Linearvelocity () is pixel/meter (see source)
3 Why is the speed up to frame four?

Http://docs.cocos.com/creator/api/zh/classes/RigidBody.html#applyforcetocenter

The official website documents are so introduced Applyforcetocenter (),

Applyforcetocenter

Exerts a force on the centroid of a rigid body.
Meta
Description
Defined in
https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/ccrigidbody.js:683
Parameter list

Force Vec2 the World force vector.
Wake Boolean also wake up the body.

In my view, the first argument force is a vector of forces, such as CC.V2 (0, 100), which is a force that exerts a vertical upward of 100 newton.

Turn to the source of Applyforcetocenter ()
/**! #en Apply A to the center of mass. ! #zh exert a force on the centroid of the rigid body. @method applyforcetocenter @param {VEC2} force-the World force vector.

@param {Boolean} wake-also wake up the body.
*/
Applyforcetocenter:function (Force, Wake) {
if (this._b2body) {
Tempb2vec21.set (Force.x/ptm_ratio, force.y/ptm_ratio);
This._b2body.applyforcetocenter (Tempb2vec21, Wake);
}
},

Tempb2vec21.set (Force.x/ptm_ratio, force.y/ptm_ratio);
This line of code is "Force.x/ptm_ratio", divided by Ptm_ratio, and the value of Ptm_ratio is 32.

Http://docs.cocos.com/creator/manual/zh/physics/physics/physics-manager.html

Open the link above, there is an introduction.
Conversion of physical units to pixel units:
Box2D uses the M-kg-second (MDS) unit of units, and the box2d is best performed in such a unit of units. But we generally use the image as a length unit in 2D game operations, so we need a ratio to convert physical units to pixel units. In general, we set this ratio to 32, which can be passed CC. Physicsmanager.ptm_ratio gets, and this value is read-only. Usually the user does not need to care about this value, the physical system will automatically convert the physical units and pixel units, the user access and set up in the 2d game development is familiar with the pixel units.
Set Physical Gravity
Gravity is a very important aspect of physical performance, and most physics games use gravity as a physical feature. The default gravitational acceleration is (0,-320) pixels per second ^2, according to the conversion rules described above, i.e. (0,-10) m/s ^2.

    Thus, the pixel units are converted to physical units, divided by Ptm_ratio, and the physical units are converted to pixel units, multiplied by ptm_ratio. The unit of the
    coordinate system in box2d is M (m)

    Therefore, the force of the first parameter of the Applyforcetocenter () method is calculated as "Pixels per second ^2".

Http://alexq.farbox.com/post/box2d-ru-men-jian-yao
In this article, we mentioned:

Gravity, simply speaking, is the force that makes the object fall naturally, and box2d uses the Vector2 vector class to simulate the force. VECTOR2 is the abbreviation for "box2d vector2d", which represents a vector in a two-dimensional space, with only X and y two in the constructor of the Vector2 class
Parameters that represent the components in the x-axis and y-axis directions, respectively.

New Vector2 (x, y)
For C + +, the gravity direction reference is as follows:

Because gravity is vertically downward, there is no force in the horizontal direction, so X is set to 0, which is not difficult to understand. As for the parameter value of Y-9.8, in fact, here Y refers to the gravitational acceleration, which we learned in high school, in the absence of any resistance effect, the theory of the gravitational acceleration is 9.8m/s2 (meters per square second). The unit of the coordinate system in box2d is M (m) This is important, it's important.

Gravity is a very important aspect of physical performance, and most physics games use gravity as a physical feature. The default gravitational acceleration is (0,-320) pixels per second ^2, according to the conversion rules of physical units and pixel units, i.e. (0,-10) m/s ^2.

OnLoad () {
var Physicsmanager = Cc.director.getPhysicsManager ();
Physicsmanager.enabled = true;
Physicsmanager.enabledaccumulator = true;
this.gravity = physicsmanager.gravity;
Cc.log ("Gravity (%f,%f)", this.gravity.x, THIS.GRAVITY.Y);
Cc.director.getCollisionManager (). Enabled = TRUE;
This.enablephysicsdebugdraw ();

This.rigidbody = This.node.getComponent (cc. Rigidbody);

},

Start () {
var m = This.rigidbody.getMass ();
var a = Cc.v2 (this.gravity.x, Math.Abs (THIS.GRAVITY.Y) *10);
var force = A.mul (M);
This.rigidbody.applyForceToCenter (Force, true);

Cc.log ("A (%f,%f) (%f,%f)", a.x, A.y, A.X/32, A.Y/32);
Cc.log ("Stone mass =%f", m);

Cc.log ("Force (%f,%f)", force.x, FORCE.Y);

},

Update (DT) {
var speed = this.rigidbody.linearVelocity;
Cc.log ("Dt=%f speed (%f,%f)", DT, speed.x, SPEED.Y);
Cc.log ("Position (%f,%f)", this.node.x, THIS.NODE.Y);
},

The log prints that the mass of the stone is 5.46875kg, then the stone is subjected to 5.46875 x 10 = 54.6875 Newton.

var a = Cc.v2 (this.gravity.x, Math.Abs (THIS.GRAVITY.Y) *10); Acceleration (0, 3200)
var force = A.mul (M);

Give an acceleration of a (0, 3200), where the unit is pixels per second ^2, converted to a physical unit after 100 m/s ^2, then subjected to a vertical upward of 546.875 Newton's force, after cost-effective, the acceleration of the stone is 90 m/s ^2

Speed (0, 48) in the log is measured in pixels per second, and is converted to physical units at 1.5 m/s. Calculated by the formula "V1 = V0 + at" with a time of approximately 0.016666 seconds

1 time is about 0.016666 seconds. Can roll out the theory is right
2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.