Page 1 of 1
How to calculate future position of Ball?
Posted: Tue Feb 28, 2012 6:51 am
by pixeldruid
Hi,
I am new to this forum. I am planning to use bepu for one of our sports based game. I am wondering how to predict future ball position prediction as my AI will be heavily dependent on that.
is ray casting is the way to go? is the possibility of ball bouncing off is factored in ray casting?.
please help
Thanks
Re: How to calculate future position of Ball?
Posted: Tue Feb 28, 2012 8:55 am
by Norbo
Predicting the position precisely is equivalent to simulating it. One option is to just simulate the action from start to finish in a parallel space. This 'predictor' space could have only the most important elements of the environment and the main gameplay elements to cut down speed so that it can be simulated at an extremely high rate of speed.
If perfect determinism is required, the simulation could run during "AI planning time" while recording the state of the game (e.g. the location of the ball(s)) during the simulation . If the AI chooses that move, then the simulation is played back. This approach isn't always usable though- if the game state changes dynamically because of later user input, the replay is invalid. It would work well for something turn based like pool.
If you don't need determinism and you don't need a particularly accurate prediction, then just using a much cheaper approximation would probably be fine. For example, a soccer ball would follow a roughly ballistic trajectory, perhaps with some modifications to account for wind or the magnus effect. A curve could represent this trajectory without the need for simulation. Bounces would be tricky against a surface which doesn't have an extremely predictable surface. For example, the bounce off the floor is simple enough to predict because its a flat plane. On the other hand, precisely predicting the contact normal on impact with a cylindrical goal post would be quite difficult without explicit simulation.
is ray casting is the way to go? is the possibility of ball bouncing off is factored in ray casting?.
Casting a ray is just querying the space for objects using a origin and direction with some length. It is possible to use a series of ray casts to approximate a curve and to approximate bounces. A raycast approximation isn't really easier than just doing the simulation, though, and it's not going to produce results as close to the real simulation as a real simulation would.
Re: How to calculate future position of Ball?
Posted: Wed Feb 29, 2012 5:33 am
by pixeldruid
Hi Norbo
Thanks for your insights. Will running a parallel simulation, for a ball's with flat surface with bounce, be handled by a WP7 hardware? If so, is there any way in the engine to predict future position? i.e like get positional value after 3 seconds from now etc?.
Thanks
Re: How to calculate future position of Ball?
Posted: Wed Feb 29, 2012 6:20 am
by Norbo
Thanks for your insights. Will running a parallel simulation, for a ball's with flat surface with bounce, be handled by a WP7 hardware?
It shouldn't be a significant problem. It may require deferring the simulation to keep the framerate smooth. As in, only do 20 timesteps or so of the super-simple predictor simulation each frame. The AI will have to wait to get a response back about the result, but typically AI doesn't need instantaneous feedback.
If so, is there any way in the engine to predict future position? i.e like get positional value after 3 seconds from now etc?.
In general, predicting the state of a simulation at some time requires performing the simulation up to that time. There is no future state without the intermediate states. If you want to only simulate three seconds, then pick your desired time step duration (Space.TimeStepSettings.TimeStepDuration, defaults to 1/60f) and perform as many Space.Update() calls as needed to reach the time. For example, at a timestep duration of 1/30f, you would need to call Space.Update() 90 times to simulate 3 seconds. (Note that Space.Update() is distinct from Space.Update(dt); the latter can call a varying number of timesteps in an effort to keep up with the passed in time.)
When there are guarantees like uninterrupted ballistic motion, approximations like the purely analytical curve approach can get you a significantly rougher guess with much less computation. These approaches can be thought of special cases of general simulation- because there's so many requirements and assumptions, less data must be actually simulated. For example, an object launched into the air with an initial velocity and affected only by gravity travels in a parabola. If the prediction you want is 'the impact location,' you don't need to run a full simulation. Just solve the curve's equation for intersection with the ground.
Re: How to calculate future position of Ball?
Posted: Wed Feb 29, 2012 6:35 am
by pixeldruid
though calculating impact position is important to me, equally important is to know what happens to the ball after impact. my AI will try to catch the ball if possible, but if not it has to chase the ball down, pick the ball up and throw it back.
great to know that we can fast forward the simulation and know the ball state ( accounting for floor ).
Also
For the case mentioned above , will solving for parabola and after impact solving for linear eq. would be a close enough approximation? In that case will the actual simulation lag or lead the calculated value? for instance if the actual simulation lags than predicted position will not be a issue, as my AI fielder will arrive early and wait for actual ball to arrive. If it is other way around then it might be a problem because based i would have changed lot of game state based on that prediction.
Thanks a lot man. you are awesome
Re: How to calculate future position of Ball?
Posted: Wed Feb 29, 2012 6:51 am
by Norbo
For the case mentioned above , will solving for parabola and after impact solving for linear eq. would be a close enough approximation? In that case will the actual simulation lag or lead the calculated value? for instance if the actual simulation lags than predicted position will not be a issue, as my AI fielder will arrive early and wait for actual ball to arrive. If it is other way around then it might be a problem because based i would have changed lot of game state based on that prediction.
The approximation can be expanded to perform post-impact guesses, but the approximation as presented would not have sufficient information to know if it leads or lags the real simulation. For example, on impact, linear and angular velocity interact to redirect the ball in ways that a simple function intercept approximation won't capture. The concept of angular velocity could be introduced to the simulation to improve the fidelity, but this will be a little slower. Basically, the more elements of the real simulation are included, the closer the results will be to the real simulation.
On the other hand, humans aren't very good about predicting the result of collisions. The more complex the situation is, the worse we are about predicting it- and angular velocity can make things pretty complicated. If the AI uses an incomplete view, it might just behave more like a human. The key is to incrementally refine the behavior after the AI sees its guess fail (as a human would). The ability to correct for prediction errors is needed anyway, since unless the game is turn-based, other environmental interactions could render a prediction completely wrong.