Link to home
Start Free TrialLog in
Avatar of JasonMewes
JasonMewes

asked on

Reverse trajectory calculation 2

Formula:
  x = vx * t
  y = vy * t + c * t²

Rules:
  sqr( vx² + vy² ) = 1
  c >= 0
  t >= 0 (in practice t >= abs( x ))

Note: vx and vy ARE NOT v*x and v*y - they are unique variables.

When the numbers c, x and y are known,
for a given value of t,
  how do I determine if the equation can be solved?
  if the equation can be solved,
    how do I determine the values of vx and vy?

Nice to know:
  x,y = position (relative to origin)
  vx,vy = direction (normalized)
  c = curvature
  t = time
Avatar of JasonMewes
JasonMewes

ASKER

NOTE!!! For clarification: vx and vy ARE NOT v*x and v*y, they are to be intepreted as their own variables/parameters.
Avatar of ozo
x = vx * t
y = vy * t + c * t²
sqr( vx² + vy² ) = 1

vx = x/t
vy = (y - c * t²)

if sqr( vx² + vy² ) = 1, then the equation can be solved
x = vx * t
y = vy * t + c * t²
sqr( vx² + vy² ) = 1

vx = x/t
vy = (y - c * t²)/t

if sqr( vx² + vy² ) = 1, then the equation can be solved
If t is not given,
x² + (y - c * t²)² = t²

x² + y² - 2*c * t² + (t²)² = t²

x² + y² - (2*c +1)* t² + (t²)²
 (t²)² + (1-2c) t² + x² + y² = 0
