In game development, it is inevitable that the actual situation needs to be simulated. Generally, the physical world is simulated.
For example, object collision, vehicle forward, and object whereabouts. Some people may think that they have achieved some effects and have not seen any physical knowledge.
In my opinion, introducing a physical engine is not necessary, but it can make the game more realistic. Box2d is preferred for physical engines, but it is written in C ++, which is inconvenient to use directly.
There are generally three good options for using the physical engine in android game development:
1. jbox2d
The Java version of box2d is a little slow. However, if you are not familiar with C ++, you can refer to the principle.
2. Havok
This is not much to say. I have never used it.
3. libgdx
Libgdx uses JNI to encapsulate box2d, which provides good speed and ease of use.
Let's take a look at an example. If you have never touched box2d before, you 'd better look for information first.
CodeAs follows:
Package Com. cnblogs. htynkn. listener;
Import Com. badlogic. GDX. applicationlistener;
Import Com. badlogic. GDX. GDX;
Import Com. badlogic. GDX. Graphics. gl10;
Import Com. badlogic. GDX. Graphics. orthographiccamera;
Import Com. badlogic. GDX. Math. vector2;
Import Com. badlogic. GDX. Physics. box2d. Body;
Import Com. badlogic. GDX. Physics. box2d. bodydef;
Import Com. badlogic. GDX. Physics. box2d. box2ddebugrenderer;
Import Com. badlogic. GDX. Physics. box2d. circleshape;
Import Com. badlogic. GDX. Physics. box2d. World;
Import Com. badlogic. GDX. Physics. box2d. bodydef. bodytype;
Public Class Demogame Implements Applicationlistener {
Protected Orthographiccamera camera;
Protected Box2ddebugrenderer Renderer; // Test painter
Private World;
@ Override
Public Void Create (){
Camera = New Orthographiccamera (48, 32 );
Camera. position. Set (0, 15, 0 );
Renderer = New Box2ddebugrenderer ();
World = New World ( New Vector2 (0,-9.8f ), True );// General standard gravity field
Bodydef BD = New Bodydef (); // Declare object definition
BD. position. Set (2f, 2f );
BD. type = bodytype. dynamicbody;
Body B = World. createbody (BD ); // Create an object through World
Circleshape c = New Circleshape (); // Create a shape (circle)
C. setradius (1f ); // Set radius
B. createfixture (C, 1f ); // Assign shape and density to objects
}
@ Override
Public Void Dispose (){
Renderer. Dispose ();
World. Dispose ();
Renderer =Null ;
World = Null ;
}
@ Override
Public Void Pause (){
// Todo auto-generated method stub
}
@ Override
Public Void Render (){
World. Step (GDX. App. getgraphics (). getdeltatime (), 3, 3 );
Gl10 GL = GDX. App. getgraphics (). getgl10 ();
Gl. glclear (gl10.gl _ color_buffer_bit );
Camera. Update ();
Camera. Apply (GL );
Renderer. Render (World, camera. Combined );
}
@ Override
Public Void Resize ( Int Width, Int Height ){
// Todo auto-generated method stub
}
@ Override
Public Void Resume (){
// Todo auto-generated method stub
}
}
We first created a world with a gravity of 9.8 and a downward direction.
Then, an object is declared as a dynamic object (that is, a dynamic object). If a circle is created, the shape is assigned to the object.
And then simulate the call.
World. Step (GDX. App. getgraphics (). getdeltatime (), 3, 3 );
The effect is as follows:
In fact, there is no big difference in usage. Libgdx does not have the class AABB. To use this boundary, use
World. queryaabb (callback, lowerx, Lowery, upperx, uppery)
That's all.
There are actually many shapes, such as circles, squares, rectangles, and polygon. For details, refer to the Help File of box2d.
Some people think it is complicated to use box2d and it is difficult to associate it with existing things. In fact, I think box2d is very convenient to use, especially it is very convenient to connect with stage.
Use
Body. setuserdata (userdata)
Assign the actor name to the body, and then use
Stage. findactor (name)
Change the actor location and shape.
Conclusion:
Here are a few questions:
1. box2d is a physical engine, not a game engine. It simulates the physical world and rules, but your images are all processed by yourself.
2. box2d is measured in meters and seconds. Note the unit conversion. We do not recommend that you use 1 pixel = 1 meter directly. In this case, some things will be very strange. The ratio depends on the situation. You can use 30, 50.
3. The stage coordinates are in the lower left corner, and box2d is generally the center of gravity. Pay attention to the changes.
4. You can use box2ddebugrenderer during debugging to clearly view the boundary and center of gravity.