How to calculate the path of an object

I have a ping pong game where only one player can play, I need to make the second paddle move around to hit the ball. So, I am trying to calculate the path of the ball, so I can point the enemy paddle move around and hit it. 

Also, the community has been very helpful in providing me many answers, but I kind of want to build it myself. So, when helping me just give a step by step process, or even pseudocode, because when I build it myself I gain a better understanding of it, and I don’t want people doing my work and using their time and energy. 

Thank you!

Hello,

If you know the initial rate/speed and position of an object, you can calculate his total path with calculation.

So make on a paper the speed vX and vY, the position at a time origin and find the equation path on a paper f(x,y), and then solve the equation f(x,y)=y*, where y* is the final destination happens at a time t*.

And use proposition with your width and height screen to have a “grid” to calculate.

Yvan.

Do you know calculus?  

I ask, because I think you’re really talking about intersection testing for two moving bodies.

If you want to calculate the position of a single body moving at a fixed velocity, you don’t need calculus, but if you want to calculate where two independent bodies with their own velocities will intersect, it certainly helps to be a familiar with more advanced mathematics.

In truth, for this scenario, what you really want to calculate is:

  • Vector of the moving ball
  • Where that vector intercepts the line the padde lies on
  • time of that intersection

then, with that information, you can set up your AI/algorithm to:

  • move the paddle there, and if you want
  • add an extra layer and move the paddle at the right (or wrong for a miss) rate

Everything you need for the above is in SSK2.  I’ll see if I can type up a sample later.

PS - The reason you don’t really need calculus here is because there is no acceleration.  If the ball were also acting under force of gravity then you would need to use calculus.  Fortunately, all of the quantities you want can be solved with algebra and a fundamental knowledge of vectors.

A few more questions before I come back with help later.

  • Do you understand the difference between a line and a segment
  • Have you taken any of these: geometry, trigonmetry, or algebra
  • Are you solid on the concept of a vector?  i.e. Can you answer the question, “What information does/can a vector represent?”
    • You don’t need to know everything at that link, but it is a great collection of vector types.
  • Do you know what a unit-vector is?
  • … I may add others later.

@sdktester15,

Hi.  I’m putting together a sample right now to answer the intersect part of this question, but can you tell me:

  1. Have you ever run the Validation code for SSK2?  This is my testbench for verifying features, but I provided it to SSK2 owners because they are also great examples.  There is an intersect example/test in the test suite (test #31 as of today).
  2. Did you examine the SSK2 math2d features/page?

Note: I’m not admonishing you here.  I think you are trying to do this without SSK2, which is awesome because that is how you will best learn the concepts.  However, I worry that I’ve done a bad job of making it easy for folks to find help in my many sources:

The steps are very easy w/ Corona SDK

  1. Calculate the line the paddle lies on.  (Two points representing a line segment will do just fine.)

  2. Get the velocity of the ball.

  3. Calculate two points representing a line segment from that velocity.

  4. Use SSK2 line-line intersect function to determine if there is an intersection and if so where.

I made a demo showing how this works:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/SSK2/forums_help/line_intersect.zip

https://www.youtube.com/watch?v=Zs_xuT4FlaI&feature=youtu.be

In closing, I know I sometime write scary answers (all my answers before the example), and I do this because I want the person asking a question (as well as future readers) to be clear that while the final answer may be elegant, getting to it is not simple and is built on a foundation of knowledge and learning.

I want you guys to be successful in game development (and beyond; this is really a stepping stone to other and bigger things for many of you), but it is crucial that:

  • You never look at a game development problem and think, “That looks simple so it must be easy.”
  • When you discover it is harder than it seems, you not quit.  Instead double down and determine what experience and knowledge gaps you have and fill them.  (This is the bigger and better things part I mentioned above).

Finally, I want all folks who ask questions to truly truly appreciate how hard it is to answer these questions sometimes.  As someone who answers a lot of questions, sometimes I read a question and I feel like my head is going to explode (don’t worry @sdktester15, not not this time).  

Then I have to remind myself.  "This person thinks the answer will be easy to give because they do have gaps in their knowledge and experience, … how can I answer this without coming off as a jerk and without crushing their interest in game development and programming.  (Sometimes I do well, sometimes I fail.)

Cheers,

Ed

I couple of answers to your questions. 

I do not know calculus (it would be amazing to know calculus at the age of 13) , but I do know geometry and algebra, and I am now starting trigonometry.

I understand the difference between a segment and a line. 

I understand the definition of a vector, but not its different types, (I don’t know what is a unit-vector)

Thank you for the example, I will try to work off of what you gave me.

Also, would this example include the possibility of the ball bouncing off of walls?

Rock on.  

Keep in mind.  You don’t have to make a perfect PONG.  

If you can make the game such that:

  • two players can play,
  • it handles scoring,
  • ball motion is purely handled by box2D collision responses (i.e. bouncing; try to solve this without changing the velocity every time it hits a paddle),

… then you are ready to move on to another game like  breakout or space invaders.

  1. No.  This doesn’t handle a reflected bounce.  At least not directly

  2. Yes. Remember, you are doing this check every frame (or less often if you take my advice from the other forum thread).  So, you’ll be constantly determining where the paddle needs to move to.  i.e. Once the ball gets past the wall bounce, the vx,vy will intersect at a new point on the line.

If you get entirely stuck let me know later today.

I will update the example with a paddle mover so you can see it in action.

However, you said you want to self solve and I think you can.  So, give it a good go and then ping back here with an update on your progress either way.

Ok, thank you!

You asked a question and deleted it, but the answer is (you may have figured this out already, thus the deletion)…

You don’t need the angle.  You need vx,vy.

This is weird.  I keep seeing questions in my e-mail, but when I come here they are gone.  

Hey.  I’m going offline.  So’ll check back and see if you got stuck or succeeded tonight.

-Ed

Unfortunately, I was not able to succeed. I kept getting stuck on the part where it has to calculate the path after the object has bounced off the wall.

if you watch the ai of most existing pong games, they just do something very simple like:

if (ball above paddle) then   -- move up, either at a built-in fixed speed, or as some ratio of the vertical separating distance -- or choosing among those two options based on the \_horizontal\_ separating distance end -- and similarly for below/move down

and it’s often surprisingly effective, even without bounce prediction.  sure it’s not perfect, but that tends to actually aid gameplay, because if the ai never failed then it would be too hard.

having said that, calculating bounces isn’t that hard, if you’re determined to do it like that anyway.  since you guys are talking vectors (and even calculus) the reflection formula is: r=d-2n(d.n), where d is the input vector (path of the ball), n is the surface normal (a unit vector perpendicular to the wall, just <0,1> or <0,-1> for horizontal walls), and r is the resulting reflection of d about n (the NEW path of the ball).

Ok, so how would I implement this formula into code? I really want to do it this way, so I can expand my knowledge on vectors.