### Draw a cylinder between two points (multiple times per frame)

So I’m working with OSCeleton and trying to make a very rough 3d volume of the body. I’m not particularly hot at low-level 3d or maths, which this seems to require in abundance.

So far I have a system that takes two points – say the right hand [A] and right elbow [B]. It finds the mid-point between the two, and sets that as the draw position. I then minus A from B to find the relative distance in cartesian space, then I run a 3dcartopol object on it to extract the azimuth, elevation and distance. I feed all those numbers to a sketch object in the hope it will draw a cylinder at that position and orientation, using these gl commands:

```
reset
glclearcolor 0. 0. 0. 1.
glclear
glrotate $6 1 0 0
glrotate $7 0 1 0
moveto $1 $2 $3
cylinder $4 $4 $5
glcolor 0.5 0.5 0.5 1.
//glbindtexture $9
```

I see a cylinder that acts roughly right, but often does strange things like flipping around its axis all of a sudden, or (as the user moves right to left) starting at the right of the screen, tracking them to the center, then returning to the right.

Trying to use multiple cylinders results in a spastically twitching, flickering heap. I’m way, way, way over my head already (and frankly surpised theres anything on screen). So I thought I’d ask if there was anything wrong with the basic concept or openGL before getting into the specifics of the patch.

I’ve read the relavent redbook sections, but nothing seems to be helping (the current gl commands are just one configuration of many I’ve tried that all draw, but dont achieve the result). I’ve pushed and popped matrices but it didn’t seem to achieve much so I removed those lines.

For anyone curious, the 3dcartopol object I used was http://www.maxobjects.com/?v=objects&id_objet=776&PHPSESSID=ad2a22436728f65b8dc4b138b589fdde . before that I was using my own system with a series of Max’s own cartopol objects, but it got my head in a mess trying to figure out how the 3 values cancelled each other out into only 2 rotations and 1 distance.

I’m also currently playing with cosm.nav but I can’t seem to get anything out of it yet (it requiring yet another mathematical process to get 4(?) rotational values). http://www.allosphere.ucsb.edu/cosm/index.html

i would start with simple spheres at the joint positions, and a simple line connecting the joints.

if that looks as expected, you can move on to more complicated geometry.

Thanks for the suggestion, but that was a step I passed through on the way to this patch (using circles rather than spheres).

Unfortunately lines are far simpler to define; using a start-point and end-point, I guess since the ‘roll’ axis doesn’t matter on whats essentially a 2d object. Adding the full number of axes of rotation is whats causing the brainial short-circuit.

I’ve come back to this problem months later, and have the rotation a bit more figured out, but I’m still stuck getting the right things on-screen.

At the moment the main issue seems to be the translation (although my test situation is simple enough that it could be hiding underlying rotation problems)

For each cylinder, the following runs:

glmatrixmode modelview [doesn't seem to make any difference]

glpushmatrix

gltranslate $1 $2 $3 [mid-point between 2 coordinates]

glcolor 0.7 0.7 0.7 1.

glrotate $4 0 1 0 [Y-axis rotation from polar coordinates]

glrotate $5 1 0 0 [X-axis rotation from polar coordinates]

cylinder $7 $7 $6 [radius of each end and length from polar distance]

glpopmatrix

Then after all the cylinders, before the frame is rendered:

glclear

reset

I have created a simple test situation, with points at

0,0,1

0,1,0

1,0,0

0,0,0,

I join them into 4 pairs, and find the polar coordinates. This should result in a neatly-aligned right-angle triangle on the positive XY plane at 0, and a line stretching down the Z axis to 1.

Instead I see:

Now the rotation I suspect I can fix later by flipping some numbers/coordinates, but I can’t figure out whats happening with the points where the cylinders intersect.

I think there might be some ‘drift’ coming from each translation, but I don’t know how. Theres a command I think is Max MSP only (moveto) I can use instead of translate, but it results in a similar situation (only with everything closer together).

The ‘z’ axis cylinder appears to be in a weird spot that is level with the midpoint of the hypoteneuse.

Thats essentially a summary of the problem, but I’ll include some more information for anyone who’s curious:

The coordinates are as follows:

01 = 0.000 0.100 0.000

02 = 0.000 0.000 0.000

03 = 0.100 0.000 0.000

04 = 0.000 0.000 0.100

paired by this list (the intial 0s on each are just because they’re stored in a 2d matrix)

0 1 0 2,

0 2 0 3,

0 2 0 4,

0 1 0 3

Run through a cartesian to polar coordinates object (output example):

90. -0. 0.1 [azimuth, elevation, distance]

which is fed into the gl commands above.

Never mind – I found I had connected a vexpr wrong so it was running before it received its righthand input, causing the slight misalignment.

Now I’m on the next issue, which is how to keep a texture on a cylinder facing forwards, when I cant really see how to lock the rotation along the axis of the cylinder. As it is, when nearly vertical the rotation slides around a lot (invisibly, unless there is a texture on it to watch).

Edit: after some sketching I’ve realised there’s no ‘correct’rotation when you’re joining two points as theres no information included about the orientation of the object between them. There will always be one axis where things get a bit crazy when the cylinder is paralell to it. I’m now thinking how to choose that axis (at the moment its Y, but I’d prefer Z)

Final edit: I flipped the coordinates system on its back just before running it through the polar coordinates, opengl translation etc. x = x , y = -z, and z = y. Then at the end of the sketch commands (after the last popmatrix) I flip the world back up again with a glrotate -90 1 0 0. This means the crazy rotation will happen when the cylinders are close to pointing at the camera, minimizing the visual effect.

Hey Bferns,

I’m dealing with a similar patch right now and surprised at my inability to successfully solve this seemingly simple problem,

any chance you could share your patch we can all(me) learn from?

greatly appreciated

Jeremy

never mind, mostly figured out, I’ll post my patch shortly

hmmm so I thought I figured it all out but I’m unable to get the angle working right when z values are between -1.0 and 1.0

any ideas?

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
4006.3oc6cssbahjF9ZmmhtTkK1CNZoOwgs1ZqMY18hb2V0T0bSlToPRsjYB
BzBX63L07NsOC6S1B8eiDXKfFAB21ipjx1.RvW+0+m5e5t++02b0rEweSjNC
8WQeBc0U+5at5J4oJNwUpiuZ1V+usLzOU9wlsLd6VQT1rqgqkI9Vl77+63fn
LzGJOezsaChBEYxuDVcx0wQYoAeWHOGYt0gOa7sYke3xytKQjl+f7yBhi9Rh
XYFfRFml+EQtVE+r7GnOW4ID4uU9Dl89j.+vR.syOa4MAQapbq3Lbw2Gicj+
hKuajp2tfUxaT7he4cX7rhy8au4ME+35Qgsd+3wV8pgi4NUZ31rVa2VmR6dq
HM0ei3Is6h9zri1pI8oUWRQvoxdXm.ZXylcZRBDNufC3VtE+hBri6wID2SgO
hD2m+ceJcfV3Gs4n7g0yIePAkL6hmWtFWaxGjQjNRalNN+JELYuNwS1ZUGzj
NwH1l24+UDdNhLGQmiXyQ74H64GkBreV0Pb7j7BlBVNZ0Vo8XJTH91tDze3s
qI+42tF+G+Kj4lm4ClGHtXK4HOu13Ft2uu3FphNzhab9cF2XIMvRbrzfarGS
+vaBWFGFmfvHq7VEZa7chrXzawn2RPukdMJc2MhDQQnU7CWkgdKG8V6Gc0vf
HwWtO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I2RDCdLXVtikGCttdLvm.Xki01l
aM2wxxBSTmZfH9QnoAIBqI0nzi7opyV+7zfoNkCm1Q3xzUYtGFCodfx.jpaJ
iNGmKsYYqN0DfXdOc1ThXKoESJFu+nI.rL1IBVPYFS1ez.AKUq.dlTwS54Hz
BFFFHuCqZnEEGMzjy3bNf6PAk1p3FFl318yIWwj2PoIHW6U38GMEnUpESOAz
xfpJuybq7HdbrUmZnPlpamdeHXWdoc8Bqj4CRdeTDigxCU2nzNAHCF1KVXri
jgcsPqTlfcBnEjIr4GhBdLjIH5JSLci6WKLw85WmNkVsSm6ZOZc5Dc6z4m.Z
YvTOit+nglXdsFWdOcFUrJQJh5PsX4pbzPQK6LfVBgclPq7kdz43KHl3ahg3
7LfJ7XiJ3sGqlvlPxMjGLXzp2K3fOwbndABylXTYoMpvSHpzATCgoxO32dy+
GuGIQnA
-----------end_max5_patcher-----------
```

FYI I finally made some progress… I got orientation to work on the elbow joint

I used the rotation matrix values out of OSCeleton (just one actually Z1) with the equation thetaY = asin(Z1) (oscelton gives you a rotation matrix X1 X2 X3, Y1 Y2 Y3, Z1 Z2 Z3)

and then 2d cartesian trig i’ve used before (atan2((y2-y1)(x2-x1)) for the missing axis… the x axis is fixed/set to 90 degrees. (the final product of those 2 equations also have to be converted from radians to degrees, use n*180 /3.14)

so shapeorient X,Y,Z

becomes…. shapeorient 90, (atan2((y2-y1)(x2-x1))*180/31.4), (asin(Z1)*180/31.4)

anyway this works for my arm test case right now, I’ll update as I get the rest of the body online.

- technically the matrix should work without my point trig above (using some other math I found here < https://groups.google.com/group/openni-dev/browse_frm/thread/ccb8e62acd17b95b/e61fd527cf1394e3?lnk=gst&q=orientation#e61fd527cf1394e3>, but it wasn’t working)

DISCLAIMER I’m not a math guy, I’m just figuring this out as I go along

Hi Jeremy,

Sorry I couldn’t help initially – I only just figured out you can get email notifications on threads here. In the first patch you posted you had the third outlet of 3dcartopol hooked up as a rotation on the z axis, whereas it actually outputs the distance between the points – this might have cause some of the issues.

You started with the same problem I did with the shapes flipping when crossing one axis, due to only having 2 axes of rotation info. I stopped there because I’ve been working on an AR project since, but I did start looking into cross products and dot products, which seemed like they could handle some of the mathematical heavy lifting (I have no background in maths, so most info I found on the web was of no use to me). Heres some of the clearer ones I found:

http://stackoverflow.com/questions/5173707/detecting-an-objects-rotation-in-3d

My next step was going to be either to lock one axis of rotation to a world axis (which I suspected could look odd) or involve a third point (hence looking at the above links). I couldn’t figure out how to break it back down into a 4 value rotation in the time I had though.

Also I think the new OSCeleton mentioned something about outputting joint angles – I don’t know if thats global rotation, or local (either would be pretty cool), but it might help optimize your patch.

Hey Bferns,

no worries,

I actually made some more progress since then and have nearly full degree of movement by doing some math on the rotation matrix (joint rotation data osceleton can now put out) – I’ll post an easy to use patch once it’s perfected.

right now I’m dealing with a small issue of having the middles of all the joints not line up, but I’m sure it’s something simple I’m overlooking.

as a side note I finally bought a bunch of trig/geometry textbooks, if I’m not good with 3D math today I’ll try and be an expert tomorrow,

almost there! -jeremy

Finally some progress! http://www.youtube.com/watch?v=FU-KqZ1U3VM

just used drawobject on a @name gridshape to get the rotation origin working

rotation calculated using the rotation matrix OSCeleton puts out and rotate shape with shapeorient in jit.gl.sketch

here’s the math…

given the rotation matrix:

X axis = (elements[0], elements[3], elements[6])

Y axis = (elements[1], elements[4], elements[7])

Z axis = (elements[2], elements[5], elements[8])

calculate angles with:

thetaY = asin(Z1);

thetaX = atan2(-Z2,Z2);

thetaZ = atan2(-rY1,X1);

FYI the math above doesn’t handle 360 degrees of rotation freedom,

see more here < http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm>

I wish there was a way to transform a gl object with the raw 3X3 matrix instead of converting everything to euler

is there?

I don’t think you can feed a raw matrix to a gl object. I’m having the same problem. However, you can convert the rotation matrix to a quaternion to avoid gimbal locks and singularities that occur with Euler angles and to gain full 360° freedom. Quaternions can be easily translated to Gl rotate format (ie. using the LFO.qn2rot object)

conversion: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

also make sure you check out the new max 6 quaternion objects:

jit.quat

jit.quat2euler

jit.euler2quat

jit.quat2axis

jit.axis2quat

as well as the jit.anim.node object, which has a gettable attribute called worldtransform, which is the 4×4 world-matrix transform of that object.

any ways these objects can be made more useful to specific tasks, i’m open to hearing!

Hi, Robert. I would love to be able to throw a full 4×4 Gl transform matrix into a jit.anim.node and let it transform children. The intention is to have a max message way to do a glmultmatrix transform

hi diablodale.

i agree this would be useful. look for it to be in the 6.0.2 version of jit.anim.node.

thanks!

Forums > Jitter