Battle pong #5 -An intelligent body

Previously in Battle Pong… *Screen fades to black*

After successfully implementing a touch movement, I got a relatively simple AI. After some tests it became obvious that the physics model was not configured or done properly and that the AI needed to be smarter.

Let’s do it !

bug
The current physics calculations are incorrect at high speed and the AI is really basic

As you can see in the above picture, if we move the Shield too fast then the collisions are not computed correctly. It’s an issue since part of the gameplay is based on the idea of smashing the ball to modify the orientation.

 

Adjusting physics !

In order correct this issue I had to go deeper in the world of the Unity physics 2D engine. Here’s my understanding of it :

  • I made the mistake of moving the GameObject.transform instead of the associated RigidBody2D
  • It’s possible to increase the numbers of calculations but I’d rather avoid it for now, maybe I’ll do that later !
  • I made the mistake of modifying physics stuff in Update() instead of FixedUpdate(). All physics calculations should be done in FixedUpdate(). Update() happens every frame while FixedUpdate() happens at a fixed rate defined in the engine configuration.
  • Last but not least, you can setup different Interpolation mode of a RigidBody2D. The answer on this post is pretty clear but I’ll try to sum it up.
    • Extrapolation : each frame the engine will “guess” where the object will go depending on the previous positions
    • Interpolation : each frame the engine will consider there is a delay and will use one of the previous position.

I selected interpolation since movements of the pad will be fast I expect some… reaction delays in the calculations. So after dealing will all of this (basically changing some line of codes and checking some buttons…) here’s the result :

smooth.gif
It’s smooth ! And very fast….

Collisions are okay but other problems arise… I will come back to it later ! Now that we are done with the physics, let’s work on the AI.

Harder, Better, Faster, Stronger…

I think that the AI improvement will be step by step. First I need to make it more… dangerous and more aggressive. The idea is simple, when the AI is closer to the ball it will rush to smash the ball before going back to initial position. I’m curious to see the result of this first try…

AI2.gif
Result : it’s to hard…..

Well…. at least it’s not easy anymore….

To sum it up

What I learned ?

  • Physics requires some tuning depending on the project to get the expected result
  • It’s still not fun to play with a bar
  • It’s not fun to play when you struggle to even touch the ball
  • It’s not fun to play against an opponent too strong

Next steps ?

  • Make the AI more stupid ! (last time I said it was too dumb…)
  • The collider deactivation script is now critical !
  • I will probably need to limit the ball speed….

Thanks for reading!

 

Battle pong #5 -An intelligent body

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s