Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hgtransform, makehgtform: rotate about a point then translate

863 views
Skip to first unread message

John Private

unread,
May 6, 2009, 6:02:02 PM5/6/09
to
I have created a rectangle using plot3 and made it a child of hgtransform which I am using to move my rectangle around in 3-space. Basically I want to freely move my object around in 3 space using the up/down/left/right keyboard arrows.

There are two transformations that I want to do but am unclear on how to do.

1. rotate about an arbitrary point. I always want to rotate about the center of my object. I know about hgtransforms axistranslate([x y z], t), but how do I determine the midpoint of my object? I guess what I'm asking is how can I get the vertex data of my object to calculate the midpoint and thus pass that as parameters to axistranslate? Or is there an easier way?

2. I want to always translate in the direction my object is facing. So, if I rotate my object by angle theta about its center, I then want to translate it in the direction (tan(theta)/y, x*tan(theta)). Is this correct?

I'd really appreciate it if someone could help me out here.

-John

Michael Garrity

unread,
May 8, 2009, 9:09:20 AM5/8/09
to
>"John Private" <jmljunior...@gmail.com> wrote in message news:gtt1cq$ihs$1...@fred.mathworks.com...

>I have created a rectangle using plot3 and made it a child of hgtransform which I am using to move my rectangle
> around in 3-space. Basically I want to freely move my object around in 3 space using the up/down/left/right
> keyboard arrows.
>
> There are two transformations that I want to do but am unclear on how to do.
>
> 1. rotate about an arbitrary point. I always want to rotate about the center of my object. I know about
> hgtransforms axistranslate([x y z], t), but how do I determine the midpoint of my object?

Perhaps you're thinking of the axisrotate option to makehgtform? Help says this:

>> help makehgtform
MAKEHGTFORM Make a 4x4 transform matrix.
...
M = MAKEHGTFORM('axisrotate',[ax ay az],t) Rotate around axis
[ax ay az] by t radians.

You're right, that could be interpreted as rotating around a line, but it can't be because the three
values aren't sufficient to describe a line. What it is actually doing is rotating around a line that
passes through [0 0 0] in the direction [ax ay az]. To do what you're trying to do, you can give
makehgtform multiple inputs. Like this:

xlim([-1 2]);
ylim([-1 2]);
center = [1 1 0];
g=hgtransform
p=patch('Parent',g)
for ang=linspace(-pi/3,pi/3,40)
set(g,'Matrix',makehgtform('translate',center,'zrotate',ang,'translate',-center));
drawnow
end

What this is doing is combining 3 transformations. So first it translates by -center.
That is to say, it translates center to the origin. Then it rotates about the origin. Then it
translates by center. That moves the object back to its original location, but in the
rotated coordinate system.

You can get the same effect by combining multiple hgtransform objects that each
have one of the three components. Perhaps this view makes it easier to understand
what's going on.

xlim([-1 2]);
ylim([-1 2]);
center = [1 1 0];
g1=hgtransform;
g2=hgtransform('Parent',g1);
g3=hgtransform('Parent',g2);
p=patch('Parent',g3);
set(g3,'Matrix',makehgtform('translate',-center));
set(g1,'Matrix',makehgtform('translate',center));
for ang=linspace(-pi/3,pi/3,40)
set(g2,'Matrix',makehgtform('zrotate',ang));
drawnow;
end

> I guess what
> I'm asking is how can I get the vertex data of my object to calculate the midpoint and thus pass that as
> parameters to axistranslate? Or is there an easier way?
>

This step would look something like this:

center = [mean(get(p,'XData')) mean(get(p,'YData')) 0]

> 2. I want to always translate in the direction my object is facing. So, if I rotate my object by angle
> theta about its center, I then want to translate it in the direction (tan(theta)/y, x*tan(theta)). Is this
> correct?
>

I don't have a clear picture of your geometry, but I would expect it to look
like that.


> I'd really appreciate it if someone could help me out here.
>
> -John

Does that help?

-Mike Garrity
-The MathWorks


0 new messages