2012-01-04 120 views
2

我有一個以球軸爲原點的球體。我使用rotate3d函數來允許其旋轉。但是,當我旋轉它時,它似乎在具有旋轉的固定點的空間中移動。我想將原點固定爲旋轉中心。我怎樣才能實現它?在Matlab中圍繞固定點的3D球體旋轉

這裏是我的代碼:

function ex 
global state; 
fh = figure('Menu','none','Toolbar','none','Units','characters',... 
    'Renderer','OpenGL'); 
hPanAni = uipanel('parent',fh,'Units','characters','Position',... 
    [22.6 10.4 53 23],'title','Controls','FontSize',11,... 
    'FontAngle','italic','FontWeight','bold'); 
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',... 
    'Position',[0.14 0.75 0.5 0.12],'String','Spin',... 
    'FontSize',10,'Callback',@hIniAniCallback); 
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',... 
    'Position',[0.14 0.5 0.5 0.12],'String','Stop',... 
    'FontSize',10,'Callback',@hFinAniCallback); 
hResetAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',... 
    'Position',[0.14 0.25 0.5 0.12],'String','Reset',... 
    'FontSize',10,'Callback',@hResetAniCallback); 
hPantSim = uipanel('Parent',fh,'Units','characters',... 
    'Position',[107.87 8 157.447 42],'BorderType','none','title',... 
    'Screen','FontSize',11,'FontAngle','italic',... 
    'FontWeight','bold'); 
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',... 
    [0 0 1 1],'BorderType','line','BackgroundColor','k'); 
axes('units','normalized','position',[0,0,1,1],'Parent',... 
    hPantSimInt); 
stars = rand(60,2); 
scatter(stars(:,1),stars(:,2),6,'y','Marker','+'); 
axis off; 
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',... 
    [0 0 1 1],'Color','none','Visible','off','DataAspectRatio',... 
    [1 1 1],'NextPlot','add'); 
T1 = 0:pi/1000:2*pi; 
Fin = numel(T1); 
if (Fin>1000) 
    Incr = floor(Fin/1000); 
else 
    Incr = 1; 
end 
Y = zeros(numel(T1),3); 
Y(:,1) = 7000*cos(T1); 
Y(:,2) = 7000*sin(T1); 
R_esf = 6378; 
[x_esf,y_esf,z_esf] = sphere(50); 
x_esf = R_esf*x_esf; 
y_esf = R_esf*y_esf; 
z_esf = R_esf*z_esf; 
props.FaceColor= 'texture'; 
props.EdgeColor = 'none'; 
props.Parent = ah4; 
surface(x_esf,y_esf,z_esf,props); 
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),... 
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b'); 
line([0 1.5*R_esf],[0 0],[0 0],'LineWidth',3,'Color','g'); 
line([0 0],[0 1.5*R_esf],[0 0],'LineWidth',3,'Color','g'); 
line([0 0],[0 0],[0 1.5*R_esf],'LineWidth',3,'Color','g'); 
pbaspect([1 1 1]); 
axis vis3d; 
rotate3d(ah4); 
view([atan2(Y(1,2),Y(1,1)),0]); 
az = 0; 
k = 2; 
ind_ini = 0; 
state = 0; 
     function hIniAniCallback(hObject,evt) 
      tic; 
      if (ind_ini == 1) 
       return; 
      end 
      ind_ini = 1; 
      state = 0; 
      while (k<=Fin) 
      set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3)); 
      pause(0.002); 
      if (k == Fin) 
       toc; 
      end 
      k = k + Incr; 

      if (state == 1) 
       state = 0; 
       break; 
      end 
      end 
     end 

    function hFinAniCallback(hObject,evt) 
     ind_ini = 0; 
     state = 1; 
    end 
function hResetAniCallback(hObject,evt) 
    set([handles.psat],'Visible','off'); 
    ind_ini = 0; 
    state = 1; 
    az = 0; 
    k = 2; 
    handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),... 
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b'); 
end 

end 
+1

你能澄清一下你的問題嗎?目前尚不清楚你目前所描述的行爲是什麼,以及你希望取而代之的是什麼。 – 2012-01-04 23:07:35

+0

我同意。也許你應該展示一些圖片。 – Oli 2012-01-04 23:08:59

+0

謝謝你的評論。我剛剛添加了顯示我的問題的代碼。如果你用鼠標旋轉球體,你會看到它隨着它的移動而移動,甚至它看起來離開了屏幕窗口。我希望當我旋轉球體時,這看起來像是固定的,剩下的物體在它周圍移動。 – julian 2012-01-05 12:16:58

回答

1

的問題是,3D旋轉相對於做你的軸的中心,而不是相對於原點。在軸上添加綠線之後,x,y,z軸的極限會自動更改,球體的中心不再位於圖形的中心。繪製所有行後,添加以下行:

ax_limits = 2*[-R_esf R_esf]; 
set (ah4, 'xlim', ax_limits, 'ylim', ax_limits, 'zlim', ax_limits) 

'2'因子只是爲了防止球體緊緊地填滿你的軸。您可以將其設置爲您需要的任何值。

+0

非常感謝您的回答。 – julian 2012-01-05 12:20:33