Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

angles between objects

Deanfred Brandeis
one who programs
Join date: 20 Aug 2006
Posts: 20
02-24-2007 20:41
If I have two objects--for example, two small spheres--and I want to connect them with a cylinder, calculating the angle between them is easy as pie if they share at least one of the XY, XZ, or YZ planes (i.e., one of their X, Y, or Z coordinates is the same). But what if they share no planes directly addressable by the viewer's edit system?

Here's an example of the first problem, which is easily solvable with simple trigonometry:

Sphere 1: <10.0, 20.0, 100.0>
Sphere 2: <10.0, 25.0, 105.0>

In this example, the two spheres share the YZ plane, and calculating the angle on that plane relative to the Y axis is simple:

tan theta = (z2 - z1) / (y2 - y1) = (10 / 10) = 1
theta = 45 degrees

I can then create a cylinder of the correct length, rotate it such that it is on the Y axis, place one of its ends on sphere 1, link the cylinder with sphere 1 as the root prim, and simply rotate 45 degrees on the Y axis. Easy.

Here's an example of the second problem, which may seem trivial in trigonometry, but actually isn't (and if you know how to solve this, you know this already):

Sphere 1: <10.0, 20.0, 100.0>
Sphere 2: <12.0, 25.0, 105.0>

In this example, the two spheres do not share any of the three addressable planes. Doing a similar calculation as above for the angle alpha (the angle relative to the Y axis on the XY plane, discounting the Z axis) and beta (the angle relative to the Y axis on the YZ plane, discounting the X axis), only beta will be correct. Alpha will be slightly too large, and when extreme accuracy is needed, "slightly too large" means useless. While I can get the distance very easily with the Euclidean distance formula and then rotate the remaining alpha by sight, I can never be sure to get the exact angle.

This may seem trivial, but consider this: Imagine these two spheres are 50 meters apart. While I could pivot a long, linked series of cylinders in other cases, the viewer will not let me link 50 meters of cylinder, so I absolutely cannot "eyeball" it in this case. There's no way to place this cylinder series without being able to calculate all angles precisely.

Does anyone know a simple way to do this using Euclidean rules? If not, using linear algebra or some other method?
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
02-27-2007 08:41
You could script the prim to llLookAt the other position.
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Ceera Murakami
Texture Artist / Builder
Join date: 9 Sep 2005
Posts: 7,750
02-27-2007 09:45
Do it in two steps?

Calculate the data for Object A and a point on the x-y plane that is at the same x-y coordinates as Object B, but shares the z-value for Object A. That gives you a different starting length and angle, that is on a plane with Object B.

Now calculate using that baseline in the x-y plane and the z-axis difference, and you can get the point to point angle and length between the two.
_____________________
Sorry, LL won't let me tell you where I sell my textures and where I offer my services as a sim builder. Ask me in-world.
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
02-27-2007 10:12
I'm not the greatest at rotations (see comment under my name...), but unless I misunderstand, I think you're making this more difficult for yourself than you need to?

sphere 1 position at (vector) pos1
sphere 2 position at (vector) pos2

(vector) dir = llVecNorm(pos2-pos1) will give a unit vector pointing from sphere 1 to 2

then use (rotation) rezRot = llRotBetween( <appropriate axis vector>, dir ), where the axis vector might be <0,0,1> or <0,1,0> or <1,0,0> to give the likely rotation you'll have to apply to your rezzed connection cylinder.

Depending how many cylinder links you need, use a loop to get positions between pos1 and pos2, rez a cylinder link at the appropriate place with rezRot rotation.

Does that do what you want? The details of the above might be wrong, but I think the concept should be sound.
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
RobbyRacoon Olmstead
Red warrior is hungry!
Join date: 20 Sep 2006
Posts: 1,821
02-27-2007 10:45
From: Ged Larsen
Does that do what you want? The details of the above might be wrong, but I think the concept should be sound.


I don't know if it does what the OP wanted, but it sure as heck helped me, thank you.

I had been looking for a better description of how to use llRotBetween(), and for whatever reason your description clicked for me where the others had not.
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
02-27-2007 12:27
From: RobbyRacoon Olmstead
I don't know if it does what the OP wanted, but it sure as heck helped me, thank you.

I had been looking for a better description of how to use llRotBetween(), and for whatever reason your description clicked for me where the others had not.


LOL, glad to be of help. And when you understand rotations, please explain them to me, because I still just hack at it, trying every which axis I can, until something vaguely resembling what I was trying for happens.

But more often, I end up with this:
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Deanfred Brandeis
one who programs
Join date: 20 Aug 2006
Posts: 20
07-15-2007 23:16
From: Ged Larsen
sphere 1 position at (vector) pos1
sphere 2 position at (vector) pos2

(vector) dir = llVecNorm(pos2-pos1) will give a unit vector pointing from sphere 1 to 2

then use (rotation) rezRot = llRotBetween( <appropriate axis vector>, dir ), where the axis vector might be <0,0,1> or <0,1,0> or <1,0,0> to give the likely rotation you'll have to apply to your rezzed connection cylinder.


Thanks for the explanation. I don't know how I missed your reply, but I ended up doing exactly (I think) what you're doing here. :)

Rotations confound me as well, BTW--or at least, what happens when applying various operations to them.