Reputation: 3
I'm trying to create a cloth simulator and need a way of storing the particle positions. I would like to store them as [x,y,z]. I need an array for the all the particle positions. This would mean having an array [[x1,y1,z1],[x2,y2,z2],...etc].
My width and height are both 3, so there should be 9 [x,y,z]'s in the grid. However my output shows 100s of positions. I don't really know what it is I'm doing wrong. Sorry if this question could be formatted better.
for i = 1:particleWidth
for j = 1:particleHeight
X = (width*(i/particleWidth));
Y = (height*(j/particleHeight));
xPos = [xPos,X];
yPos = [yPos,Y];
end
end
[T1,T2,T3] = ndgrid(xPos,yPos,Z);
grid = [T1(:),T2(:),T3(:)];
disp(grid);
Upvotes: 0
Views: 35
Reputation: 1580
ndgrid
replicates the inputs in order to create a grid.
[X,Y]=ndgrid(1:3,4:6)
X =
1 1 1
2 2 2
3 3 3
Y =
4 5 6
4 5 6
4 5 6
If want to use that function, you should initialize xPos
and yPos
as vectors :
xPos = (width/particleWidth).*(1:particleWidth);
yPos = (height/particleHeight).*(1:particleHeight);
[T1,T2] = ndgrid(xPos,yPos); %T1 and T2 will be width-by-height arrays
grid = [T1(:),T2(:),zeros(numel(T1),1)]; % Or whatever Z should be
Basically, you had already created xPos
and yPos
as arrays with width x height entries, so you get at least the square of that number out of ndgrid
. If Z
also have 9 elements, that would make 9^3 = 729 rows out.
Upvotes: 1