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!