How can I plot a 3D parabolic trajectory?

36 views (last 30 days)
Martin
Martin on 8 Jun 2025 at 17:31
Edited: Cris LaPierre on 10 Jun 2025 at 13:26
Contextualizando, tengo que hacer un codigo que simule un conecta 4, donde el cubo aterrice con movimiento parabolico en la posición designada por el usuario que serian las columnas del sector derecho
Más especificamente mi problema de codigo es en la parte de - Tiro parabolico - ya que no se como hacer que desde una posición al azar y un agujero de muralla al azar, se grafique el plot de trayectoria y que después ademas se elimine para cada turno.
To put it in context, I need to create code that simulates a Connect 4 game, where the cube lands with a parabolic motion in the position designated by the user, which would be the columns of the right sector.
More specifically, my code problem is in the "Parabolic Shot" part, since I don't know how to make the trajectory plot appear from a random position and a random hole in the wall, and then delete it for each turn.
%Grafico sin malla
figure('Name','Conecta 4','NumberTitle','off')
hold on;
axis equal;
xlabel('Columnas','Color','b')
ylabel('Profundidad','Color','b')
zlabel('Filas','Color','b')
xlim([-8,7])
ylim([0,1])
zlim([0,7])
view(25,25)
%Malla
%Lineas verticales
for c=0:7
plot3([c,c],[0,0],[0,6],'Color','k')
end
for c=0:7
plot3([c,c],[1,1],[0,6],'Color','k')
end
%Lineas horizontales
for a=0:6
plot3([0,8],[0,0],[a,a],'Color','k')
end
for a=0:6
plot3([0,8],[1,1],[a,a],'Color','k')
end
%Lineas de profundidad
for a=0:6
for c=0:7
plot3([c,c],[0,1],[a,a],'Color','k')
end
end
%Número de columnas
for c = 1:7
text(c-0.5, 0, 0, num2str(c),'HorizontalAlignment','center', 'FontSize',15, 'Color','b');
end
%Funcion dibujar cubo
function dibujar_cubo(x1, y1, color,transparencia)
x = x1 - 0.5;
z = 6-y1 - 0.5;
y = 0.5;
v = [-1 -1 -1; 1 -1 -1; 1 1 -1; -1 1 -1; -1 -1 1; 1 -1 1; 1 1 1; -1 1 1];
v = v * 0.4 + [x y z];
f = [1 2 3 4; 5 6 7 8; 1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8];
patch('Vertices', v, 'Faces', f, 'FaceColor', color, 'FaceAlpha',transparencia,'EdgeColor', 'none')
end
%Columnas disponibles
juego = zeros(6,7);
columnas_disponibles = find(juego(1,:) == 0);
% Solicitar al usuario que elija una columna válida
%- - - Tiro parabolico - - -
x1=randi(5);
dibujar_cubo(-7,x1, 'r',0.2)
x=randi([x1,5]);
v1=[-5,0,0;-5,0,x;-5,1,x;-5,1,0];
f = [1 2 3 4];
patch('vertices',v1,'faces',f,'FaceColor','r')
v1=[-5,0,x+1;-5,0,7;-5,1,7;-5,1,x+1];
f = [1 2 3 4];
patch('vertices',v1,'faces',f,'FaceColor','r')
turnoJugador=true
tablero=zeros(6,7);
while true
if all(tablero(1,:) ~= 0), disp('Empate.'); break; end
if turnoJugador
columnas_disp = find(tablero(1,:) == 0);
fprintf('Columnas disponibles: %s\n', mat2str(columnas_disp));
col = input('Elija una columna (1-7): ');
if ~ismember(col, columnas_disp)
disp('Columna inválida.'); continue;
end
for f=6:-1:1
if tablero(f,col)==0, break; end
end
xEnd = col; yEnd = 2; zEnd = f-1;
dibujar_cubo(xEnd, zEnd, 'r', 1);
tablero(f,col) = 1;
a1=randi([0,7])
dibujar_cubo(xEnd, zEnd, 'r', 1);
end
end
  1 Comment
Cris LaPierre
Cris LaPierre on 8 Jun 2025 at 18:14
Edited: Cris LaPierre on 10 Jun 2025 at 13:26
You need to create a vector of (x,y,z) positions that capture the position of the cube as it moves from the start to end position. Once you have those, you then need to create an animation by plotting the cube as it moves from the start to end position.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 10 Jun 2025 at 3:55
For what it's worth, see my attached demo where I compute just about everything you could want to know about a projectile and it's trajectory. All starting parameters (height, speed, angle, etc.) are variables you can modify. Perhaps you can adapt it.

Community Treasure Hunt

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

Start Hunting!