Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2012-12-27 13:58:47

greenbug
Member
Registered: 2012-12-27
Posts: 18

### How do I get an angle in (x,y) between two points?

I'm not sure how to word this question.. I know the speed I want... but I want, not just the the angle from Opoint to Dpoint but the x,y vector if they are moving at some speed S..

the Origin point is at some angle (x,y) to destination point. What would be the angle in (x,y)?  I'm amusing x and y would be a single digit numbers and because the destination would change either positive or negative in value. I'm coding in C++ and it works but it has the wrong speed, I need to apply the speed to the angle (x,y) to get my vector.  How is this done? Or what other wording do I need to express this?

current code, which is wrong but it works for now, but the speed is wrong when the distance between the two points increases. ax = -((x-destination.x)*16)/(sw+1)     ay = -((y-destination.y)*12)/(sh+1)
I don't think I need the screen width or height but its working this way.. and the multiples 16 and 12 are a magnitude of some sort, again I narrowed it in to get them and they would be pointless if I can just get the angle in x,y...

Dp
|         /
|       /
|  a(x,y) *speed   = vector
Op______

Angle (x,y) I guess is unknown and would be dependent on the speed to make it a vector differential? but what would (x,y) be if I just wanted it in the simplest of terms? Because then I could just multiply that with the speed for the vector I need. I tried looking it up but I keep finding atan2 giving the angle, which isn't the problem, how do I express that angle in (x,y)?

I need angle x and angle y to figure out the differential , if I'm applying s for speed.. I complicate things sorry..

Ax
∑ Ay
s->

lol or would that be

O.x,O.y
∑ D.x,D.y
s->

Last edited by greenbug (2012-12-27 14:09:34)

Offline

## #2 2012-12-27 14:15:05

mathaholic
Member
From: Earth
Registered: 2012-11-29
Posts: 3,251

### Re: How do I get an angle in (x,y) between two points?

Welcome to the forum, greenbug.

Mathaholic | 10th most active poster | Maker of the 350,000th post | Person | rrr's classmate

Offline

## #3 2012-12-27 14:15:53

greenbug
Member
Registered: 2012-12-27
Posts: 18

Thank you..

Offline

## #4 2012-12-27 14:19:45

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

i guess the simplest terms would be a.x = O.x - D.x  and y.x = O.x-D.x  ... then (a.x,a.y)* Speed = vector... as I'm thinking about it...

Offline

## #5 2012-12-27 14:27:04

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

But that's not right.. I even tried dividing by the screen width and height .. then applying the speed... its still off and the original is still better.
not working.... I guess the magnitude in the original would be the speed.. and again I'm dividing by the screen width... what it appears is happening is that the speed still changes on the distance they are apart, so o.x - d.x   is giving me the distance they are apart.. I want perhaps the square root of that? or something??  I dont want the distance between o.x and d.x ... but the vector angle so.. vectorO, vectorD and vectorA ??? figure out vector angle if it's position is dependent on the speed..?? humm...

Last edited by greenbug (2012-12-27 14:40:13)

Offline

## #6 2012-12-27 21:41:40

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

hi greenbug,

Welcome to the forum.

I'm a bit confused about what you want.  I've made a diagram (see below, click on it to see it full size).  Perhaps you can explain using that.

Meanwhile here are some mathematical formulas which may help.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #7 2012-12-28 00:35:41

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

The program is a bullet and target. What I want is a constant velocity(vx,vy) for an object(bullet) moving from an origin point to a destination.. It should be simple rise over run and then and something for the speed of the object(bullet).. What I'm using changes the speed of the object(bullet) and that's not right, the speed changes because the distance between the origin and destination change. What I want is a constant speed. So now I'm thinking I need to?? divide the speed into the equation to give each step a constant speed regardless of the distance??

Offline

## #8 2012-12-28 00:50:37

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

I don't want the angle... The velocity will form the angle.. as the object moves threw each point until it hits the target. But I need to know how to calculate the velocity for both x and y so they together move each step till they meet the target. .. like step 1)  x + .01, y + .002, step 2) x+ .01, y+.002 .. and so on.. I know how to calculate the angle but I don't know how to find the values to add to x and y  a.e. the .01 and .002... for the constant velocity.

Offline

## #9 2012-12-28 00:54:45

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

OK.  One more question then.

Do you want to allow for the effect of gravity on the bullet?  Snipers have to aim high to allow for this.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #10 2012-12-28 01:01:46

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

the bullet needs to travel,, for x ... Dx - Ox = total distance... Now that distance changes so if I divide by a constant the speed should be constant, right? (Dx-Ox) / sw  like the screen width...  and same for y .. (Dy - Oy)/ sw ... and I can change the speed by adding or subtracting from screen width...  or what I did original was change the value of the total distance.. But I think there should be another way to do that..

Offline

## #11 2012-12-28 01:09:03

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

but I just tried this and its not working... its close.. but the speed of the bullet still changes the further away the faster the bullet is... (btw it still misses)
ax = ((target.x-x)/(sw+1))*10;
ay = ((target.y-y)/(sh+1))*10;

I can't negate the distance from change the speed of the bullet.

Last edited by greenbug (2012-12-28 01:10:59)

Offline

## #12 2012-12-28 01:10:41

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

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.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #13 2012-12-28 01:36:21

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

Ok lets just substitute time with speed...
so
x1+(x2-x1)/s

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...

Last edited by greenbug (2012-12-28 01:37:23)

Offline

## #14 2012-12-28 01:39:29

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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..

Last edited by greenbug (2012-12-28 01:44:58)

Offline

## #15 2012-12-28 01:43:25

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

That should give a constant speed of s.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #16 2012-12-28 01:51:31

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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..

Offline

## #17 2012-12-28 01:54:31

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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.

Offline

## #18 2012-12-28 02:22:50

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

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.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #19 2012-12-28 18:26:08

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

ok I tried it again.. and this is way off not even hitting target..
equation

speed=40;
tx=(x2-x1)/speed;
ty=(y2-y1)/speed;
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

Last edited by greenbug (2012-12-28 18:26:43)

Offline

## #20 2012-12-28 18:38:54

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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

Offline

## #21 2012-12-28 18:55:20

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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..

Offline

## #22 2012-12-28 20:14:10

bob bundy
Registered: 2010-06-20
Posts: 8,155

### Re: How do I get an angle in (x,y) between two points?

hi greenbug

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?

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #23 2012-12-29 00:43:35

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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..

Last edited by greenbug (2012-12-29 00:55:39)

Offline

## #24 2012-12-29 01:05:44

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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.

|
|             D(x,y)
|           /
|         /
2 ..... X  speed(ax,ay)
|     /  :
|   /    :
O(x,y)  2___________________________

(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.

| D1
|           D2        D3
|> ...........X
|      >       .            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..

Last edited by greenbug (2012-12-29 02:40:16)

Offline

## #25 2012-12-29 03:16:15

greenbug
Member
Registered: 2012-12-27
Posts: 18

### Re: How do I get an angle in (x,y) between two points?

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..

Offline