+ Reply to Thread
Results 1 to 9 of 9

Thread: Calculating Bearing, Elevation, and Distance with vectors

  1. #1
    Wire Noob GhostMX's Avatar
    Join Date
    Jul 2007
    Posts
    4

    Thumbs up

    So I'm working on a laser guided turret, but I've run into a small hitch. It seems that when I calculate the bearing and elevation between two vectors, they're global and not local to the object (No matter where I point, the bearing is absolute. Even if the rotation of the object changes, the bearing doesn't. Pointing at 0* points me at the north section of the map). This is the code to calculate (Thanks Shandolum)

    @I X Y Z X2 Y2 Z2
    @O Bearing Elevation Distance
    V1 = vector(X,Y,Z)
    V2 = vector(X2, Y2, Z2)
    V3 = vecsub(V2 - V1)
    Bearing = vecyaw(V3)
    Elevation = vecpitch(V3)
    Distance = veclength(V3)

    If anyone can help me make those three outputs behave more like the beacon sensor's, that would be great.

    I just poked around in the beacon sensor lua code, and found out how they calculate the bearing

    local MyPos = self.Entity:GetPos()
    local BeaconPos = self.ToSense:GetBeaconPos(self.Entity)
    local DeltaPos = self.Entity:WorldToLocal(BeaconPos)
    brng = DeltaPos:Angle()

    Roughly translated: My position
    Their position
    Delta position by converting world coordinates of them to local coordinates to me
    Output that angle

    I guess WorldToLocal(Vector pos) would return MyPos - pos or pos - MyPos. I'm not sure how Angle() works though

    I think I need to find the acos, so acos(vecdot(MyPos,Theirpos)) but that doesn't seem to work

    Thanks!

  2. #2
    Expressionism 2.0 Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,573

    Default

    You need to use a gyroscope or one more GPS to get bearing and elevation to work properly, because they are relative to the direction of the object itself, which you can't know using just one GPS. (Which means that your current solution assumes that the prop is always facing the same direction and never rotates in any way)

    Hmm, might need 3 GPSes in total when I think of it, but I'm not sure. (So you can extract both an up-vector, forward-vector and right-vector)

  3. #3
    Wire Noob GhostMX's Avatar
    Join Date
    Jul 2007
    Posts
    4

    Default

    Just a thought

    I'm in class now, but I think I might have solved it. I need to convert the world coordinates to local, like this

    Pos = XYZ (world)
    TPos = X'Y'Z' (world)

    LPos = X+cos(Yaw)*Distance,Y+sin(Yaw)*Distance,Z (The projection to create our A vector
    LTPos = X'Y'Z' - XYZ
    LLPos = LPos - XYZ (to localize it)

    So now we have two local vectors (relative to the position of the object)

    arccos(vecdot(LTPos * LLPos / |LTPos|*|LLPos|)) (normalize and dot product them)

    I hope this works, I was quite excited when I drew out the diagram. I have not yet confirmed it, but I will when I get back from class. If anyone wants, they can confirm it for me.

  4. #4
    Wirererer 1xinfusion's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    112

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    bump.

  5. #5
    Wire Noob jakbruce2004's Avatar
    Join Date
    Apr 2007
    Posts
    5

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    bump

  6. #6
    Wire Sofaking IEF015's Avatar
    Join Date
    Feb 2008
    Location
    London, ON (Canada, eh?)
    Posts
    1,640

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    Why
    "It's my favourite country song. And I hate it."

  7. #7
    Wire Amateur kuro11's Avatar
    Join Date
    Apr 2007
    Posts
    76

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    The correct way of finding bearing elevation. (distance part was right). This is relative to the chip's forward vector.
    Code:
    N@Bearing
    I@Xt Yt Zt
    O@Bearing Elevation Dist
    interval(1)
    Target=vecsub(vector(Xt,Yt, Zt),extpos())
    TargetN=vecnormalize(Target)
    Br=acos(vecdot(extdirfw(),TargetN)/(veclength(extdirfw())*veclenght(TargetN)))
    En=acos(vecdot(extdirup(),TargetN)/(veclength(extdirup())*veclenght(TargetN)))-90
    Trt=acos(vecdot(extdirrt(),TargetN)/(veclength(extdirrt())*veclenght(TargetN)))
    Tr=angnorm(Trt-90)
    Bearing=(Br>0&Br<181?Br*sgn(Tr):0)
    Elevation=En
    Dist=veclength(Target)
    The reason I post this here was I made a turret using the idea you had here, to do everything with vectors. However, that didn't work when the turret was tilted. So, with this, it will work right. Have fun.

  8. #8
    Wire Amateur BManx2000's Avatar
    Join Date
    Jun 2008
    Posts
    34

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    There's a typo in your code, you have "veclenght" instead of "veclength".

    Also, I tested this with a beacon sensor linked to a laser pointer reciever, and it works most of the time, but sometimes the bearing is wrong. Maybe you should look into it.

  9. #9
    Spucatum Tauri Bull's Avatar
    Join Date
    Jun 2008
    Location
    Finland
    Posts
    6,010

    Default Re: Calculating Bearing, Elevation, and Distance with vectors

    You do realize this is an almost year old thread?
    My signature has a point.
    Quote Originally Posted by Squeakyneb View Post
    when l3ulletje says do it, do it.
    That

    Quote Originally Posted by Anticept View Post
    By the way, Bull is in charge.

+ Reply to Thread

Similar Threads

  1. Bearing from vectors
    By Pyro-Fire in forum Installation and Malfunctions Support
    Replies: 4
    Last Post: 04-22-2008, 10:49 AM
  2. Bearing and elevation out of vectors
    By Shockbolt in forum Installation and Malfunctions Support
    Replies: 4
    Last Post: 11-06-2007, 05:46 PM
  3. Calculating bearing?
    By thesage1014 in forum Installation and Malfunctions Support
    Replies: 7
    Last Post: 08-19-2007, 11:32 PM
  4. Calculating Bearing and Elevation from GPS co-ords
    By MistaGiggles in forum Installation and Malfunctions Support
    Replies: 4
    Last Post: 06-06-2007, 12:41 AM
  5. Beacon Sensor Outputting Distance Instead Of Elevation?
    By BrandonR in forum Bug Reports Archive
    Replies: 5
    Last Post: 05-31-2007, 09:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots