-1

Instead of typing it all out, here's a picture for you to see what I want. You know what they say, a picture is worth a thousand words.

img

What I have is p1, Θ, and d and speed, s of the projectile.

Speed:

From this I can deduce p2 using the equation p1.x * speed, p1.y * speed which are the co-ordenates for p2. From this I can calculate the distance using the equation |dx| + |dy|.

Distance

However, if I wasn't given speed, how would I be able to calculate the co-ordenates of p2 only using Θ, p1 and d?


Are there any other methods that would be the most efficent?


 public double getDistanceTraveled() {
    return Math.abs(x - oldX) + Math.abs(y - oldY); 
 }  //use Manhattan aproach as it is more efficent than Euclidean
 public double getSpeed() {
    return getDistanceTraveled() / level.TICKS_PER_SECOND; 
 }

is what I am using at the moment. I realized that I had the variable time, so I was able to deduce p2 using this method:

d = |dx| + |dy|
d = s / t
s = t(|dx| + |dy|)

Not sure if this is that efficient though. Any suggestions to this problem, and just to repeat: I have the variables theta, p1, d and t and I have to find p2.

Efficency >>> Accuracy

nyxaria
  • 479
  • 1
  • 3
  • 13
  • 1
    You say that efficiency is more important than accuracy, but _how much_ more? It's pretty efficient to say that p2 = p1, for example, but it's unclear if that's useful to you, because it's probably not very accurate. What are the bounds on the accuracy? – Andy Turner Jan 24 '15 at 21:09
  • @AndyTurner I have to run 30 cycles a second and not have it throttle. This includes other calculations, ofcourse – nyxaria Jan 24 '15 at 21:10
  • 7
    30 cycles per second is nothing. Unless you need at least 100,000 such computations per second, so long as you are using a formula, use whichever one is more intuitive. In fact, even 100,000 would be blazing fast on most modern computers. – Sergey Orshanskiy Jan 24 '15 at 21:14
  • 1
    @nyxaria - that's a bound on the time, what are your accuracy requirements? – Andy Turner Jan 24 '15 at 21:36

3 Answers3

1

If theta is the angle between line and y-axis.
Then you can calculate it with the following formulas.
Let P1 be the point (p1x, p1y). and P2 be the point (p2x, p2y)

p2x = p1x + d * sin(theta)
p2y = p1y + d * cos(theta)

If you use Math.sin(theta), then keep in mind that theta should be in radians. You can use Math.toRadian(degree) to get angle in radians.

tom10
  • 60,398
  • 8
  • 116
  • 129
afzalex
  • 8,254
  • 2
  • 29
  • 53
  • 1
    Did you mean for `p2y` to be on both the left and right sides of the second equation? – VGR Jan 24 '15 at 21:32
  • If theta is angle between x-axis and line then yes it should be swapped. But from the image that he shown it seems to be angle between line and y-axis. @tom10 – afzalex Jan 24 '15 at 21:56
0

You know what they say, a picture is worth a thousand words.

enter image description here

Adam Stelmaszczyk
  • 18,835
  • 4
  • 63
  • 104
0

If you want efficiency, there's actually a much better approach than using the trig operations at all. Basically, since you already have d, the only thing you care about is the relative movement along x and y, which is defined by theta.

Knowing that, it's pretty trivial to just store unit vectors at whatever your required granularity is in a hashmap and multiply by d

After you've decided on your granularity, just pre-calculate a HashMap like so:

{0: [1, 0],
 0.01: [0.99, 0.001], etc...
}

Then round the incoming number, see this question for details on making sure it matches your granularity. Then the calculation is just (in pseudo-code):

x = p1[0] + d * <HashMap>.getKey(angle)[0]
y = p2[1] + d * <HashMap>.getKey(angle)[1]

A HashMap lookup and a multiplication is WAY more efficient than even a single trig operation. Preliminary tests got me ~10x speedup but YMMV.

Community
  • 1
  • 1
Slater Victoroff
  • 19,762
  • 18
  • 78
  • 135
  • 1
    There's no need to involve hash maps where a simple array would do. If the granularity is uniform, then the array index can be derived very simply. – Marko Topolnik Jan 24 '15 at 22:01
  • @MarkoTopolnik, Ah, my mistake, I forgot that hashmaps are built on arrays in Java, I usually dev in python where the reverse is true – Slater Victoroff Jan 25 '15 at 00:20