Jump to content

Get clock bearing to unit B relative from unit A


Go to solution Solved by d0ppler,

Recommended Posts

Posted (edited)

I can't seem to find any solutions to this when I search, but my apologies if it has been up before.

I'm trying to get the clock bearing to a unit from my player unit's standpoint.

I've divided this problem up in several "sub problems" :

1. I need to know the heading of my player (check)

2. I need to know the x/y pos on both my units (check)

3. I need to calculate the angle between those positions (check)

4. I need to be smart in order to put these things into a smart math formula so I can convert this into 1 to 12 (not gonna happen, I'm not smart enough)

 

local function getAngleDeg(p1, p2)
    return math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / math.pi;
end


b747 = Unit.getByName("b747")
player = Unit.getByName("player")

player_hdg_degrees = mist.getHeading(player, false) * 180 / math.pi;
angle = getAngleDeg(player:getPoint(), b747:getPoint())

trigger.action.outText("Plr hdg : " .. player_hdg .. "\nAngle : " .. angle, 1, true)

I'm using the mist library, so maybe we have some tricks there?

 

Regards, d0ppler

Edited by d0ppler

A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H

  • d0ppler changed the title to Get clock bearing to unit B relative from unit A
Posted

Here is a way to do it. 

 

local player = Unit.getByName("player"):getPoint()
local target = Unit.getByName("b747"):getPoint()
-- mist.utils.getHeadingPoints is basically the same thing your code does. 
local angle = math.deg(mist.utils.getHeadingPoints(player, target)) + 15 -- add 15 degrees because each oClock is a +/- 15 degrees. Without it a target at 179 would reutn as 5 o clock
local oClock = math.modf(angle/30)   -- divide it by 30 and math.modf returns two values, the first being the integer and the 2nd being a decimal of a give number. Only care about the first here. 
if oClock <= 0 then -- if its 0 or lower somehow change it to 12 because there is no zero o clock. 
    oClock = 12
end

 

  • Like 2

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

  • 2 years later...
Posted (edited)

I'm sorry, but I'm bringing this back up. But there's obviusly something not quite right. (Nevermind the the heading, it is only showing the heading of my plane, not the bearing to the bandit).

 

But this should be more like 10, maybe 11 o'clock, not 12.

image.png

 

script at start (after MIST-load):

function getClockBearing(player_point, target_point)
    local angle = math.deg(mist.utils.getHeadingPoints(player_point, target_point)) + 15
    local oClock = math.modf(angle / 30)
    if oClock <= 0 then 
        oClock = 12
    end
    return oClock
end

player = Unit.getByName("player-1")
target = Unit.getByName("lead-1")

 

repetetive script :

local clock_bearing = getClockBearing(player:getPoint(), target:getPoint())

 

Edited by d0ppler

A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H

  • Solution
Posted

Ok, I finally figured it out. The o'clock can't be based on the bearing from player to target alone, it also needs the players bearing, because its' the delta of those two. It is probably possible to refactor this, but here is the code I got it to work with:

 

Startup (after MIST.lua load) - do script:

function getHeading(unitpos)
    local hdg = math.atan2(unitpos.x.z, unitpos.x.x)
    if hdg < 0 then
        hdg = hdg + 2 * math.pi
    end
    hdg = math.floor(hdg / math.pi * 180)
    return hdg
end

function getClockBearing(player_point, target_point, player_heading)
    local angle = math.deg(mist.utils.getHeadingPoints(player_point, target_point))
    if player_heading > angle then
        angle = 360 + angle - player_heading
    else
        angle = math.abs(player_heading - angle )
    end
    local oClock = math.floor(angle / 30 + 0.5)

    if oClock <= 0 then
        oClock = 12
    end
    return oClock
end

player = Unit.getByName("player-1")
target = Unit.getByName("lead-1")

 

Then the repetetive task, do script:

local heading = getHeading(player:getPosition())
local clock_bearing = getClockBearing(player:getPoint(), target:getPoint(), heading)

 

A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H

Posted
  • Like 1

Natural Born Kamikaze

-------------------------

AMD Ryzen 5 3600, AMD Fatal1ty B450 Gaming K4, AMD Radeon RX 5700 XT, 32 GB RAM Corsair Vengeance LPX, PSU Modecom Volcano 750W, Virpil Constellation Alpha Prime on Moza AB9 base, Virpil MongoosT-50CM3 Throttle, Turtle Beach VelocityOne Rudder.

  • 1 month later...
Posted
On 1/15/2025 at 6:38 PM, d0ppler said:

... but here is the code I got it to work with

Thank you!  By sheer coincidence, I was trying to figure this out myself over the weekend.  Your code snippet saved me a ton of time.  Thanks!!

  • Like 1

System Specs:

Spoiler

📻Callsign:Kandy  💻Processor:13th Gen Intel(R) Core(TM) i9-13900K - 🧠RAM: 64GB - 🎥Video Card: NVIDIA RTX 4090 - 🥽 Display: Pimax 8kx VR Headset - 🕹️Accessories:  VKB Gunfighter III MCG Ultimate, VKB STECS Standard, Thrustmaster TPR Pedals, Simshaker JetPad, Predator HOTAS Mounts, 3D Printed Flight Button Box 

📹 Video Capture Software:  Open Broadcaster Software (OBS), 🎞️ Video Editing Software:  PowerDirector 35

 Into The Jungle (MP Mission)  F18: Scorpion's Sting  Apache Campaign - Griffins  Kiowa Campaign - Assassins 

 

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...