# Two-part rotation in separate axes

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

So I have this little thing. It's a turret that rolls around its base to aim around itself, and pitches the cylindrical cannon to aim for targets at different altitudes.

This turret is linked to a larger linkset, so neither of them is the root (but the root's center will be the bottom of the base).

I'm having trouble with the math behind this, as usual. Rotating the base is pretty easy:

```vector target_position; // Could be anything.

vector direction = llVecNorm(target_position - llGetPos());
direction.z = 0; // Ignore the Z offset, the base MUST stay upright.

rotation final = llRotBetween(<1,0,0>, direction);

Even going so far as rotating both, the base and the cannon to face the same direction is trivial, but I don't know how to add to the cannon's local Y rotation so that it would only pitch up/down without rolling around itself and looking all wonky.

• 1
##### Share on other sites

Maybe you should study how to do child prim rotations.

##### Share on other sites

Wulfie --   That's hard to get your head around.  One of the best discussions I have seen is the one that Chalice Yao wrote ages ago and is now in the VirtualVerse site at https://www.virtualverse.one/forums/threads/kept-simple-rotate-object-towards-another-object.178/

Innula and I have each borrowed that bit of code and adapted it for controlling turrets and robot arms and whatever.

• 2
• 1
##### Share on other sites

13 hours ago, Rolig Loon said:

Wulfie --   That's hard to get your head around.  One of the best discussions I have seen is the one that Chalice Yao wrote ages ago and is now in the VirtualVerse site at https://www.virtualverse.one/forums/threads/kept-simple-rotate-object-towards-another-object.178/

Innula and I have each borrowed that bit of code and adapted it for controlling turrets and robot arms and whatever.

Helpful as ever, Rolig! Thank you for reminding me of that. I recall reading the old version of the thread before the forum migrated.

I was actually pretty close.. had the right idea anyway.. but couldn't get the calculation right for the other llRotBetween.
My final code looks something like this-ish:

```vector target;

vector direction = llVecNorm(target - llGetPos());
direction.z = 0;

rotation final = llRotBetween(<1,0,0>, direction);

float distance = llVecDist(<target.x, target.y, 0>, <this.x, this.y, 0>);
rotation pitch = llRotBetween(<1,0,0>, llVecNorm(<distance, 0, pos.z - this.z>));

PRIM_LINK_TARGET, 3, PRIM_ROTATION, pitch * final]);```

Edited by Wulfie Reanimator
typo
• 2