Making a pong game shaped like a octagon, need help!

5 views (last 30 days)
I am trying to make a pong game in matlab that has an octagon shaped court to learn about the math of billiards.
At this point I have the goals working and I have the ball bouncing for every case except for the diagonals of the octagon.
The top, bottom and sides are easy because I just test if the new x and y vectors I'm creating are > or < then the values I have set for the walls. The diagonals are a bit tricky because I need to test the x and y vectors of the ball for every single point of the diagonal line.
I created the polygon using
POLY = [1 75; 1 25; 75 5; 175 5; 250 25; 250 75; 175 96; 75 96];
POLYG = [POLY; POLY(1,:) ]; %repeat first row at the end
plot(POLYG(:,1),POLYG(:,2))
axis equal %same scale on both axes
I need a way to create a set of points between the points that draw the diagonal line so that I can test the balls path against those points. E.g. if one diagonal is the line between (1,25) and (75,5) on my plot how do I get all of the sets of points between them into a matrix?
Thank you for your help

Accepted Answer

Image Analyst
Image Analyst on 10 May 2015
Edited: Image Analyst on 10 May 2015
It's not that much harder. You just have four equation of lines like
y = x + r % Upper left quadrant
y = -x + r % Upper right quadrant
y = -x - r % Lower left quadrant
y = x + r % Lower right quadrant
I'm sure you can figure out what r is based on your side length or whatever other parameters you're using - it's just simple math. Then you just stick in your proposed x and find out if the resulting y is greater than or less than the y from the above equations. Like if the proposed x = 4, and your proposed y = 5, but the y from the equation would be 4, and it's in the upper half plane, then your proposed y is above the lines, so you need to reflect it. Does that make sense?
  4 Comments
brian grove
brian grove on 10 May 2015
I like that idea. Do I need to put this in a loop so binaryImage can use all values in the matrix? or is it simpler than that?
Image Analyst
Image Analyst on 10 May 2015
You must have a loop for moving your ball don't you? How else are you going to generate new (x,y) coordinates?

Sign in to comment.

More Answers (0)

Categories

Find more on Number games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!