Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an image to an animation in matlab

I created a 3D stick man walking in matlab and I want to add an image as the floor that he is walking on. I'm not sure how to go about doing this. I looked at an example of someone adding an image as a background. That is kinda what I want but I want it to appear as the floor. I going to give the stick man a trajectory and make him walk across the floor. Can anyone point me in the right direction.

Ok now I got it thanks to Andrey.

clear all

cyl = UnitCylinder(2);
sph = UnitSphere(2);

% Head 
L1 = 2;

Head = translate(scale(sph,L1/2, L1/2, L1/2),0,0,L1+4.5);
Head.facecolor = 'yellow';

%Shoulder 
r2 = 0.3;
L2 = 3;
Shoulder = translate(rotateX(scale(cyl,r2/2,r2/2,L2/2),90),0,0,5);
Shoulder.facecolor = 'red';


%Left Upper Arm
w1_s = [-20:4:20 20:-4:-20];
r3 = 0.3;
L3 = 2;
Upper_Arm_left = translate(scale(cyl,r3/2,r3/2,L3/2),0,0,-L3/2);
Upper_Arm_left.facecolor = 'red';

%Right Upper Arm
Upper_Arm_right = translate(scale(cyl,r3/2,r3/2,L3/2),0,0,-L3/2);
Upper_Arm_right.facecolor = 'red';

%Left Forearm
w2_s = [-5:1:5 5:-1:-5];
L3_f = 2.5;
Fore_Arm_left = translate(scale(cyl,r3/2,r3/2,L3_f/2),0,0,-L3_f/2);
Fore_Arm_left.facecolor = 'red';

%Right Forearm
Fore_Arm_right = translate(scale(cyl,r3/2,r3/2,L3_f/2),0,0,-L3_f/2);
Fore_Arm_right.facecolor = 'red';


%Chest
r4 = 2;
L4 = 2;
Chest = translate(scale(cyl,r4/2,r4/2,L4/2),0, 0, 5-L4/2);
Chest.facecolor = 'yellow';

%Weist
r5 = 1;
L5 = 2;
Weist = translate(scale(cyl,r5/2,r5/2,L5/2),0, 0, 5-L4-L5/2);
Weist.facecolor = 'yellow';

%Hip
L6 = 1.5;
Hip = translate(rotateX(scale(cyl,r2/2,r2/2,L6/2),90),0,0,5-L4-L5-r2/2);
Hip.facecolor = 'green';

%Left Upper Leg
r7 = 0.4;
L7 = 2.5;
L71 = (L6/2+r7/2);
L72 = 5-L7/2-L4-L5;
Upper_Leg_left = translate(scale(cyl,r7/2,r7/2,L7/2),0,0,-L7/2);
Upper_Leg_left.facecolor = 'green';

%Right Upper Leg
Upper_Leg_right = translate(scale(cyl,r7/2,r7/2,L7/2),0,0,-L7/2);
Upper_Leg_right.facecolor = 'green';

%Left Lower Leg
L7_f = 3;
Lower_Leg_left = translate(scale(cyl,r7/2,r7/2,L7_f/2),0,0,-L7_f/2);
Lower_Leg_left.facecolor = 'green';

%Right Lower Leg
Lower_Leg_right = translate(scale(cyl,r7/2,r7/2,L7_f/2),0,0,-L7_f/2);
Lower_Leg_right.facecolor = 'green';

angle1 = 0;
angle2 = 0;

for i = 1:120

    angle1 = w1_s(rem(i,length(w1_s))+1);
    angle2 = w2_s(rem(i,length(w1_s))+1);    

    Arm_left = combine(translate(rotateY(Fore_Arm_left,angle2),0,0,-L3), Upper_Arm_left);
    Arm_right = combine(translate(rotateY(Fore_Arm_right,-angle2),0,0,-L3), Upper_Arm_right);

    Arm_left = translate(rotateY(Arm_left,angle1),0,-L2/2,(5-L3/2)+L3/2);
    Arm_right = translate(rotateY(Arm_right,-angle1),0,L2/2,(5-L3/2)+L3/2);

    Leg_left = combine(translate(rotateY(Lower_Leg_left,-angle2),0,0,-L7), Upper_Leg_left);
    Leg_right = combine(translate(rotateY(Lower_Leg_right,angle2),0,0,-L7), Upper_Leg_right);

    Leg_left = translate(rotateY(Leg_left,-angle1),0,-L71,L72+L7/2);
    Leg_right = translate(rotateY(Leg_right,angle1),0,L71,L72+L7/2);

    Upper_Body = combine(Head, Shoulder, Arm_left, Arm_right, Chest, Weist);
    Lower_Body = combine(Hip, Leg_left, Leg_right);
    walker  = combine(Upper_Body, Lower_Body); 

    cla
    img = imread('peppers.png');
    [X,Y] = ndgrid([-10 10],[-10 10]);
    zImage = [-5 -5; -5 -5];
    surf(X,Y,zImage,'CData',img,'FaceColor','texturemap');
    %     view([1 1 1]);
    hold on
    view(3)
    set(gca,'xlim',[-10 10],'ylim',[-10 10],'zlim',[-6 6]);
    renderpatch(walker);
    camlight
    box on
    drawnow
    pause(0.04)

end

I want to get something like this floor but I don't know how to do it. This person used a .fig in their code.This is a piece of what they used.

F1 = open('background1.fig');
background_gca = gca;
F2 = figure(2);
.
.
.
 clf(F2)
    copyobj(background_gca,F2);
    view([-40,25])
    set(gca,'xlim',[-5 5],'ylim',[-5 5],'zlim',[0 10]);
    renderpatch(walker);
    camlight
    box on
%     axis off
    drawnow; 

example of the how I want mine to look

like image 986
oldbutnew Avatar asked Dec 07 '25 13:12

oldbutnew


1 Answers

You had a small syntax error:

First, you reversed z and y :

surf(xImage,zImage,yImage,...)

Also, you should think about surf as a surface. Thus, all z should be 0

img = imread('peppers.png');
[X,Y] = ndgrid([-5 5],[-5 5]);
zImage = [0 0; 0 0];
surf(X,Y,zImage,'CData',img,'FaceColor','texturemap');
view([1 1 1]);

enter image description here

like image 176
Andrey Rubshtein Avatar answered Dec 10 '25 22:12

Andrey Rubshtein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!