Azzera filtri
Azzera filtri

Geometry question for a pong game

3 visualizzazioni (ultimi 30 giorni)
brian grove
brian grove il 30 Mag 2015
Risposto: Josh il 17 Ago 2017
I am trying to create a pong game that reflects the ball off the walls of an octagon.
I have a polygon that is defined by....
POLY = [1 75; 1 25; 75 5; 175 5; 250 25; 250 75; 175 96; 75 96];
BLWALLY= linspace(POLY(2,2),POLY(3,2),1000);
BLWALLX= linspace(POLY(2),POLY(3),1000);
BRWALLY= linspace(POLY(4,2),POLY(5,2),1000);
BRWALLX= linspace(POLY(4),POLY(5),1000);
TRWALLY= linspace(POLY(6,2),POLY(7,2),1000);
TRWALLX= linspace(POLY(6),POLY(7),1000);
TLWALLY= linspace(POLY(8,2),POLY(1,2),1000);
TLWALLX= linspace(POLY(8),POLY(1),1000);
I also have x and y coordinates for my ball defined by
newX = ballX + (ballSpeed * ballVector(1));
newY = ballY + (ballSpeed * ballVector(2));
my if statement to detect when the ball reaches the wall is
elseif ((newX <= TLWALLX) | (newY >= TLWALLY-BALL_RADIUS))
Now for the difficult part... I need to manipulate the X and Y of my ball based on the angle between the corner walls of the octagon and the ball when it hits the wall.
heres what I have tried doing:
theta=atan2d(newY,newX)-atan(TLWALLSLOPE);
reflectv = [(cos(theta) * ballVector(1,1)) - sin(theta) * ballVector(1,2) , (sin(theta) * ballVector(1,1)) + cos(theta) * ballVector(1,2)];
ballVector=reflectv;
This is not working for me.. the ball starts bouncing all crazy. I'm not sure if my equations need tweaking or if I need a whole new approach.
Your help is really appreciated

Risposte (1)

Josh
Josh il 17 Ago 2017
One obvious problem (might be a typo), when you assign theta you use atan2d, which returns an angle in degrees, and atan, which returns a value in radians, so the result is gibberish. It's probably best to avoid using inverse trig functions for what you're trying to do, since they're not continuous. It looks like you're using a Given's rotation matrix to change the direction of the ball. It may be more straightforward to use a Householder reflection matrix:
H = eye(2) - v*v';
Where v is the tangent vector of one of the octagon sides. So the direction of the ball would be modified like:
ballVector = -H * ballVector;
This avoids having to use any messy inverse trig functions.
(The code assumes you're vectors are stored in columns)

Categorie

Scopri di più su Video games in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by