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

2021/7/23 11:06:46

本文主要是介绍【运动学】基于matlab GUI模拟投篮系统(角度+力度可调)【含Matlab源码 1114期】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、简介

基于matlab GUI模拟投篮系统(角度+力度可调)

二、源代码

%% -------------------- 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

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a



这篇关于【运动学】基于matlab GUI模拟投篮系统(角度+力度可调)【含Matlab源码 1114期】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程