- » Help Me !
- » How do I get an angle in (x,y) between two points?
Topic review (newest first)
- bob bundy
- 2012-12-30 04:20:07
Glad you got it sorted. Now just add the sound of bullets whizzing thru the air ......
- 2012-12-30 02:16:15
Ahh ha.. I got it...
dx = x2-x1;
dy = y2-y1;
goal distance(gd) = Math.sqrt( (dx * dx) + (dy * dy) );
ratio = speed / gd;
ax = ratio * dx;
ay = ratio * dy;
Just had to square the distance, get the square root and then apply a ratio for speed...
Thanks again bob..
- 2012-12-30 00:05:44
Basically I need to plot out a rise over run to meet an angle.. The intersecting point on the line from origin to destination would be the product of the sum of the rise pluss the run which would be an distance equivalent to the speed.
2 ..... X speed(ax,ay)
| / :
| / :
(the doted lines can't be straight they are a circle)
(ax+ay) = 4 (total speed)
But ax and ay will change based on the quadrant the destination is in and its angle from the origin.
I cant make a circle but line(OD) will always intersect the circle around the origin based on the speed, which would be the circles radius, and follow the line no matter what angle..
This should be an easy equation and I'm sure its right in front of my face but I can't think of it.. Not enough math practice.
| D2 D3
| > . D4
| > .
'X' would be where 4 would land, which would be wrong.. the '>' would be where ax,ay would need to land in order for the speed to remain the same regardless the Destination 'D'
I cant draw the lines but imagine them from the Origin passing threw a '>' (on the circle radius of speed) to reach a 'D' destination point..
- 2012-12-29 23:43:35
Your angle never changes.. In each step you have to increase or decrease from the origin to the destination, if each step for ax or ay is always 4 then the angle is always the same.. Which isn't true in every instance.
That is why I named them ax (angle x) and ay (angle y) because they are representing velocities to the angle needed to reach the destination. In the simplest terms, I'm thinking ax should never be more then 2 or less then -2, (well I guess they could as long as they are together not more then 4) same for ay. Adding ax to ay it should always be an equivalent of 4, as that is a representation of the speed.
If you use my second equation you can see that's not true, they are not the same and the overall speed is ax+ay ax = -1.18125 ay = -0.79375 total = -2.60625 speed
but the speed changes when the distance is further.. ax = -3.47125 ay = -2.84125 total = -6.3125 speed
I need the bullets to be the same, overall, speed but still reach the destination. 4 would be nice but again there would be no angle and so miss the target...
p.s. Just an example, if ax was 1.25 and ay was 2.75 the total speed would be 4 and so a constant speed with a different angle then
if ax was 2.37 and ay was 1.63 the speed would be the same but the angle would be different and so be what I'm looking for..
- bob bundy
- 2012-12-29 19:14:10
I've checked the calcs and my figures are the same for the first two data sets.
ax and ay should always give 4 so the rest are not right. ???
My plan was to split the distance to be covered into n steps. With distance = 400 and s = 40, that would be make t = 10 and n = 100 steps.
So if x1 = 650 and x2 = 250, the distance to be covered is -400, so each step would be an increment of -4
With x1 = 1050 and x2 = 250 D = -800, s=40, t = 20, so n = 200. Each step = -4
What happens if you ignore all the sums and just step -4 each time?
- 2012-12-29 17:55:20
I think the compiler is changing the number because its out of range or something.. I tried var:Number and var:Int .. had no effect on the number.. but I'm not sure why one is out of range and the other isn't..
- 2012-12-29 17:38:54
Another example of the second equation... ax = (x2-x1)/40; a bullet near and far...
x1 = 297.25 y1 = 231.75 x2 = 250 y2 = 200 ax = -1.18125 ay = -0.79375
x1 = 388.85 y1 = 313.65 x2 = 250 y2 = 200 ax = -3.4712500000000004 ay = -2.8412499999999996
- 2012-12-29 17:26:08
ok I tried it again.. and this is way off not even hitting target..
ax = (x2-x1)/(tx/0.1);
ay = (y2-y1)/(ty/0.1);
read out for two different bullets..
x1 = 307.55 y1 = 320.45 x2 = 250 y2 = 200 tx = -1.4387500000000002 ty = -3.0112499999999995 ax=4 ay=4
x1 = 402.85 y1 = 20.4 x2 = 250 y2 = 200 tx = -3.8212500000000005 ty = 4.49 ax=4 ay=4
Yet this does hit target but the speed is still changing..
ax = (x2-x1)/40;
ay = (y2-y1)/40;
x1=255.35 y1=2.55 x2=250 y2=200 ax = -0.13374999999999987 ay =4.936249999999999
x1=197.65 y1=63.5 x2=250 y2=200 ax = 1.3087499999999999 ay = 3.4125
- bob bundy
- 2012-12-29 01:22:50
because the distance is changing
t is computed from the distance and fixed speed so when the distance is small the travel time should be small.
When the target is near are the bullets going faster or slower?
Can you 'dump' your variables for say two bullets only, so you can check the calculations. Get a print out of all the x positions, the value of t, the value of s. Then it may be obvious what is going wrong.
- 2012-12-29 00:54:31
I guess I shouldn't be complaining because the bullets are right on target.. but I just think it looks strange when the bullets are traveling at different speeds.
- 2012-12-29 00:51:31
You would think so but its not happening.. and I'm not sure why.. like I said the only thing I can think of is because the distance is changing..
- bob bundy
- 2012-12-29 00:43:25
That should give a constant speed of s.
- 2012-12-29 00:39:29
as in.. bullet one is shot when x1 is relatively close to x2, the speed of the bullet is slow.... but bullet 2 is shot when x1 is far from x2 and the speed is fast.. I want both bullets to be traveling at the same speed, regardless of the distance.
p.s. and still be on target..
- 2012-12-29 00:36:21
Ok lets just substitute time with speed...
would be right but the problem is because the distance changes between x2 and x1 (more then one bullet being shot in each instance) the speed changes for each bullet... and I don't want it to...
- bob bundy
- 2012-12-29 00:10:41
I'll have to introduce my own letters.
Let (x1,y1) be the coordinates of the gun.
And (x2,y2) the coordinates for the target.
Say you want to hit the target in t seconds, advancing the action in 0.1sec jumps.
Then the number of jumps is t / 0.1
The x distance to be covered is x2 - x1, so each jump must be (x2 - x1) / ( t /0.1) in length.
So position one is x1
Position 2 is x1 + (x2-x1) / (t / 0.1)
Position 3 is x1 + 2 times (x2-x1) / (t /0.1)
Last position is x1 + (t/0.1) times (x2-x1) /(t/0.1) = x1 + (x2-x1) = x2 as required.
The ys can be worked out similarly.