Wiki source code of Ball Physics
Last modified by Soeren Domroes on 2025/01/30 12:18
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | The **ball physics model**'s main objective is to provide a point in time to the controller, when the ball is predicted to be hittable, in order to not let the ball fall into the drain. | ||
| 2 | The code for what is described in this section can be found in the files "physics.cpp" and "physics.h". | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | {{toc/}} | ||
| 7 | |||
| 8 | == Ball trajectory == | ||
| 9 | |||
| 10 | ---- | ||
| 11 | |||
| 12 | The trajectory of a ball can be modeled to be parabolic. From two point //p// and //p_old// with corresponding timestamps, the velocity //v// is computed. | ||
| 13 | For a future point in time //t//, with //t=0 //being the timestamp of// p//, we can then approximately predict the corresponding ball position using the following formulas. | ||
| 14 | |||
| 15 | [[image:attach:Bildschirmfoto von 2020-03-07 12-21-22.png]] | ||
| 16 | |||
| 17 | The incline of the playfield causes a vertical acceleration, which is modeled via a gravitational pull with the factor //g = sin(6°) * 9.81 m/s²//. | ||
| 18 | The value of 6° is the incline of the playfield measured on the physical device. | ||
| 19 | |||
| 20 | == Prediction == | ||
| 21 | |||
| 22 | ---- | ||
| 23 | |||
| 24 | The controller calls the function //full_prediction// passing it two subsequent ball positions with timestamps. From these a ball trajectory is inferred as described above. | ||
| 25 | Along the trajectory future times are stepwise tested for whether the corresponding predicted position is in the flipper range. | ||
| 26 | Once a position inside the flipper range is found the search is continued both forwards and backwards in smaller steps in order to find a good enough estimate of the first and last possible time to hit the ball. | ||
| 27 | If the trajectory does not intersect the flipper range within a certain prediction frame (see below "**Prediction parameters**") a value far enough into the future is returned, that will be overwritten by subsequent predictions. | ||
| 28 | |||
| 29 | With the current configuration the controller uses the middle point in time between the first and last possible time in order to be most positive to hit the ball. | ||
| 30 | The computed velocity is also passed to the controller and used is some more advanced tactical decisions like catching balls. | ||
| 31 | |||
| 32 | == Configuration == | ||
| 33 | |||
| 34 | ---- | ||
| 35 | |||
| 36 | === Prediction parameters === | ||
| 37 | |||
| 38 | (% class="wrapped" %) | ||
| 39 | |=((( | ||
| 40 | Parameter | ||
| 41 | )))|=((( | ||
| 42 | Description | ||
| 43 | )))|=((( | ||
| 44 | Default value | ||
| 45 | ))) | ||
| 46 | |((( | ||
| 47 | PREDICTION_FRAME | ||
| 48 | )))|((( | ||
| 49 | window size for prediction - How far | ||
| 50 | |||
| 51 | into the future are predictions calculated at most. | ||
| 52 | )))|((( | ||
| 53 | 70ms | ||
| 54 | ))) | ||
| 55 | |((( | ||
| 56 | PREDICTION_STEP_SIZE | ||
| 57 | )))|((( | ||
| 58 | size of the steps for the forward search | ||
| 59 | )))|((( | ||
| 60 | 10ms | ||
| 61 | ))) | ||
| 62 | |((( | ||
| 63 | REFINEMENT_FACTOR | ||
| 64 | )))|((( | ||
| 65 | The prediction step size for the reverse search is | ||
| 66 | PREDICTION_STEP_SIZE divided by REFINEMENT_FACTOR. | ||
| 67 | )))|((( | ||
| 68 | 10 | ||
| 69 | ))) | ||
| 70 | |||
| 71 | When setting the PREDICTION_FRAME keep in mind that predictions, that reach multiple frames into the future, are likely to be overwritten, when new frames arrive and new predictions are calculated. | ||
| 72 | Nevertheless it is reasonable to calculate predictions beyond the next frame in case a ball is not recognized on the next frame. | ||
| 73 | |||
| 74 | With the default values up to 7 predictions are calculated if the ball is not predicted to be in flipper range roughly within the next two frames. | ||
| 75 | |||
| 76 | The REFINEMENT_FACTOR is also the worst case number of predictions calculated during the reverse search part of the prediction process. | ||
| 77 | |||
| 78 | \\ | ||
| 79 | |||
| 80 | === Flipper range === | ||
| 81 | |||
| 82 | The model of the area, where the the flippers can hit a ball is computed from a set of five points. These points are set to real world coordinates in meters | ||
| 83 | |||
| 84 | and should not need further adjustment after the camera is correctly calibrated (see [[doc:Projects.Archive.Pinball Project - Winter Term 201920.Project Documentation.World Coordinate System.WebHome]]). | ||
| 85 | |||
| 86 | The values for the left flipper are set to be symmetrical. | ||
| 87 | |||
| 88 | [[image:attach:physics_points.png]] | ||
| 89 | |||
| 90 | (% class="wrapped" %) | ||
| 91 | |=((( | ||
| 92 | Point# | ||
| 93 | )))|=((( | ||
| 94 | Description | ||
| 95 | ))) | ||
| 96 | |((( | ||
| 97 | 1 | ||
| 98 | )))|((( | ||
| 99 | center point of the circle that the flippers tip moves o | ||
| 100 | ))) | ||
| 101 | |((( | ||
| 102 | 2 | ||
| 103 | )))|((( | ||
| 104 | highest point a ball can be at when it hits the resting flipper | ||
| 105 | ))) | ||
| 106 | |((( | ||
| 107 | 3 | ||
| 108 | )))|((( | ||
| 109 | lowest point a ball can be at when it hits the resting flipper | ||
| 110 | ))) | ||
| 111 | |((( | ||
| 112 | 4 | ||
| 113 | )))|((( | ||
| 114 | lowest point a ball can be at when it hits the fully triggered flipper | ||
| 115 | ))) | ||
| 116 | |((( | ||
| 117 | 5 | ||
| 118 | )))|((( | ||
| 119 | highest point a ball can be at when it hits the fully triggered flipper | ||
| 120 | ))) | ||
| 121 | |||
| 122 | \\ | ||
| 123 | |||
| 124 | From these points an area, where the ball is deemed to be hittable, is computed. | ||
| 125 | |||
| 126 | [[image:attach:physics2.png]] | ||
| 127 | |||
| 128 | \\ | ||
| 129 | |||
| 130 | === Inlane Area === | ||
| 131 | |||
| 132 | Since the trajectory prediction does not consider collisions in general, we need a different kind of predictions for the inlanes, | ||
| 133 | i.e. the path feeding a falling ball from the playfield to the flippers, behind the slingshot. | ||
| 134 | A ball is considered to be in an inlane, when it is in a rectangular area, with one edge given by the beginning of the flipper | ||
| 135 | and the other by a point at highest, outermost position of the angled part of the inlane. | ||
| 136 | |||
| 137 | In this area the vertical acceleration is deactivated in the prediction, since it would cause the prediction to assume the ball would slide into the wall. | ||
| 138 | Though in reality there is still some acceleration, a simple linear prediction has shown to be practical in this less critical area. | ||
| 139 | |||
| 140 | [[image:attach:physics_inlane.png]] |