News:

MFG's Character of the Month: October 2026 Nominations

Main Menu

Math Question

Started by altoiddealer, November 08, 2016, 01:50:11 AM

Previous topic - Next topic

altoiddealer

I'm coding the AI for this pong-like hyper move, but struggling to get the behavior I'm looking for.

I know someone here knows the answer right off the top of their head.

I would like an equation to calculate the final Y pos, given the starting X Pos, Y Pos, X Vel and Y Vel.  The ending X Pos is 230.

This is in a 230 x 320 space where the ball will deflect off the top and bottom walls.

Please see crudely drawn image

Spoiler (click to show)


Many thanks in advance!!!


altoiddealer

#1
Figured out that the equation I'm looking for is for a Triangle Wave

Getting closer to figuring this out (I think)

Their example:   y(x) = abs((x % 4) - 2) - 1


I'll be editing below and hoping someone can fill in my blanks or point out errors

Period: Gameheight
Amp: Vel X/Vel Y
X: Initial Pos Y (???)

abs((Pos Y % Gameheight) - 2) - (Vel X/Vel Y)


Odb718

would it error out on vel y = 0?

Are you just trying to get the pad to the spot before the ball gets there?
Is there a speed limit on how fast or slow the pad can move up or down?
I'd think an easy way to fake this is just have the pad watch the pos y of the ball and set it's vel y depending on the difference in distance.

like if pos y < 30+ball, pos y then vel y = 4
You could even figure out the ball pos x and divide that and use that as a velmul or veladd.
vVv Gouken718 vVv SabreTooth718 vVv


altoiddealer

#3
Quote from: Odb718 on November 08, 2016, 04:54:13 PM
I'd think an easy way to fake this is just have the pad watch the pos y of the ball and set it's vel y depending on the difference in distance.

like if pos y < 30+ball, pos y then vel y = 4
You could even figure out the ball pos x and divide that and use that as a velmul or veladd.
That's how I have it set up now, and it works, but I hate how it tracks the movement so specifically as the enemy bounces from top to bottom.  If I have code allowing the AI to make a mistake (lose), then it looks whoever is controlling the movement simply gave up.

To make it look more like a real player, I will need the final Y value in hand.  With that I can factor a number of things like Time (for paddle velocity), Random% (AI difficulty), etc, during the time the "ball" is on its way.


My formula is currently trash :)  There probably shouldn't even be a division of X and Y.  I'll be stumbling through this the whole way


XGargoyle

I had to code a pong game back during my university days, and the solution I did for the AI was the following one:

The AI paddle has a Y position
The ball has at all times a Y position
You calculate the offset difference between ball Pos Y and paddle Pos Y, allowing a margin for the paddle width and the AI scale level

With low AI, the margin is bigger. With high level AI, the margin is lower.

So, how it works: on high AI, the paddle axis will be almost aligned at all time with the ball Pos Y, ensuring the bounce.
Under low AI, due to the big margin, the paddle will move only as long as the Ball Pos Y is not within the range. When it is in range, the paddle will not move, waiting for (and expecting to) bounce the ball. If by luck, the paddle is exactly aligned to the ball, then the silly AI will reflect the ball. If not aligned, the AI will lose the match

Hope I make sense with my explanation, do not hesitate to ask for more details.


XGargoyle: Battle posing since 1979
http://xgargoyle.mgbr.net
http://www.pandorabots.com/pandora/talk?botid=e71c0d43fe35093a  <-- Please click that link
http://paypal.me/XGargoyle  <-- Donations welcome!

Odb718

I'd say monitor the ball's pos x and velocity. So you know which way it's heading and how fast.
If it's moving away, return to center slowly.
If it's moving towards but not half way remain stationary.
It's half way and moving fast, move fast, if it's half way and slow move medium.

Use time to increase the distances of error. This will look like AI to the average idiot. Every second played would be 10 pixels difference or something. If you scale the AI up, scale down the pixels of error.
vVv Gouken718 vVv SabreTooth718 vVv


altoiddealer

Thanks guys, this is all good logic to apply to this AI.

I won't hold my breathe waiting for the prediction formula, but I'm hoping someone can jump in with it (or if I can figure it out).