Making a pong game shaped like a octagon, need help!
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Image Analyst
2015년 5월 10일
편집: Image Analyst
2015년 5월 10일
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
Image Analyst
2015년 5월 10일
You must have a loop for moving your ball don't you? How else are you going to generate new (x,y) coordinates?
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!