Game AI or in my case just simple Maths

Hello everyone, long time lurker here. But I am learning both to Codea and OOP as an on going process (as I guess we all do), so decided to join the discussions. Or in my case be the discussion due the ton of questions always running around in my mind as I try to code something.

So yes, not an absolute newby, but never the less I beg all your pardons for any stupid questions that might come your unfortunate way from self:init. Oh dear, sorry for that. Let’s hope I will be the the only instance.

I am working on a pong game and with the useful tutorials from various wonderful posters on here and the example apps within Codea I have come quite far. But I am now starting with two aspects, first is the fsm, which I am going through at our aussie colleague’s blog: Minesweeper. Aussies. You know? Bloody cheating cricketing rugbying Aussies. Another matter entirely. Struggling a bit to convert it into my adaptation, but never the less it is not why I am standing with hat in hand.

I want to also create a single player mode, which is giving my poor saffa brain trig nightmares. The “computer’s paddle” movement. Urgh. Stupid paddle. What? Oh yes I am wrapping my head around polar vs cartesian systems. So here is what I got into my small brain so far:

Vectors are really, really cool and sits at the heart of codea, but they can become very complex and knowing which system to use for a newby is quite the experience. Do I apply the Pythagorean theorem or is it not even applicable. So I try to break it down to what is known and what must be calculated:

Known are x, y coords of the ball, both paddles and the space they are in at any given time. With math.atan I should solve for angle of origin uhm right? Then if no bounce against a wall the vector length should give me where the ball will go? What if it bounces? I have a funny feeling that it’s a simple matter that I am overcomplicating, or its serious business and I should go and study more?

Coul someone send me in the right direction, I’ll do the coding myself so that I learn from it. Its just what and how to apply I guess?

Thanks everyone, and hello once more from Sunny South Africa!

@CodeD Welcome. I think you’re trying to put too much math into it. Just have the center of the paddle follow the verticle value of the ball. When the ball reaches the paddle, it will always hit it’s center.

To figure out where to put the paddle, you need to figure out where the ball will reach the computer’s edge of the screen.

Suppose the ball is at position (bx,by), travelling at a velocity of (vx,vy) from left to right toward the computer’s paddle (defending the right of screen).

You need to know the height of the ball when it reaches the x position of the paddle (px, say). We’ll call the unknown height py.

The time taken to travel from bx to px is (px-bx)/vx

In this time, the y value will change by vy * (px-bx)/vx, so the y position is

py = by + vy *  (px-bx)/vx

However, if the ball bounced at the top or bottom of the screen, vy reverses direction after the bounce, so we need to allow for that. Most simply

if py<0 then py=-py elseif py>HEIGHT then py=2*HEIGHT-py end

If this looks like black magic, just try drawing it on a piece of paper, it should become clear.

PS I’m another Aussie, I’m afraid - but swallow your pride now, your lads just weren’t good enough. Good losers and all that.

@CodeD Here’s a simple example of what I was talking about. Having the paddle follow the ball not using any math.


function setup()

function draw()

yes, but who wants to play a perfect computer player? :-??

To the both of you, a sincere thanks. I will get stuck into your kind advice and hopefully see the light.

Ps. Pffffffft bloody aussies. :slight_smile:

@Ignatz That was just a simple example to show no math involved. To make it more interesting, random code would be added so the ball would go in different directions and speeds based on where it hit the paddle. There would still be no math involved to calculate the direction of the ball.

@CodeD - you may have found them already, but I’ve written lots of posts and ebooks that may help you in general

oi oi

the example is so perfect that i couldnt play

@Ignatz, yeah thanks mate. Your tutorials have been absolutely brilliant, also as mentioned the other contributions by other posters. To be honest actually the most I learned was from your showcase program. I could see the applications applied and that helped me loads. Btw, did you manage to patch the error in your code yet? It’s complaining over indexing in your table class. I was heartbroken after the last update when I saw how many apps were now spewing out errors. Most I could fix, but your I have not tried yet.

which app was giving an error? i thought i fixed mine.

@Ignatz You did too, I got my usernames mixed up. My apologies. :">