HELP - Need help with vectors.

2 views (last 30 days)
brian grove
brian grove on 21 May 2015
Edited: Joseph Cheng on 22 May 2015
So Ive recently written a pong game that is shaped like an octagon and I was manipulating vector values to make the ball bounce. Come to find out this does not work for the angled sides of the polygon.
I have a vector that represents the path of the ball and a set of points that I draw the polygon with. I need to come up with a way to calculate the angle between a corner of the octagon and the path of the ball so that I can reflect the ball at this same angle.
First I need to figure out how to make each side of the octagon into a plottable vector...
POLY = [1 75; 1 25; 75 5; 175 5; 250 25; 250 75; 175 96; 75 96];
BLWALLV= ([75,5]-[1,25]);
I tried this but when I graph BLWALLV the vector does not line up with the graph of my polygon. Any help with this would be greatly appreciated!
Thanks

Answers (1)

Joseph Cheng
Joseph Cheng on 21 May 2015
Edited: Walter Roberson on 21 May 2015
If we took some time and looked at what you've done.
POLY = [1 75; 1 25; 75 5; 175 5; 250 25; 250 75; 175 96; 75 96];
BLWALLV= ([75,5]-[1,25]);
it is clear that you are not creating BLWALLV in the way you want
[75,5]-[1,25] = [74 -20]
which is either a single point at x = 74 and y=-20. If you were plotting it as
plot([ 74 -20])
then you have essentially plotted
plot(1:2,[74 -20])
  7 Comments
Joseph Cheng
Joseph Cheng on 22 May 2015
ah... rereading the question i see where i misunderstood. I see now after looking at the math you were calculating the normal of a line and as such it is a vector. Vectors can be move freely in the space as they have a direction and a length but no start point.
So you would need to displace the normal vector to wherever the point you wish.
Joseph Cheng
Joseph Cheng on 22 May 2015
Edited: Joseph Cheng on 22 May 2015
here is a little thing i threw together during dinner. not the cleanest thing but i think it demonstrates what you're trying to attempt with the BLWALLV vector. drop down to where i define nV which is the normal vector of the side that we've just crossed. I use the function inpolygon to determine if we're outside the defined polygon.
figure(1),hold on
POLY = [1 75; 1 25; 75 5; 175 5; 250 25; 250 75; 175 96; 75 96;1 75];
plot(POLY(:,1),POLY(:,2))
axis([-5 255 0 100]),axis equal
for ind = 1:length(POLY)-1
Edges(ind).side = POLY(ind:ind+1,:);
plot(Edges(ind).side(:,1),Edges(ind).side(:,2),'r-.');
end
%define some parameters
dxdydt = [-20 -5]; %dx/dt and dy/dt
path = [20 50]; %set up origin point
tic; %start timing
time = toc; %get current time
etime = 0; %initialize elapsed time
while time<40 %do this for 40ish seconds
etime = toc-time; %get delta time
time = toc; %set elapse time again
path = [path; path(end,:)+dxdydt*etime]; %propogate position
%determine if we are outside of the bounds.
if ~inpolygon(path(end,1),path(end,2),POLY(:,1),POLY(:,2))
%figure out which edge
for ind = 1:length(Edges)
const1 = polyfit(Edges(ind).side(:,1),Edges(ind).side(:,2),1);
%above defined slope and y intercept.
%use slope and y intercept to see current point is near edge
%need to also set condition for vertical line (slope>>1)
if abs(path(end,2)-(path(end,1)*const1(1)+const1(2)))<.5
break;
elseif abs(const1(1))>1e6 & abs(path(end,1)-Edges(ind).side(1))<.5
break;
end
end
%determine normal vector of current edge.
nV= null(diff(Edges(ind).side))';
%determine reflection of "partical" direction
dxdydt = dxdydt-2*dot(dxdydt,nV)*nV;
%update outside position by the reflection.
%didn't implement to carry current point to polygon edge.
path(end,:)=[path(end-1,:)+dxdydt*etime];
if ~inpolygon(path(end,1),path(end,2),POLY(:,1),POLY(:,2))
disp('stillout');
end
end
plot(path(end,1),path(end,2),'r.')
pause(.001)
end
**note my edge detection is simplistic and due to the discrete changes the point may escape outside the polygon. one improvement could be reduce dxdydt to smaller values and or determine intersect between point current location and last location and edge line.

Sign in to comment.

Categories

Find more on Line Plots 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!