Need help calculating a location

Everything about mods can be found here.

Moderator: Morax

Need help calculating a location

Postby Resin_Smoker » 11 May 2015, 03:41

I've two units with known locations.

What i want to do is use both units locations to create a vector (direction) and then set a new location a specific distance along said vector.

Example:

Unit A Position.
Unit B Position.
New Destination along the same vector a given distance away.

A ------ B -----> D

While i can easily calculate a vector from two points, creating a new destination from said vector and a specified distance has eluded me thus far.

Thanks in advance!

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help calculating a location

Postby IceDreamer » 11 May 2015, 04:22

What do you want help with? Developing the coordinate for D, or actually issuing a command to one of the units?
IceDreamer
Supreme Commander
 
Posts: 2607
Joined: 27 Dec 2011, 07:01
Has liked: 138 times
Been liked: 488 times

Re: Need help calculating a location

Postby ckitching » 11 May 2015, 05:17

Isn't this fairly trivial?

Given a vector from A to B, you can obtain a vector from A to a point beyond B by multiplying it by a scalar.

It's also not quite true that a vector is "a direction": a vector represents both direction and magnitude.
User avatar
ckitching
Avatar-of-War
 
Posts: 229
Joined: 03 Jan 2015, 12:51
Has liked: 2 times
Been liked: 69 times
FAF User Name: ckitching

Re: Need help calculating a location

Postby FunkOff » 11 May 2015, 05:20

Shouldn't this be easy?

Find the difference between the X and Z values for the positions of the units. Use that to calculate the distance. Then calculate two ratios, one for the X value over the distance and one for the Z value over the distance. (These would be called "Sine" and "Cosine" in trigonometry). Then pull whatever your set distance between unit B and position D is (I assume it's a fixed value?) and multiple these ratios times the set distance, and you have your X and Z position deviations from unit B.
FunkOff
Supreme Commander
 
Posts: 1863
Joined: 26 Aug 2011, 17:27
Has liked: 14 times
Been liked: 43 times
FAF User Name: FakeOff

Re: Need help calculating a location

Postby Vee » 11 May 2015, 10:28

Define unit(v) = v/sqrt(v.x^2 + v.y^2 + v.z^2). The position you want is B + unit(B-A)*given_distance.
Vee
Evaluator
 
Posts: 677
Joined: 04 Dec 2013, 20:43
Has liked: 275 times
Been liked: 225 times
FAF User Name: Vee

Re: Need help calculating a location

Postby Resin_Smoker » 11 May 2015, 13:21

The problem is with the specified distance not meeting my expectations.

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help calculating a location

Postby Myxir » 11 May 2015, 13:35

there is a lua/utilities.lua, should be helpful

something like this might work
Code: Select all
A + NormalizeVector( GetDirectionVector( v1, v2 ) )*length
Unhappy with balance http://i.imgur.com/q5G2BlM.png
User avatar
Myxir
Evaluator
 
Posts: 791
Joined: 09 Apr 2012, 14:01
Has liked: 94 times
Been liked: 306 times
FAF User Name: Washy (irc)

Re: Need help calculating a location

Postby Resin_Smoker » 11 May 2015, 15:15

Here is what I've written thus far...

This not only gets the entities at the location but also validates and sorts entities by range.

Any suggestions?

Note: This will be used for the chain-lightning revamp.

Resin

---Code---
Spoiler: show
Code: Select all
   GetValidTargetsAlongScaledVector = function(self, entity1, entity2, scale)
      if self.BeamDebug then WARN('GetValidTargets') end
      local posV1 = entity1:GetPosition()
      local posV2 = entity2:GetPosition()
      local modPos = utilities.GetScaledDirectionVector( posV1, posV2, scale )
      if self.BeamDebug then
         WARN('modPos x: ', modPos[1],'modPos y: ', modPos[2],'modPos z: ', modPos[3])
      end
      local targets, targByDist = {}
      local targets = GetEntitiesInRect( Rect(modPos[1] - scale, modPos[3] - scale, modPos[1] + scale, modPos[3] + scale) )
      if targets == nil or table.getsize(targets) < 0 then return nil end
      for a, b in targets do
         if not b:BeenDestroyed() and b:GetHealth() > 0 then
            table.insert(targByDist, {dist = utilities.XZDistanceTwoVectors(posV1, b:GetPosition()), unit = b})
         end
      end
      table.sort(targByDist, sort_by('dist'))
      if self.BeamDebug then
         for c, d in targByDist do
            WARN('    Unit: ', d:GetEntityId(), ' Distance: ', utilities.XZDistanceTwoVectors(posV1, d:GetPosition()) )
         end
      end
      return targByDist
   end,
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help calculating a location

Postby Legion Darrath » 11 May 2015, 16:06

Moved to mods.
Legion Darrath
Evaluator
 
Posts: 693
Joined: 03 Oct 2011, 19:50
Has liked: 7 times
Been liked: 117 times
FAF User Name: Legion_Darrath

Re: Need help calculating a location

Postby Resin_Smoker » 11 May 2015, 16:17

Legion Darrath wrote:Moved to mods.


Gee thanx for the assist... No idea what the world would of done with you at that moment in time. (sarc)
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Next

Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest