# Help on Optimization Problem for Tennis Ball Physics + prediction

## Recommended Posts

Hi, my team and I are stuck at section 3.2 of this PDF attached (link below too). We are implementing realistic ball flight with prediction for our tennis game made in Unity and need help with this optimization problem.

In a a nutshell, we are implementing realistic tennis ball flight in our game and require a prediction algorithm so that the player can control where the ball lands precisely via mouse cursor.

If this is something you are familiar with and would like to take a shot, please let me know. Any help would be super appreciated!

Screenshots:

video game tennis ball physics 39.pdf

##### Share on other sites
Posted (edited)

I have coded something like this in 3D, but I lost the source code. I have no idea about applying optimizations. I would avoid optimizations until you have the rest of the code working successfully.

Getting the ball's path in 3D space also must take into account the Magnus force. There is also a wind force, if you want to get physical. Here force F = ma, where a = F/m.

Getting the server's ball's initial velocity and initial angular velocity, to solve for the region that you've mouse-clicked on, is not simple. The naive method is to serve many balls and find the one that lands closest to the mouse click. What method are you trying now? Are you familiar with the concept of gradient descent? Once you find the landing spot closest to the mouse click, you can use gradient descent to hone the location: https://en.wikipedia.org/wiki/Gradient_descent

Edited by cowcow

##### Share on other sites

Getting the ball's path in 3D space also must take into account the Magnus force. There is also a wind force, if you want to get physical. Here force F = ma, where a = F/m

-not doing wind force as thats too sim heavy for now

Getting the server's ball's initial velocity and initial angular velocity, to solve for the region that you've mouse-clicked on, is not simple. The naive method is to serve many balls and find the one that lands closest to the mouse click.

-someone just suggested the same thing to me. thought it was a viable way. how is this naive? (are are developing for PC/mac and not mobile). Could this be a valid workaround until a more efficient solution is devised?

What method are you trying now? Are you familiar with the concept of gradient descent? Once you find the landing spot closest to the mouse click, you can use gradient descent to hone the location: https://en.wikipedia.org/wiki/Gradient_descent

-haven't started, still on the drawing board. Are you on discord? would like to discuss with you further. Posting here is good too!

##### Share on other sites
Posted (edited)

I forgot to mention that you should also take drag into account.

Naive because it's a brute force method. Im sure there's some analytical solution, but I don't know it.

I'm not on discord. I am on GitHub. Can you post the code to GitHub, please? I ask only because all of my source code is in the public domain, so I expect the same from everyone.

Are you working in 3D or 2D?

P.S. cowcow was my temporary account.

Edited by taby

##### Share on other sites
Posted (edited)

I'm working in 3D. Were you able to find your source code?

I'm happy to post my code to GitHub if I get to view yours. Can you provide the link here. while I get everything uploaded to GitHub?

For drag, I'm using Unity which takes care of drag for Rigidbodies. However if it has limitations in reaching my gameplay goals, I'm willing to consider implementing a custom solution. I just don't have the expertise to implement anything custom on my own, hence this forum post

##### Share on other sites
Posted (edited)

I lost my code years ago, sorry. It's lost for good.

Don't worry about sharing your code.  I wish you the best of luck.

Edited by taby

##### Share on other sites
Posted (edited)

would you be able and willing to recode it? I'd be happy to compensate you hourly or as a lump sum if you have an estimated # of labor hours.

Since code sharing isn't mutual at the moment, I prefer to keep my code closed source for now. But if we reach a workable solution together on a pro bono basis, I'm happy to make it open source.

In other words, please let me know if either of these options are possible: 1) contract work with closed source code, or 2) open collaboration and open source upon completion.

##### Share on other sites
Posted (edited)

Let me think about it. I'm leaning toward doing it myself from scratch. I won't worry about things like collision with a net, just to simplify everything -- you can worry about those details later. The code would be in the public domain, so don't worry about compensation. I'll use OpenGL 1.x code, so it's fairly simple to follow (and replace, with your own code, if you like).

Do you already have the code that converts a mouse-click into a location on the court?

Are you using C++?

Edited by taby

##### Share on other sites
Posted (edited)

i could implement that. However right now my problem is I have 2 different test scenes (as-is) but have not integrated both into 1 test scene (ideal).

Scene #1: Right now the ball always launches towards an invisible game object. All I would simply need to do is set the object position to mouse position. However this is for my test scene w/o magnus force. Based on this projectile tutorial - https://vilbeyli.github.io/Projectile-Motion-Tutorial-for-Arrows-and-Missiles-in-Unity3D/

Scene #2: For my scene with magnus force, I don't know how to control the landing point yet since the algorithm is different. X distance is unknown and calculated from everything else. But for a skilled programmer, it shouldnt be hard to merge my code for these two different scenes.

##### Share on other sites

It is potentially a difficult problem if you don't have access to the exact calculations used in the physics. On that basis if you wanted to do it the way you are thinking you might be better doing the ball physics yourself.

However this is for a game, so smoke and mirrors. My first thoughts were, send off the ball towards the destination with a ballpark solution, then refine it by applying forces in flight.

Then I just thought of a far simpler way which may achieve what you want. Find a ball park solution for ball going from player A to destination B. Then find a reverse rough solution going backwards from the destination towards player A. Neither of these has to be exact. Then in flight, simply lerp between the two solutions.

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 12
• 9
• 11
• 15