0
\$\begingroup\$

I've looked around and almost all examples is see have a velocity vector which I do not have, or at this point am unable to figure out. Im trying to find the 3D position where i can launch a trap in game to land in the targets path of movement assuming constant speed.

What I do Have

Two Coords: Initial and a second set of X,Y,Z after 1 frame. Unit speed as a number from the game, hoping i can calculate a better number based on the one frame difference above. The speed of the trap - 25yards/s The time between frames 1/ Games current FrameRate

This works on still targets but I cannot find out how to get the units speed into the calculation:

function PredictTrapPosition(unit)
unit = unit or 'target'
local x1, y1, _ = UnitPosition(unit) --forget z assume they are running on flat ground. 
C_Timer.After(1/GetFrameRate(), function() -- after one frame
    local x2, y2, z = UnitPosition(unit)
    local px, py, _ = UnitPosition('player') --My position
    local distance = math.sqrt((x2 - px)^2 + (y2 - py)^2)
    local trapSpeed = distance / 25 -- trap travel time. 
    local angle = atan2(x2-x1, y2-y1)
    local x = x2 + cos(angle) * trapSpeed
    local y = y2 + sin(angle) * trapSpeed
    WorldClick(x, y, z)
end)

end

Hopefully a math guru can chime in and help me fix this probably simple formula.

\$\endgroup\$
  • \$\begingroup\$ Couldn't you just calculate the velocity from the current and next set of coordinates, normalize it and multiply with speed to get the distance travelled per frame (or per unit of time)? With that, it should be trivial to extrapolate for X amount of frames if you assume constant direction and speed. \$\endgroup\$ – Christian Jan 29 '15 at 15:45
  • \$\begingroup\$ is it not possible to get the distance traveled per frame from just the coords itself no need to deal with speed or am i mistaken? \$\endgroup\$ – Richard Jan 29 '15 at 17:14
  • \$\begingroup\$ Calculate the vector between both sets of coordinates and then its length, that should be the distance it has travelled inbetween. \$\endgroup\$ – Christian Jan 30 '15 at 9:57
1
\$\begingroup\$

I am a little curious on why you don't have the velocity vector. But, no matter it can be calculated.

Note that there will be error in here somewhere... it's 5am. The framework is the same however.

First step. We need that Vector.

We know the position before, and after the frame step. You should have the delta time as well. if not, there really isn't much you can do.

Let xi be Inital Player position before render, xf be position at the frame's render.

Let yi be initial Player y position before render, yf be the position at the frame's render.

let dt be Delta Time

Distance 2d Distance= Sqrt((xf-xi)^2 + (yf - yi)^2 ))

Radians = Atan((yf-yi)/(xf-yi))]

Find Speed = Distance*( dt*(dt/1))

Now we can find our vector!

vecVelocity.x = cos(radians)*speed

vecVelocity.y = sin(radians)*speed

Second Step. We need to know how long it takes for the trap to land.

TimeNeeded = Trap.Distance / Trap.Speed

Third Step. Find the minimum distance we need to launch the trap.

AngleFromTrap = ATan((Unity.y - Trap.y)/(Unit.x - Trap.x))

Using the distance line between trap and unit as a reference, we calculate the angle from the reference frame to

AngleBetweenVectors = ACos((unit.x) / (unit.x + unit.y))

NOTE: This is a distance from the trap, that is perpendicular to the distance between Unit and Trap. This intersects the trajectory of the unit.

Distance from traps center = Cos(AngleBetweenVectors)*(unit.x + unit.y)

Before we continue... this is a safety check.

If Distance from the traps center is larger than Trap's affect radius, we don't bother with activating.

Other wise... Find out how the minimum activating distance.

We start from the "Distance from Traps Center" and calculate back towards the unit. We multiply the unit's velocity by the number of seconds it will take for the trap to land.

Once we find the point. Find the distance from that to the center. And that is your radius.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.