ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【运动学】基于matlab GUI模拟投篮系统(角度+力度可调)【含Matlab源码 1114期】

2021-07-08 20:52:00  阅读:159  来源: 互联网

标签:10 angle ... text GUI 源码 matlab end line


## 一、简介 基于matlab GUI模拟投篮系统(角度+力度可调) ## 二、源代码 ```c %% -------------------- copy right -------------------- % % date : 2021-3-3 % description : several uicontrols and 3D bascket ring, allow to change % view angle freely with four default view angles % %% -------------------- global setting -------------------- clear all; h = figure('numbertitle','off','name','Shoot Backetball 3D'); %% -------------------- axis setting -------------------- axis tight; axis([-10, 10, -10, 10, 0, 5]); %axis off %% -------------------- draw basket -------------------- hold on; r = 2; [a, b, c] = cylinder(r, 30); mesh((a+8), b, (c/4)+2.8) %% -------------------- draw background & wall -------------------- % draw ground ground_x = [-10 10 10 -10]; ground_y = [-10 -10 10 10]; h_ground = fill(ground_x, ground_y, [1 0.4 0]); two_pts_line_width = 2; three_pts_line_width = 3; % draw 2 points and 3 points lines z_line = [0 0]; x_side_line = [-5 10]; x_mid_line = [-5 -5]; y_mid_line = [-3 3]; y_left_line = [-3 -5]; y_right_line = [3 5]; plot3(x_mid_line, y_mid_line, z_line, 'LineWidth', two_pts_line_width, 'Color', 'w'); plot3(x_side_line, y_left_line, z_line, 'LineWidth', two_pts_line_width, 'Color', 'w'); plot3(x_side_line, y_right_line, z_line, 'LineWidth', two_pts_line_width, 'Color', 'w'); t = 0:pi/50:10*pi; line((3*sin(t) - 5), 3*cos(t), 'LineWidth', two_pts_line_width, 'Color', 'w'); line((20*sin(t) + 10), 10*cos(t), 'LineWidth', three_pts_line_width, 'Color', 'w'); % draw board board_x = [0, 0, 0, 0, 0]; board_y = [0, 0, 10, 10, 0]; board_z = [1, 3, 3, 1, 1]; plot3((board_x+10), (board_y-5), (board_z+2), 'LineWidth', 3, 'Color', 'b'); small_board_x = [0, 0, 0, 0, 0]; small_board_y = [0, 0, 4, 4, 0]; small_board_z = [1, 2, 2, 1, 1]; plot3((small_board_x+10), (small_board_y-2), (small_board_z+2), 'LineWidth', 2, 'Color', 'b'); grid on %% -------------------- user interface control -------------------- % close pushbutton uicontrol(h, 'Style', 'pushbutton','Position', [250 0 50 30], 'String', 'Close', ... 'CallBack', 'close', 'BackgroundColor', 'r', 'FontWeight', 'bold'); % height angle slide slide_height_an = uicontrol(h, 'Style', 'slider', 'Position', [10 370 140 20], ... 'Min', 0, 'Max', 90, 'Value', 45, 'BackgroundColor', 'y', ... 'CallBack', 'set(text_height_cur,''String'',num2str(get(slide_height_an,''Value'')))'); text_height_min = uicontrol(h, 'Style', 'text', 'Position', [10 390 40 20], ... 'String', num2str(get(slide_height_an, 'Min'))); text_height_max = uicontrol(h, 'Style', 'text', 'Position', [110 390 40 20], ... 'String', num2str(get(slide_height_an, 'Max'))); text_height_cur = uicontrol(h, 'Style', 'text', 'Position', [60 390 40 20], ... 'String', num2str(get(slide_height_an, 'Value')), ... 'BackgroundColor', 'y', 'FontAngle', 'italic', 'FontWeight', 'bold'); % direction angle slide slide_direct_an = uicontrol(h, 'Style', 'slider', 'Position', [410 370 140 20], ... 'Min', -90, 'Max', 90, 'Value', 0, 'BackgroundColor', 'c', ... 'CallBack', 'set(text_direct_cur,''String'',num2str(get(slide_direct_an,''Value'')))'); text_direct_min = uicontrol(h, 'Style', 'text', 'Position', [410 390 40 20], ... 'String', num2str(get(slide_direct_an, 'Min'))); text_direct_max = uicontrol(h, 'Style', 'text', 'Position', [510 390 40 20], ... 'String', num2str(get(slide_direct_an, 'Max'))); text_direct_cur = uicontrol(h, 'Style', 'text', 'Position', [460 390 40 20], ... 'String', num2str(get(slide_direct_an, 'Value')), ... 'BackgroundColor', 'c', 'FontAngle', 'italic', 'FontWeight', 'bold'); % velocity edit text_v = uicontrol(h, 'Style', 'text', 'Position', [210 390 20 20], 'String', 'm/s'); edit_v = uicontrol(h, 'Style', 'edit', 'Position', [170 390 40 20], 'String', '6', ... 'CallBack', 'get(edit_v, ''String'')', 'BackgroundColor', 'w'); function victory = sun_is_fool (place, height, angle_y, angle_z, velocity, gravity) x = 0:0.01:10; y = 0:0.01:10; z = 0:0.01:10; miss_y = 8; %左右角度误差量 miss_v = 0.25; %速度误差量 miss_z = 10; %上下误差量 victory = 0; angle_z = angle_z + miss_z * (rand - 0.5); angle_y = angle_y + miss_y * (rand - 0.5); velocity = velocity + miss_v * (rand - 0.5); vx = velocity * cosd(angle_y) * cosd(angle_z); vy = velocity * sind(-angle_y) * cosd(angle_z); vz = velocity * sind(angle_z); x(1) = -10; y(1) = place; z(1) = height; % variables for audio playing t_audio = 2; [yy, fs] = audioread('shots.wav'); % end of variables for audio playing for t = 2:500 x(t) = x(t-1) + vx * 0.02; y(t) = y(t-1) + vy * 0.02; z(t) = z(t-1) + vz * 0.02 - 0.5 * gravity * 0.02^2; vz = vz - gravity * 0.02; place = (x(t) - 8)^2 + y(t)^2; if z(t) < 0.4 z(t) = 0.4; vz = - vz * 0.4; if vx ~= 0 vx = vx * 0.4; end if vy ~= 0 vy = vy * 0.4; end end if place < 2 && z(t-1) >= 3.05 &&z(t) <= 3.05 vx = 0; vy = 0; victory = 1; t_audio = t; end if place >= 2 && place <= 9 && z(t-1) >= 3.45 && z(t) <= 3.45; vz = - vz * 0.4; if x(t) <= 9 && x(t) >=7 if vy > 0 vy = vy - rand; elseif vy < 0 vy = vy + rand; end else if vx > 0 vx = vx - rand; elseif vx < 0 vx = vx + rand; end end end if x(t-1) <= 9 && x(t) >= 9 vx = - vx * 0.65; end if abs(y(t-1)) <= 9 && abs(y(t)) >= 9 vy = - vy * 0.65; end end x(501) = -1000; y(501) = -1000; z(501) = -1000; %画球球 [x_b, y_b, z_b] = sphere(30); X_b = x_b + x(1); Y_b = y_b + y(1); Z_b = z_b ./ 2.5 + z(1); h = mesh(X_b, Y_b, Z_b); for k = 2:501 %绘制篮球 X = x_b + x(k); Y = y_b + y(k); Z = z_b ./ 2.5 + z(k); set(h, 'XData', X); set(h, 'YData', Y); set(h, 'ZData', Z); % add audio if (t_audio ~= 2) && (k == t_audio) audioplayer(yy, fs); end % end of audio drawnow; end ``` ## 三、运行结果 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210708203452127.JPG?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01hdGxhYl9UYW5nTWVu,size_16,color_FFFFFF,t_70#pic_center) ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210708203452112.JPG?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01hdGxhYl9UYW5nTWVu,size_16,color_FFFFFF,t_70#pic_center) ## 四、备注 版本:2014a

标签:10,angle,...,text,GUI,源码,matlab,end,line
来源: https://blog.51cto.com/u_15287606/3018979

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有