Billiard game core Algorithms and AI (2)

Source: Internet
Author: User


Objective:
Recently studied the box2dweb, feel oneself to write HTML5 edition billiards game time ripe already. This is also a round of their own a desire, a dream.
To undertake this sequence of related blog posts:
• Billiard game core Algorithms and AI (1)
Combine HTML5 's learning notes at the same time:
Box2dweb Study Notes--sample explanation
This article specifically explains the box2d model abstraction of billiards game and gives a preliminary version.

Demonstrate:
The prototype of the billiard game is as follows:
  
The billiard game, adapted from the Box2dweb demo program, you can drag the mouse to move the ball.
Download link for code: Http://pan.baidu.com/s/1sjzCwqD

Analysis:
Let's make a simple physical abstraction of the billiard game and then "discovering", dissecting each component with box2d.
  
, it is an abstraction of 6 ball bags and 6 frames, the ball bag is the goal to fall into the ball, the border is limited to the range of billiards activities.
Border Abstraction
Billiard Ball frame is relatively simple, it can be regarded as static object. Its physical shape is an edge.

Set to static object type Wallbodydef.type = b2body.b2_staticbody;//takes polygon shape, then Setasedge is set to edge wallfixdef.shape = new B2polygonshape; WallFixDef.shape.SetAsEdge (New B2VEC2 (x1, y1), New b2vec2 (x2, y2));

Note: The border becomes box2d object or simple.
Ball Bag Abstraction
The bag itself is also a static object, but different from the border, its box2d abstraction, a bit more complex and tricky.
1). Sensing Settings
The ball bag area should be the sensing area, the ball can enter the area, but does not have a collision reaction with it.
This can be achieved by setting the issensor property of the Customizer (Fixture) to true, as shown in the following code:

var holefixturedef = new B2fixturedef;holefixturedef.shape = new B2circleshape (0.5); holefixturedef.issensor = true;

Note: The feature is capablePerceived collisionsButNo collision reaction occurs
2).effective area transform of falling bag
When the ball bag and ball area intersect, it does not mean that the ball is in the hole. As shown in the following:
  
Note: The red ball coincides with the area of the bag, but the center of gravity does not fall within the range of the ball bag.
In order to perfectly solve the logic of the ball into the hole, we have two ways to solve.
One way of thinking is:calculate the distance from the resulting collision contact Object B2contact, if the distance between the two centers is less than the radius of the ball bag, then the hole is counted.
Another way of thinking is to do a trick skill,construct a radius = Ball bag radius-radius of the ball, the center is still the circle of the centre of the ball bag, and replaces the box2d physical model as a ball bag. If the circle intersects with the ball, it can be assumed that the center of gravity falls into the ball bag area. This can be avoided by the former calculation.
  
Note: The green inner circle is the core circle of the constructed ball bag, and the outer circle is the circle of the physical representation. The scene intersects the ball and the bag, but the center of gravity and the inner circle do not intersect, i.e. the center of gravity does not fall into the ball bag area.
Surround the 3/4 circle of the bag itself, then usePolygon to approximate the simulation(The example uses a 16-side shape), which is also to prevent the ball out of the effective area (in fact, this can be ignored).
Sphere Placement
As we all know, billiards simulation, the most difficult is often the time of the kickoff. A pile of balls huddled together, and every moment, there were many balls touching each other.
The stacking of the sphere is actually a skill, and the ball that is placed does not need to be next to each other.leave some space as appropriate.. As shown below:
  
Overall simulation
Due to the vertical view of the billiard table, the gravity direction is pointing inward. When creating a World object, you can simply setgravity is zero vector.

var world = new B2world (new b2vec2 (0, 0), true)

While the billiard table itself friction resistance, due to billiards game in the Box2 world, there is no related physical objects, so we need to set the ball line speed damping to simulate the billiard table friction resistance.

ballbodydef.lineardamping = 0.25;

Final Billiard Game overall box2d physical model, to convert to such as:

  goal handling
Ball goal bag, need to disappear, can be understood as the ball from the physical world of box2d elimination.
For the collision response, BOX2D provides two ways to deal with it.
1). Register Contactlistener Mode
2). Traverse the ContactList list
The sample code takes the second way, for the following reasons:
1). Contactlistener's callback is in the step simulation process, Box2D explicitly specifies that the physical property is not allowed to be modified during step simulation.
2). Because the number of objects in a billiard game is not many, it is acceptable to traverse the ContactList list for its performance.

        /* Clear the ball into the bag */        var contactlist = world. Getcontactlist ();        for (var contact = contactlist; GetNext ()) {            if (!contact). Istouching ()) {/   * Contact only represents AABB coincident but does not represent body collisions */                continue;            }            var B1 = contact. Getfixturea (). GetBody ();            var b2 = contact. Getfixtureb (). GetBody ();            if (B1. Getuserdata () && B2. Getuserdata ()) {                if (B1). Getuserdata () = = = Ball_type.bg_hole_type && b2. Getuserdata () = = = Ball_type.bg_ball_type) {World                    . Destroybody (B2);                }                if (B2. Getuserdata () = = = Ball_type.bg_hole_type && B1. Getuserdata () = = = Ball_type.bg_ball_type) {World                    . Destroybody (B1);}}}        

Note: The processing code is in the world. After the step is called.

Summarize:
The demo graphic here is rendered with the help of Box2D's drawdebug. The next step is to replace it with beautiful material and perfect the game rules for billiards. Although the level is limited, but feel a solid step forward, this feeling is very good.

Written at the end:
  
If you think this article is helpful to you, please give it a little reward. In fact, I would like to try to see if blogging can bring me a little bit of revenue. No matter how much, is a kind of sincere affirmation to the landlord.

Billiard game core Algorithms and AI (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.