Jump to content

Recommended Posts

Posted

Hey much-smarter-than-me-folks,

My old nemesis math(s) has raised its ugly head, and I could do with some help. How would I go about calculating a random point in 2D space that's an exact distance from a known point? Specifically, I'm looking to create the second point of a racetrack orbit that's exactly X nautical miles from the first. 

I assume it's got something to do with finding a random point on the circumference of a circle, but that sounds suspiciously like trigonometry to me... 😀

Any guidance from the resident math(s) masters would be much appreciated.

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

  • Solution
Posted (edited)
1 hour ago, Pizzicato said:

I assume it's got something to do with finding a random point on the circumference of a circle, but that sounds suspiciously like trigonometry to me

Actually, in 3D that would be a random point on a sphere, and it can be done rather quickly by giving a radius (x miles), theta (random) and phi (random). Problem is: you'll probably want some more attributes, like the point having a certain altitude etc. If you are talking about a map, and you want a 2D point at x miles, that's even easier: take r (x miles) and random number between 0 and 360, and you have a random angle from that you can quickly calcualate x offset and y offset, add that to your current position, and you have a point on a circle around your point. You'll need to check the altitude again, but with that simple formula (radius and random angle), you can quickly resolve that issue.

So, if (X,Z) [in DCS, x and z are map coords, and y is altitude above MSL] is your current point, then make phi = math.random(360) * 0.0174533 [that 0.01... converts to rads] and r = distance you want

your new point is at (X + r * cos(phi) , Z + r * sin(phi))

Simple as that. Yupp, trigonometry all the way.

Edited by cfrag
  • Like 1
Posted

Here we go again 😎

--- Armor deploys RPG_Team
--- this clones a RPG_Team team at an armor group's position
--- Tanks vs. Tanks 05

--- Mission Start Radio Add for Coalition per VGrp
--- Once Deploy RPG_Team, remove Radio

--- Blue
--- Blue VGrp12 RPG Team
--- Blue VGrp01 Tank
--- Blue VGrp07 Bradley
---
--- Red
--- Red VGrp12 RPG Team
--- Red VGrp01 Tank
--- Red VGrp07 BMP
---

local grpNameArmor = 'Blue VGrp01 Tank'
local grpNameRPG_Team = 'Blue VGrp12 RPG Team'
local unitNameArmor = Group.getByName(grpNameArmor):getUnit(1):getName()
local basePos = mist.getLeadPos(grpNameArmor)
local baseHeadingRad = mist.getHeading(Unit.getByName(unitNameArmor))
local baseHeadingDeg = baseHeadingRad * 180 / math.pi
local newGroupRPG_Team = mist.getGroupData(grpNameRPG_Team)
local newCoords = 
    {
        [1] = 
        {
            x = basePos.x + ((75 * math.cos(baseHeadingDeg)) - (0 * math.sin(baseHeadingDeg))),
            y = basePos.z + ((75 * math.sin(baseHeadingDeg)) + (0 * math.cos(baseHeadingDeg))),
            heading = baseHeadingDeg,
        },
        [2] =
        {
            x = basePos.x + ((-75 * math.cos(baseHeadingDeg)) - (75 * math.sin(baseHeadingDeg))),
            y = basePos.z + ((-75 * math.sin(baseHeadingDeg)) + (75 * math.cos(baseHeadingDeg))),
            heading = baseHeadingDeg + 40,
        },
        [3] =
        {
            x = basePos.x + ((-75 * math.cos(baseHeadingDeg)) - (-75 * math.sin(baseHeadingDeg))),
            y = basePos.z + ((-75 * math.sin(baseHeadingDeg)) + (-75 * math.cos(baseHeadingDeg))),
            heading = baseHeadingDeg - 40,
        }
    }
for i = 1, #newGroupRPG_Team.units do
    newGroupRPG_Team.units[i].x = newCoords[i].x
    newGroupRPG_Team.units[i].y = newCoords[i].y
    newGroupRPG_Team.units[i].heading = newCoords[i].heading
end
newGroupRPG_Team.clone = true
mist.dynAdd(newGroupRPG_Team)
trigger.action.outText(grpNameArmor .. ' just dropped off an RPG Team!', 20)

--- end of Armor deploys RPG_Team

 

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Posted

Thanks for the explanation, @cfrag and thanks for the tangible example @Wrecking Crew. 👍

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

  • Recently Browsing   0 members

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