Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Slide 2 of 14

3 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB  Let’s animate a pool ball on a frictionless table.  Define the Problem in more detail: o Define the ball as a red unit disk o Define the size of the table as a 10x10 square o Provide a random initial velocity o Provide a random initial position o Ball will bounce if it touches a wall  If it hits a horizantal wall the y-velocity changes sign  If it hits a vertical wall the x-velocity changes sign Slide 3 of 14

4 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers  Define the ball as a red unit disk o Use rectangle(…) graphic object to draw the red ball  Define the size of the table as a 10x10 square o Use axis([xmin xmax ymin ymax]) to define the axis limits o Could also use xlim(…) and ylim(…)  Provide a random initial velocity rectangle('Position',[??, ??, 1, 1], 'Curvature',[1,1], 'FaceColor','r'); axis([0 10 0 10]); % Set the initial velocity vel.x = 5*(rand - 0.5); % X & Y initial velocity (m/sec) vel.y = 5*(rand - 0.5); Slide 4 of 14 Diameter = 1

5 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers  Provide a random initial position o Make sure that the ball is on the table!!!  Set an animation time step  Update the position at each time step % Set the initial position (center of the ball) pos.x = 0.5+9*rand; % X & Y initial position (m) pos.y = 0.5+9*rand; dT = 0.1; % Animation time step (sec) pos.x = pos.x + vel.x * dT; % X position update pos.y = pos.y + vel.y * dT; % Y position update Slide 5 of 14

6 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers  Ball will bounce if it touches a wall o If it hits a horizantal wall the y-velocity changes sign o If it hits a vertical wall the x-velocity changes sign  Radius of ball is 0.5 !! % Check for bouncing off a wall if pos.y > 9.5 % Hit the top wall vel.y = -vel.y; elseif pos.y < 0.5 % Hit the bottom wall vel.y = -vel.y; end if pos.x > 9.5 % Hit the right wall vel.x = -vel.x; elseif pos.x < 0.5 % Hit the left wall vel.x = -vel.x; end Slide 6 of 14

7 3D animation in MATLAB First Attempt Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers % Set the initial velocity vel.x = 5*(rand - 0.5); % X & Y initial velocity (m/sec) vel.y = 5*(rand - 0.5); % Set the initial position (center of the ball) pos.x = 0.5+9*rand; % X & Y initial position (m) pos.y = 0.5+9*rand; dT = 0.1; % Animation time step (sec) % Animate the pool ball moving while 1 % Run forever % create a circle using rectangle function & handle to object rectangle('Position',[pos.x-0.5, pos.y-0.5, 1, 1],'Curvature',[1,1], 'FaceColor','r'); axis([0 10 0 10]); pause(dT); % Pause for dT seconds pos.x = pos.x + vel.x * dT; % X position update pos.y = pos.y + vel.y * dT; % Y position update % Check for bouncing off a wall if pos.y > 9.5 % Hit the top wall vel.y = -vel.y; elseif pos.y < 0.5 % Hit the bottom wall vel.y = -vel.y; end if pos.x > 9.5 % Hit the right wall vel.x = -vel.x; elseif pos.x < 0.5 % Hit the left wall vel.x = -vel.x; end Slide 7 of 14 Why not use else here?

8 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Result Slide 8 of 14

9 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Updates:  Only plot the current ball o METHOD#1: Call rectangle once and update the [x, y] position data of the rectangle, or o METHOD#2: Turn the visibility on and off  Reduce the speed with each bounce (simulate friction) o vel = 0.9 * vel;  Exit when the speed drops too low o While |vel| > 0.5, continue Slide 9 of 14

10 3D animation in MATLAB METHOD#1 – Update ‘Position’ Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers % Set the initial velocity vel.x = 5*(rand - 0.5); % X & Y initial velocity (m/sec) vel.y = 5*(rand - 0.5); % Set the initial position (center of the ball) pos.x = 0.5+9*rand; % X & Y initial position (m) pos.y = 0.5+9*rand; dT = 0.1; % Animation time step (sec) h_rect = rectangle('Position',[pos.x-0.5, pos.y-0.5, 1, 1],'Curvature',[1,1], 'FaceColor','r'); axis([0 10 0 10]); % Animate the pool ball moving while (vel.x^2 + vel.y^2) > 0.5 % Exit when the speed drops too low % create a circle using rectangle function & handle to object pause(dT); % Pause for dT seconds pos.x = pos.x + vel.x * dT; % X position update pos.y = pos.y + vel.y * dT; % Y position update % Check for bouncing off a wall if (pos.y > 9.5) | (pos.y < 0.5) % Hit the top or bottom wall vel.y = -vel.y*0.9; end if (pos.x > 9.5) | (pos.x < 0.5) % Hit the right or left wall vel.x = -vel.x*0.9; end set(h_rect, 'Position', [pos.x-0.5, pos.y-0.5, 1, 1]); end Slide 10 of 14

11 3D animation in MATLAB METHOD#2 – Update ‘Visibility’ Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Slide 11 of 14 % Set the initial velocity vel.x = 5*(rand - 0.5); % X & Y initial velocity (m/sec) vel.y = 5*(rand - 0.5); % Set the initial position (center of the ball) pos.x = 0.5+9*rand; % X & Y initial position (m) pos.y = 0.5+9*rand; dT = 0.1; % Animation time step (sec) % Animate the pool ball moving while (vel.x^2 + vel.y^2) > 0.5 % Exit when the speed drops too low % create a circle using rectangle function & handle to object h_rect = rectangle('Position',[pos.x-0.5, pos.y-0.5, 1, 1],'Curvature',[1,1],'FaceColor','r'); axis([0 10 0 10]); pause(dT); % Pause for dT seconds pos.x = pos.x + vel.x * dT; % X position update pos.y = pos.y + vel.y * dT; % Y position update % Check for bouncing off a wall if (pos.y > 9.5) | (pos.y < 0.5) % Hit the top or bottom wall vel.y = -vel.y*0.9; end if (pos.x > 9.5) | (pos.x < 0.5) % Hit the right or left wall vel.x = -vel.x*0.9; end set(h_rect, 'Visible', 'off'); end

12 3D animation in MATLAB Save the animation to a movie Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Use the MATLAB VideoWriter to save frames to a movie  VideoWriter(filename, profile) 1.First create a writerObject and set file properties 2.Open the file 3.Capture the frames 4.Close the file % Create file to capture video & set file properties writerObj = VideoWriter('bouncing_ball.mp4','MPEG-4'); writerObj.Quality = 90; writerObj.FrameRate = 5; open(writerObj);... frame = getframe; writeVideo(writerObj,frame);... close(writerObj); % Close file after capturing video Slide 12 of 14

13 3D animation in MATLAB Save the animation to a movie Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Video: Slide 13 of 14 3D Version of the Bouncing ball

14 Next Lecture Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers Graphical User Interface (GUI) Design in MATLAB Slide 14 of 14


Download ppt "EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google