Box2d tutorial 6-collision intensity

Source: Internet
Author: User

Before reading this tutorial, please read: box2d tutorial 5-Collision Detection

Box2d tutorial 1-create a collision world

Box2d tutorial 2-mouse Interaction

Box2d tutorial 3-Rigid Body binding

Box2d tutorial 4-complex appearance of complex rigid bodies

When two objects collide, apart from checking whether they are in conflict, we often need to know how powerful the collision is. For example, Angry Birds decide whether to eliminate obstacles based on the collision intensity. This tutorial explains how to obtain the collision intensity.

The collision intensity is physically expressed by an impulse (impluse). In classical mechanics, the impulse is equal to the variation of the momentum of the object.
Impulse Formula
I = ft (unit: N * s)
If constant force F is applied to objects with a mass of m and a static state, what will happen after the time t? According to Ft = MAT = mV, the greater the product ft of force and time, the greater the velocity V is obtained for a static object. The smaller the FT, the smaller the object speed.
Impulse is vector
The direction of impulse is determined by the direction of force. If the direction of the Force remains unchanged during the time when the force is applied, the direction of the force is the direction of the impulse.
Relationship Between impulse and momentum
The product of the mass and velocity of a moving object is momentum, P = mV, and the international unit is kilograms per second.
Under the action of constant force F, the velocity of an object with a mass of M changes from V to V 'within the time t ′. According to Newton's second law, F = ma represents the force of external force on the object. The multiplication time t, Ft = MAT = MV '-mv on both sides of the equation, so I = p'-P

To sum up, the speed obtained after a collision of an object of a certain quality can be used to measure the collision intensity. It depends on the product of the collision force and time and the impulse. Therefore, in box2d, we need to determine the collision intensity and obtain the impulse of the collision. box2d provides you with a convenient way to obtain the collision intensity. The specific method to obtain data is as follows.

In tutorial 5, we have explained how to perform collision detection. Create a custom customcontactlistener and overwrite begincontact and endcontact to get the collision start and end. To obtain the collision impulse, We need to overwrite another method postsolve (contact: b2contact, impulse: b2contactimpulse). We can see that this method has two parameters.

B2contact: collision object. You can obtain two collision objects.
Impluse: collision impulse and the parameters we need to measure the collision intensity

Impluse impulse is an array with two elements: normalimpulses and tangentimpulses, which are vector vectors.

Impulse produced by normalimpulses colliding with common force
Tangentimpulses simulates the impulse produced by the friction in the tangent direction (the actual trace is always 0, which may be the cause of my Rigid Body)

In this tutorial, overwrite the postsolve method to throw a collision event and carry an impulse array as a parameter.ProgramListen on this event and output the impulse value.

Customcontactlistener. As override postsolve Method

 1 Override Public   Function Postsolve (contact: b2contact, impluse: b2contactimpulse): void
2 {
3 VaR Collisionevent: collisionevent = new collisionevent (collisionevent. post_solve );
4 If (Contact. getfixturea (). getbody (). getuserdata ()! = Null & Contact. getfixtureb (). getbody (). getuserdata ()! = Null )
5 {
6 Collisionevent. impluse = impluse;
7 Eventdispatcher. dispatchevent (collisionevent );
8 }
9 }

Collision event collisionevent.

 1 Package comingx. Jingle. Events
2 {
3 Import Box2d. Dynamics. b2contactimpulse;
4
5 Import Flash. Events. event;
6
7 Public Class Collisionevent Extends Event
8 {
9 Public Static Const collision_start: String = "Collision_start ";
10 Public Static Const collision_end: String = "Collision_end ";
11 Public Static Const post_solve: String = "Post_solve ";
12
13 Public VaR Bodyaname: String = "";
14 Public VaR Bodybname: String = "";
15 // Collision impulse Array
16 Public VaR Impluse: b2contactimpulse;
17
18
19 Public Function Collisionevent (type: String , Bubbles: Boolean = False , Cancelable: Boolean = False )
20 {
21 Super (Type, Bubbles, cancelable );
22 }
23 }
24 }

Listening output in the main program

 1           Private   Function Initcontactlistener (): void
2 {
3 VaR Customcontactlistener: customcontactlistener = new customcontactlistener ();
4 Customcontactlistener. eventdispatcher. addeventlistener (collisionevent. collision_start, handlecollisionstart );
5 Customcontactlistener. eventdispatcher. addeventlistener (collisionevent. collision_end, handlecollisionend );
6 // Collision processing end listening
7 Customcontactlistener. eventdispatcher. addeventlistener (collisionevent. post_solve, handlepostsolve );
8 World. setcontactlistener (customcontactlistener );
9 }
10
11 /* *
12 * Output collision impulse
13 * @ Param EVT collision event
14 *
15 */
16 Private Function Handlepostsolve (EVT: collisionevent): void
17 {
18 Console. addinfo ("collision impulse:" + EVT. impluse. normalimpulses [0]. tostring ());
19 If (EVT. impluse. normalimpulses [0]> 30)
20 {
21 // Your logic, such as making obstacles burst like angry birds
22 }
23 }

Source code download

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.