File Exchange

image thumbnail

Arcball like 3D figure rotation

version 1.0 (2.24 KB) by

Example that shows how to add arcball control to a 3D window

1 Download


View License

One of the major drawbacks of matlab is the lack of a way of changing the viewpoint of a 3D mesh using a natural (arcball) interaction.

In this simple function I demonstrate the arcball interaction allowing a 3D sphere to be moved naturally. Run the function and drag and drop with your mouse on the sphere surface.

A very important aspect that you should note is that I am rotating the ACTUAL 3D object instead of the viewpoint.

There is a specific reason for this choice. The OpenGL lighting is linked to the axis in matlab, thus, if you rotate the veiwpoint, the lighting on the mesh surface wouldn't change. On the other hand, if you rotate your object using hgttransoforms you achieve this result.

It is now apparently bug-free :)

Comments and Ratings (14)

Sandor Toth

Sandor Toth (view profile)

Thanks, this is a really great code!

I never tried to do something like this but I am slightly curious to see it... I might end up doing it for fun sooner or later... If I do, I will let you know


p (view profile)

i need help with something.

i need to create a kind of arcball using images in matlab.
i have images of an object. and i should create a 3D object that the user can spin and see the images like a 3D model. i tried to use the regular arcball, and show my images, but it did'nt work out.

do you have an idea, please?

standard "view" command. But I never liked it, it's very un-natural way of interaction.


p (view profile)

how can i rotate according the viewpoint, and move the camera, and not the object?


p (view profile)

this way i can show more than one image, because you can see few sidelocks at the same view. and i want to create a kind of movie between the views on a object, and create an arcball for object with my images...

See the script. You will have the call to patch() with the sphere with a rectangle and then texture map it.


p (view profile)

but How can I add features that that the arcball have, like move according to a mouse input

With images? you mean rotate an image in 3D? I guess you can just use a simple rectangular mesh (as a Patch object) and texture map it....


p (view profile)

nice work.
do you know how to create an arcball in matlab with images?
i mean that i wan't to create kind of mesh that have the images as vertexs.
tnx :)


The sphere example proposed in the code does not work with Matlab (R14) and (R14). There may be a problem between surf objects and hgtransform. It works properly with mesh and surface objects. Great work, thanks.

Andrea Tagliasacchi

Thanks for the observation anyway. I took a look at the code again and even though Noh2 observation was correct, there was a similar bug. The "rotation direction" was embedded in model space using the inverse of the current rotation matrix (taken from graphic context) and not the one of the original model.

I will upload the final version.
There was also a vector normalization missing that might have caused other issues.

Noone U. Noh2

Actually that is not true. If you check, the rotation is applied to the model which is "saved" in M_previous in the moment the first mouse click take place. That doesn't change anymore during dragging.

Noone U. Noh

The cause of your "bug" is likely that at each step you rotate the object that was rotated in the last step. Each rotation introduces a little bit of round-off errors that are being magnified by doing it a zillion times. Instead, you should always make the rotations with respect to the original (unrotated) object.

MATLAB Release
MATLAB 7.6 (R2008a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video