(t²) = (-(1-2c) ±sqr((1-2c)² - 4(x² + y² ))/2
which is solvable if (1-2c)² > 4(x² + y² ) and sqr((1-2c) - 4(x² + y² ) > (1-2c)
if i understand this correctly - when c is contant, there are always two possible t for every vx and vy (and vice versa)? i guess this does seem to fit the results of my simulations...

please have look at the following though:

when
 c  =  7
 t  =  0.1050
 vx =  0.4664581256585
 vy = -0.8845432815907
then
 x =   0.0489781031941
 y =  -0.0157020445670
using
 t1 = Sqr((-(1 - (2 * c)) + Sqr(((1 - (2 * c)) ^ 2) - (4 * ((x ^ 2) + (y ^ 2)))) / 2))
 t2 = Sqr((-(1 - (2 * c)) - Sqr(((1 - (2 * c)) ^ 2) - (4 * ((x ^ 2) + (y ^ 2)))) / 2))
gives
 t1 =  4.4158573916999
 t2 =  2.5495496653664

what am i doing wrong?

the formalas above have been converted to "vbscript annotation" hope it is not too confusing
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, your math worked out perfectly and in combination with ozo's second post I was able to implement it in practice to calculate the two possible trajectories for a given destination, as well as the time it will take for the "projectile" to arrive there. Very much appreciated!
x² + (y - c * t²)² = t²

x² + y² - 2*c * t² + c² * (t²)² = t²

x² + y² - (2*c +1)* t² + c² * (t²)²
c² * (t²)² - (1+2c) t² + x² + y² = 0
(t²) = ((1+2c) ±sqr((1+2c)² - 4(x² + y² )c² )/(2c²)
which is solvable if (1+2c)² > 4(x² + y² )c² and sqr((1+2c) - 4(x² + y² )c²) > -(1+2c)
Jason,

Thanks for the version 1 points.

I understand what you want to accomplish with those equations, but I am not sure you've phrased it out in the correct way in both versions. For a realistic "trajectory" (!), your Vx and Vy should actually be Vx0 and Vy0, which are initial speeds along horizontal and vertical axes, according to your formulation. c is a constant, which is actually g, 9.8, in most cases. In a case like that, Vxt at any time t always equals to Vx0, but Vyt is a variable which is affected by the value of c. Vx0 and Vy0 can be any combinations that satisfy the restriction from your 3rd equation -- sqr( Vx0² + Vy0² ) = 1, and the number of possible combinations are infinite.

Lei
PS: I think you somehow got confused between the initial speeds Vx0 and Vy0, and the speeds at time t, Vxt and Vyt. If you have an accelerating factor of c, which will acts on either Vx or Vy, or both. If you re-formulate your equations carefully with clear distinction between these variables and constants, the problems can be really easy to solve almost effortlessly. Please let me know if I can be of further help.

ozo, sorry that still does not test out. your formula is very close to hagmans:
t² = ( (2 c y +1) +- sqrt( (2 c y +1)² - 4 c² (x²+y²)) )/(2c²)
for easy comparison yours is equivalent to (i think):
t² = ( (2 c   +1) +- sqrt( (2 c   +1)² - 4 c² (x²+y²)) )/(2c²)

sqr(vx²+vy²) using your formula gives me values that are well off 1, while hagmans formula always results in 1 for both t-values using your formulation:

vx = x/t
vy = (y - c * t²)/t

hagmans formulation has already proven itself in practice (it checks out perfectly every time) so i'm considering the problem solved - just thought i'd let you know. still mighty good work tho, thanks for your help!

leiz,

Regarding "version 1" I made a mistake and the formulas were incorrect alltogether. That formula would have resulted in a linear trajectory which is nothing like the code from which it was taken. You are right that vx and vy are initial speeds (or velocity) They are the result of normalization of the target offset in the game, ie vx|y = target_x|y / sqr( target_x² + target_y² ). They might as well have been (and for bot code they are) derived from an angle using cos/sin which is why sqr( vx² + vy² ) is always 1. Did I say c was a constant? Sorry about that - not true. c is not g (since you write 9.8 i am guessing you mean gravitational accelleration?) c in this instance is the "curvature" of the weapon that was used to fire the "projectile". For example the "rifle" has a curvature of 2, while the "grenade launcher" has a curvature of 8. t is actually "real world" time * speed in the game since different weapons have different speeds as well. since it was not necessary to solve the question i ommitted this part.

While I am not very good with mathematics I did perform a good deal of simulations while waiting for an answer to this post, and I found that there seemed to always be exactly two possible initial trajectory angles (vx,vy) for which the trajectory intersected each exact point in space. This seems to be verified by hagmans and (almost) ozos formulations so I can only assume that they are correct and that there are not an unlimited number of possibilities.

What I was trying to accomplish was to calculate the "perfect" targeting (velocity) vector at which to fire a projectile using a given weapon (c) and the target location. Since the target is moving I am now using an iterative algorithm that takes the average of the trajectory time (t from hagmans formula) and the target predictor time (tp) and feeds this back into the target predictor (which results in the variables x and y) until the two times match up closely enough (tp=t), then I calculate vx and vy using vx = x / t, vy = ( y - c * ( t * t ) ). The result is an absolute perfect hit each and every time, given that the prediction parameters for the target do not change while the projectile is in transit - it is after all impossible to tell the future.

My post may seem confused, I am good at formulating code - not math =) but I assure you I am not confused as to the difference between initial speed and speed at time t. Speed at time t is never calculated and never comes into play for this particular problem. I erroneously thought that there was a solution for every x, y and t which is why I asked about a solution "for a given value of t" but as ozo pointed out the solution to this was just as easy as it seemed - and pointed in the direction of calculating t instead. As it turns out this was what i really wanted. The fact that there are two possible trajectories for each target location was an added bonus and provides me with a slower "fallback" trajectory that I can use when the target is behind a wall that will block the direct trajectory =)

Thanks everyone for your help!
Oh and when I said vx|y = target_x|y / sqr( target_x² + target_y² ) i meant "target" as in where the user/player is aiming - like a cursor of sorts.

When I wrote "target" while explaining the iterative algorithm I am referring to the opponent players position. The target predictor is an algorithm that predicts an opponents location into the future given that particular opponents input and the world geometry.

Sorry, did not mean to confuse - I always manage to do just that =)
Hope you got what I was trying to explain anyways...