Lame note 34-cocos2d-x-3.2 Box2d physical engine World class introduction, cocos2dbox2d

Source: Internet
Author: User

Lame note 34-cocos2d-x-3.2 Box2d physical engine World class introduction, cocos2dbox2d

Reprinted indicated Source: http://write.blog.csdn.net/postedit/38711365

Previous review

This famous saying goes:If we transfer the energy of love to others, we will become a pipeline to absorb the holy energy from heaven. That mysterious experience is something we can all taste![James rednon]

In the previous article, we learned the Contact class in the dynamics module of the Box2d physical engine, which contains all the Contact types. The collision of objects is specific in this class.

Continue to learn about the classes in the dynamics module. This time we will learn about the World class.

 

Introduction

The World class (which is actually the b2World class. As said before, the classes in Box2d start with b2) contains the Body and Joint. It manages all the simulations that allow asynchronous queries.

Creating a World is very simple. You only need a Gravity Vector and a boolean type (indicating whether the Body can sleep ).

The World class is used to drive simulation. Define a time step, speed, and position iterations.

We need a fixed time step, but a large time step can improve the frame rate. The time step is preferably less than 1/30 seconds. If it is 1/60 seconds, it will get better results.

The number of iterations controls the number of resolution scans in contact. Do not use too many iterations. 10 iterations of 60Hz are better than 20 iterations of 30Hz. Finally, call the b2World: CleareForces () function to delete the external force.

Explore the world

World is a container of the body, contact, and Joint classes.

You can easily obtain all the bodies in the World. As follows:

For (b2Body * B = myWorld-> GetBodyList (); B = B-> GetNext ())

{

GameActor * myActor = (GameActor *) B-> GetUserData ();

If (myActor-> IsDead ())

{

MyWorld-> DestroyBody (B); // ERROR: now GetNext returns garbage.

}}

However, the above Code has a problem. If one of the bodies is destroyed, the GetNext () function will get a garbage. The solution is to copy the pointer before destroying the Body.

 

Query

Sometimes you need to know all shapes in a region. The World class has a fast Log function used to use the broad-phase data structure.

It usually provides the world coordinates of a Body and the b2QueryCallback function. It is called when Fixture overwrites the Body.

Laser

You can use laser to detect and implement this function. Use a callback function and provide the start and end points to implement laser. When a Fixture encounters a ray, World will call your class. The callback function provides Fixture, intersection, unit vector, and distance. Note that due to fractional errors, laser rays can zoom in through small gaps. If not accepted by the application, they can be scaled up properly.

 

Force and impulse

The force, torque, and impulse can be applied to the Body. A starting point is required when the force and impulse are applied. Applying force or impulse will wake up the Body.

 

Coordinate Conversion

The Body class has functions that allow us to convert coordinates locally and in the world space.

 

Traversal

We can traverse all Fixture on the Body. Likewise, we can traverse all the Joint and contacts. However, it should be noted that the Contact does not always exist in the previous step.

 

Summary

I learned the World class in the dynamcis module this time. If you have any mistakes, please be sure to make your friends correct.


How to Use the box2d physical engine in vc ++ 60?

It is estimated that you do not include box2d. X

An error occurs because the compiler cannot find the box2d. h or box2d. cpp file.

You need to find this file and put it in the Vc installation directory.

Or you can put the function of this file in a file named box2d. h or box2d. cpp.

Then add the project you wrote and compile it.

You should download and install the box2d for VC package.

Flash as30 BOX2D physical Engine

McBall. x + = mcBall. vx;
McBall. vx + =. 03;

McBall itself has a ball with horizontal speed, vertical speed attributes, and acceleration. There is a loop in the document class to increase their speed based on their respective acceleration.
Pay attention to the problem that the acceleration and speed will change in reverse direction after the boundary. the collision between the two balls should be calculated using the momentum conservation theorem of middle school.
If you use the box2D physical engine, the effect will be better.


Related Article

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.