Make chip face aimPos but always pitched 90 degrees

I can’t find some code I posted on the Wiremod forum because the forum no longer exists. I tried Wayback machine to no avail.

I’m trying to make an E2 chip face my aimPos. This forum has helped me do just that using the code below:


@persist Vec:vector Ang:angle E:entity O:entity
if (first()) {
    runOnTick(1)
    O = owner()
    E = entity()
    E:setMass(1000)
}

Goto = O:pos()+vec(0,0,100)
Cur = E:pos()
Vec = Goto-Cur
E:applyForce((Vec + $Vec*5)*E:mass()*10)

P = E:elevation(O:aimPos())
Y = E:bearing(O:aimPos())
R = E:angles():roll()
Ang = -ang(P,Y,R)
E:applyAngForce((Ang+$Ang*5)*E:mass()*0.5)

I have to use E:mass()*0.5 in the last line otherwise the E2 spazzes out.

This makes the E2’s side face my aim position. The top of the E2 is facing the sky. I’d like to make the E2’s top face my aim position.

I’ve tried a lot of things to do this, but the E2 keeps spazzing out.


P = E:elevation(O:aimPos()) + 90

makes the E2 spaz out immediately.


P = E:elevation(O:aimPos()) + 75

keeps the E2 fairly stable the more I aim at the floor. When I’m looking up the E2 starts twitching more and more.

I’ve tried stuff with swapping Yaw and Roll (since when you’re pitching 90 degrees, the E2’s Yaw will act as Roll and the E2’s Roll will act as the Yaw).

I think my math is correct: applyAngForce keeps the E2 stable when its angles are 0,0,0. This works with the code above, except I want to keep it stable at -90,0,0 so I need to translate my desired pitch to whatever applyAngForce needs to work with:


E:angles() = applyAngForce()
  -90,0,0  =    0,0,0
  -45,0,0  =  -45,0,0
 -110,0,0  =   20,0,0
   45,0,0  = -135,0,0
  -20,0,0  =  -70,0,0
    0,0,0  =   90,0,0
  
-20,10,-10 = -70,-10,10

I know that in order to counteract the current angles, I need to negate the current angles. So if the E2 is yawed 20 degrees, I need to apply -20 degrees yaw in applyAngForce. The same principle is true for pitch; when the E2 is pitched 45 degrees, I need to apply -45 degrees.

So basically I need to add 90 to whatever the current elevation is:


E:angles()     =  desired = applyAngForce()
  -90,0,0  +90 =    0,0,0 =    0,0,0
  -45,0,0  +90 =  +45,0,0 =  -45,0,0
 -110,0,0  +90 =  -20,0,0 =   20,0,0
   45,0,0  +90 = +135,0,0 = -135,0,0
  -20,0,0  +90 =  +70,0,0 =  -70,0,0
    0,0,0  +90 =  +90,0,0 =  -90,0,0

What am I doing wrong?