Billiard game core Algorithms and AI (1)

Source: Internet
Author: User

Objective:
08, wrote a billiard game, using Java, but the code is really used in the legendary artifact Notepad written (you believe it? In fact, written in GVIM , ^_^), many classes are written in the same Java file. It can be seen that the Java level is really not good, when the move, or the same not well.
This is the csdn. Download Link: Java (billiards game), the implementation is relatively simple. Later wrote a version, more powerful than this, unfortunately the source is missing.
The effect is displayed in the following:
  
This article wants to talk about the implementation of the core algorithm in billiard game, and the design skill of game AI. Of course, I also have a small wish, hoping to achieve a HTML5 version of the billiards game.

Basic Physics Knowledge:
Friction resistance
It satisfies Newton's second law:
f = m * A
Equation of velocity and acceleration:
VT = V0 + A * t
The ground friction is opposite to the moving object, which hinders the movement of the object forward.
Conservation of momentum
Suppose that the object a mass is M1, the velocity is V1, the object b mass is M2, the speed is V2, the collision speed is V1 ', V2 ' respectively.
The momentum is satisfied:
M1 * v1 + m2 * v2 = m1 * v1 ' + m2 * v2 '
  

Collision Type and conservation of energy
1). Full Elastic collision
No loss of kinetic energy, the following formula is satisfied:
M1 * v1^2 + m2 * v2^2 = * * M1 * v1 ' ^2 + * m2 * v2 ' ^2
Note: The kinetic energy of the object is balanced and there is no other energy conversion.
Together with the previous momentum, we can get further:
V1 ' = [(m1-m2) * v1 + 2 * m2 * v2]/(m1 + m2)
V2 ' = [(M2-M1) * v2 + 2 * M1 * V1]/(m1 + m2)
2). completely inelastic collision
There is the conversion of other energies, and the kinetic energy is not conserved.
And at this time two objects adhesion, the speed of the same, that is v1 ' =v2 ', at this time the greatest loss of kinetic energy.
3). Elastic Collision
Between a fully elastic collision and a completely non-elastic collision. Kinetic energy has a loss.

Physical Model:
Billiards game, the core is itsabstraction of the physical modelAndexecution process of collision algorithmThe.
Since it is the 2D version of billiards game, we make a simplification of the physical model, the direction of the ball movement must cross the center of the ball.
Abstract each billiard ball into a circle (x, y, radius), and the billiard table border is abstracted as a line segment ((x1, y1), (x2, y2)).
Collision Detection
1).detecting ball and ball collisions
We assume the ball a (x1, y1, R), Ball B (x2, y2, R). The condition is met:
(x1-x2) ^ 2 + (Y1-y2) ^ 2 <= (2*r) ^ 2
Collision occurs, or no collision occurs
2).detects the collision between the ball and the billiard table frame
relatively simple. The vertical distance from the sphere to the border can be, if less than equals the collision occurs, if greater than the absence.
Collision Response
1).ball-to-ball collision response
  
Momentum is a vector, which is conserved in two orthogonal directions. We choosethe line of the center of the two balls is the X axis,the y-axis perpendicular to the center line. As described.
The conservation of momentum is met on the x-axis:
M1 * vx + m2 * UX = m1 * vx ' + m2 * ux ';
It is assumed that the two-ball collision is a completely elastic collision, the two-ball masses are equal m1=m2, according to the conclusion of the basic physics knowledge.
VX ' = [(m1-m2) * vx + 2 * m2 * UX]/(m1 + m2) = UX;
UX ' = [(M2-M1) * UX + 2 * M1 * VX]/(m1 + m2) = VX;
In the x-axis direction, the two balls exchange speed, while in the Y direction, the two ball points speed unchanged.
Vy ' = Vy;
Uy ' = Uy;
   the velocity formula after the final collision is:
V ' = Vx ' + Vy ' = Ux + Vy;
U ' = Ux ' + Uy ' = Vx + Uy;
2).collision response of ball to frame
Consider the billiard frame as mass infinity, then simply put the ball in the direction of the vertical border.
  
