How to translate a coordinate systrem into a spawn angle..

Hi, so my problem is like so:

I got predefined custom directions for Forward, Right, Up, which make a coordinate system
with a vector origin and the F, R, U. Their lenghts are equal to 1, but it does not matter at all, because
I need their world angle ( Origin ).

I also got another coord system define like the last one ( 3 vectors for X,Y,Z I will call it Target)

If I have an Entity and I call SetAngles

Prop:SetAngles(Origin.Forward:Angle()) The Entity’s Forward will mach Origin’s forward, but not Target’s Forward…

Also With SetAngles() I can’t match the Target’s Up to the Origin’s up!

How do i get the world angle to align Target to Origin without any Angle Offsets

I got some ideas of using AngleEx but I am still in closed street …

Are you trying to make an entity face a specific vector? You’re not entirely clear on what exactly you’re trying to do… If what I said is it then wouldn’t it be something like


(vec2 - vec1):Angle()

Use SetEyeAngles for players to face a certain direction. If you type getpos in console, the angle should appear, and you would only need to set the yaw when spawning a player.

Yep, that would be it … So you see i got one ENT ( Origin ) and second ENT ( Target )
Got a local vector realtive to the Target, for example vec(1,1,0) at angles ang(0,0,0)
and a Origin’s local vector vec(1,0,0) rotated to Origin’s angles.

I want to calc an angle, which will direcltly face Target’s vec(1,1,0) (at angle ang(0,0,0) )
to the Oroigin’s vec(1,0,0):Roteate(Origin:GetAngles())

Мay be like so:



local Vo = vec(1,0,0)  // Local Origin ENT's Defined vector
        Vo:Roteate(Origin:GetAngles()) // Rotate it

// Target ENT has angles ang(0,0,0) so....

local Vt = vec(1,1,0)

Target:SetAngles((Vo -Vt):Angle())   // Has to be it !



I got two defined coordinate systems, its a sin not to be using them xD

Make sure that Local Roll will be not defined That way I have to mix them somehow …

Use ENTITY:LocalToWorld to convert your vectors into world space then get the angle between them. LocalToWorld will take into account the entities rotation.



local Vo = vec(1,0,0)  
local Vt = vec(1,1,0)

local vOW = Origin:LocalToWorld(Vo)
local vTW = Target:LocalToWorld(Vt)

local Ang = vOW:Angle() - vTW:Angle() 

Target:SetAngles(Ang)  // Seems nice :)


I was thinking
[lua]local Ang = (vOW - vTW):Angle()[/lua]
but whatever works for you

10x I will be sure to try that asap…

[editline]23rd April 2014[/editline]

BTW Do you think that I can ignore calcing


 local vTW = Target:LocalToWorld(Vt) 

because my target angles are


 Angle(0,0,0) 

and I should have


 vTW:Set(Vt) 

Last update added these matrix functions, give them a try. If you really have your own forward, right and up vectors and you are sure they are orthogonal to each other, this should give you what you need.

[lua]local m = Matrix()
m:SetForward(F)
m:SetRight®
m:SetUp(U)

local ang = M:GetAngles()[/lua]

[editline]a[/editline]

Nevermind, I tried to read your other posts and I don’t think that’s what you need.

Mind explaining this post in more detail? I find it really hard to understand what you’re trying to do exactly. A more concrete example would be more useful. Either way this is the kind of problem that’s easily solved with matrices but I can’t give you a solution if I don’t know precisely what you want to do. It looks like you are trying to rotate Target in such a way that a vector going from its origin to a local position points towards a specific spot on the Origin entity.

I am trying to rotate Target ENT with custom F,R,U vectors to an Origin ENT with a custom F,R,U vectors … and i need the angle, which will do that
They are indeed ortogonal on the Target and Origin (if they are not, i got a script for doing just that :wink: )

  1. Target’s negative custom Forward has to face Origin’s positive custom Forward (Same direction here)
  2. Target’s positive Up --> Origin’s positive Up (Here too)
  3. Target’s negative Right --> Origin’s positive Right (Here also)

Give it Forward and up and it has to return where Target should be spawned…

So far i got this:

[lua]
function aGetAngleSnapByFwdUp(FTarget ,UTarget ,FOrigin,UOrigin)
local OriginFwdAngle = FBase:Angle()
local OriginRghAngle = FBase:Cross(UOrigin):Angle()
local AngOff = Angle(2*OriginFwdAngle.pitch,0,OriginRghAngle.pitch)
local Rez = Angle(0,0,0)
Rez:Set(OriginFwdAngle - AngOff)
return Rez
end
[/lua]

An I know that I don’t use Target in the equation, coz everytime I do the angles get messy … really messy…

I am gonna try this indeed :slight_smile:

Another way of doing that is using a DotProduct() and RotateAroundAxis() done every iteration with a small step like 0.00001 for ex;

[lua]
local Dot = OriginF:DotProduct(TargetF)
// Target stars with Ang(0,0,0) xD custom ones
// it may be for example Vector(-1,1,0) when Ent’s Angles are Angle(0,0,0)
local TargetAng = Angle(0,0,0) // Remember Target’s angles are all 0-s We are matching vectors here …
local Ang = 0
local Step = 0.00001
local Min = 1000
local Yaw = 0
while(Ang < 360) do
Dot = OriginF:DotProduct(TargetF)
if(Dot <= Min) then
Min = Dot
Yaw = Ang
end
TargetAng:RotateAroundAxis(Vector(0,0,1),Step)
Ang = Ang + Step
end
[/lua]

This one for yaw … kinda slow though … Min is

I have done It
You can close the thread
Answer: http://wiki.garrysmod.com/page/Vector/AngleEx “Деян Добромиров” (Ме)

Angle = MyForward:AngleEx(MyUp)