Libgdx Box2D Practice --- release the ball (3: Combination of common body and genie Rules)

Source: Internet
Author: User
Tags libgdx

Libgdx Box2D Practice --- release the ball (3: Combination of common body and genie Rules)

Today we will introduce how the rule body works with images. In the previous article, I introduced the basic knowledge of box2D. If you are careful, you will search for simple online demos. I will not write those. What should I do if I use an image to represent my body instead of a simple line?

Next, I will take the small ball object generation in my project as an example to introduce how to combine the genie and body:

The following is an important note for creating a ball game:

Public class Ball {private static final float BALL_RADIUS = 0.15f; // Ball radius private final Random rand = new Random (); public Body ballModels; // Ball bodypublic Sprite ballSprite; // genie public int type; // Ball type public Ball (World world, int type) {BodyDef ballBodyDef = new BodyDef (); // generates a defballBodyDef. type = BodyType. dynamicBody; // defines a ball as an uncontrolled Dynamic Object CircleShape shape = new CircleShape (); // defines the shape of the ball. setRadius (BALL_RADIUS); FixtureDef fd = new FixtureDef (); // generate a Fixturefd. density = 1; // density fd. friction = 0f; // friction fd. restitution = 0.5f; // elastic fd. shape = shape; ballModels = GameScreen. world. createBody (ballBodyDef); // load def ballModels for the body. createFixture (fd); // creates a fixture shape for the body. dispose (); reset (type);} public void reset (int type) {this. type = type; float tx = rand. nextFloat () * 1.0f-0.4f; float ty = GameScreen. camera. position. y + GameScreen. camera. viewportHeight/2 + BALL_RADIUS; float angle = rand. nextFloat () * MathUtils. PI * 2; Vector2 vec = new Vector2 (); ballModels. setActive (true); ballModels. setAwake (true); ballModels. setLinearVelocity (vec. set (0, 0); ballModels. setAngularVelocity (0); ballModels. setTransform (vec. set (0.3f, 10), angle); // the initial position of the ball creation. ballSprite = new Sprite (GameCenter. bils [type]); // generate an genie ballSprite. setSize (BALL_RADIUS * 2, BALL_RADIUS * 2); ballSprite. setOrigin (BALL_RADIUS, BALL_RADIUS );}}

Then we can call Ball = new ball (world, 0) in the game interface to generate a Ball, but you will find that when you write it, it will not produce a Ball. Why? It is not because it is invisible, so we need to put a "coat" on it so that we can see it, that is, drawing the genie, and let the genie synchronize with the body of the ball. That's not enough!

Here, I wrote a function to synchronize the body and the Genie. When we put this function in the Render, we kept refreshing:

public ArrayList
 
   balls = new ArrayList
  
   ();public ArrayList
   
     ballSprites = new ArrayList
    
     ();
    
   
  
 
Public void ballsRender () {for (int I = 0; I <bils. size (); I ++) {Vector2 ballPos = bils. get (I ). ballModels. getPosition (); // first obtain the body position ballSprites. get (I ). setPosition (ballPos. x-ballSprites. get (I ). getWidth ()/2, ballPos. y-ballSprites. get (I ). getHeight ()/2); // update the sprite location ballSprites here. get (I ). setRotation (bils. get (I ). ballModels. getAngle () * MathUtils. radiansToDegrees); // The Rotation Angle of ballSprites. get (I ). draw (batch); // drawing genie }}

I want to see how you can write and try it by yourself. There is no need to set so many parameters to see if they can be implemented. If you have any questions, leave a message below and I will reply soon!

Well, the above is the implementation of simple and regular graphics. Currently, box2D does not support many shapes, but some simple and commonly used shapes. You can go to the official website to carefully study those demos. So the question is, how can we combine the body and the genie if we encounter irregular graphics? I will reveal the answer in the next blog!


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.