Assume that the collision plane is the x-axis
VX ' = VX;
Vy ' =-vy;
   the final velocity formula is:
V ' = Vx ' + Vy ' = Vx-vy;

Collision Execution Algorithm:
The main loop of the game often follows the following code structure:

    while (true) {        game.update (time_interval);        Game.render ();    }

This time interval (time_interval) is determined by the game's FPS. Take 24 frames as an example and refresh every 40 milliseconds.
For billiards itself, if the time_interval is an update cycle, the sphere of motion satisfies:
Vt = V0 + A * t
The operating distance is:
S = V0 * t + * A * t^2.
It then detects if the sphere has collided and then reacts with the collision. There seems to be no problem.
However, when the initial velocity of the ball is very fast, it is possible to have a crossing phenomenon in the time_interval.
As shown in the phenomenon:
  
The purple ball in the T2 moment, and the blue Ball detects a collision, but in fact, in the purple ball at some point between the t1~t2 and the blue Ball has collided.
In order to solve this problem, in the specific algorithm, we need to introduce finer time Shard Slice, which is simulated in the specific update.
Update function for the entire billiard scene:

void Update (Time_interval) {while time_interval > 0://Collision Detection if Detectioncollide (Time_interval, Least_time, Ball_pairs )://Game Update least_timebilliards.update (least_time)//collision response to two ball collisions collidereaction (ball_pairs=> (balls, Other))//Time _interval reduced Least_timetime_interval-= least_timeelse://game update least_timebilliards.update (time_interval) time_ Interval = 0}

Note: Collision response, according to the physical model of the story.
and the specific collision detection algorithm is:

/* @brief in Time_interval time, return the first collision ball or billiard edge, and point in time */bool detectioncollide (Time_interval, Least_time, ball_pairs) {res = False;least_time = Time_interval;foreach Ball in Billiards:foreach Otherball in billiards://find the distance between the two balls s = distance Erball)//with a ball as the reference coordinate system, then make a ball velocity vector into U ' =u-v//in the center of the line as x-axis UX (relative) = UX (Other ball)-Vx//If the direction of the two balls away, then directly ignore if Ux (rel ative) < 0:continue//a certain direction so that the two balls close, you can expect the collision of the expected time point A ' = 2 * a;//acceleration of the original twice times//take both the minimum point in time Delta_time = min (time_interval, Ux (rel ative)/AX ')//The expected distance is less than two ball distances, then collisions will not occur in Time_interval if the ax ' * delta_time ^ 2 + Ux (relative) * Delta_time < s-2*r: continue//solution of a two-time equation, using a binary search approximation to solve res_time <= slove (* Ax ' * x ^ 2 + Ux (relative) * x = S-2 * r) if Res_time < least _time:ball_pairs <= (Ball, otherball) least_time = Res_timeres = Trueforeach Wall in billiards:s = distance (ball, wall) Set the direction perpendicular to the plane to the x-axis if Vx < 0:continue//take the minimum point of time Delta_time = min (time_interval, Vx/ax)//expected distance is less than two ball distance, then in Time_interval will not Collision If Delta_time * Ax *^ 2 + vx * Delta_time < s-r:continue//solution of a two-time equation, using a binary search approximation to solve res_time <= slove (A * A * x ^ 2 + vx * x = s-r) if re S_time < Least_time:ball_pairs <= (ball, walll) least_time = Res_timeres = Truereturn Res}

Note: for a two-time equation, it can also be divided into 1000 fine-grained time slices, and then calculate the approximation solution .
Billiard Ball Simulation Collision algorithm process, is generally described above.
The most complicated moment of calculation is actually the kick -off and the time to break up a pile of balls.

Summarize:
This article refers to "Nehe's OpenGL Chinese tutorial 30th lesson Collision Detection and model movement". Of course, the realization of billiards game, may not really need the algorithm, many developers directly use box2d can be perfect and easy to implement. Refer to "Implementation using Cocos2d-x box2d". Follow-up articles that want to tell how AI is designed and implemented under billiard games. Hope to work together.

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 (1)

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.