Calculating angle between two points and moving from one to the other?

Greetings, first time posting here, so I wasn't exactly sure whether this should be in the beginning Java section or the more advanced one...

So, what I'm trying to do should be fairly simple, I have two characters, both have X and Y coordinates, I wish to calculate the angle between these two points and move an object from the first point to the second one.

When I looked around, I kept seeing this posted as the solution:

Where locationX and locationY is the start point and character.locationX and character.locationY is the destination point.

As I understand it, if I wish for a object to travel from the starting point of locationX, locationY I would do so by advancing with:

However, the actual result appears to be quite random...

When testing, the starting point remains the same and never change, whilst I move the end point around in the near area for the starting point, at times the starting point is aiming in the opposite direction, at times directly at it, but even then if I move the end point just a little bit to the left/right it starts aiming in a completely different direction, nowhere near correctly...

Any chance anyone here knows the proper form for my desired result? I'm at a complete loss as to what I'm doing wrong.

If there is any more code you wish to see, just ask and I'll provide it, however I don't think any other code is relevant to this issue since this is the only part meant to calculate the actual direction I'm after.

Oh, and to clarify:
aim, locationX and locationY are all variables of the type double, just in case that was important to know.

Do you just need to get from point A to point B? I would just do it this way -- if point A is (27,93) and point B was (15,105) I would make my first move to (26,94) then to (25,95) and so on. You could make up some proportionality function to make it approximate a straight line better if you needed to. The slick geometric way would be to make the two points the hypotenuse of a right triangle, and at that point you could beat on it with trig functions.

Lora Twinblade

Greenhorn

Posts: 10

posted 2 years ago

Guillermo Ishi wrote:Do you just need to get from point A to point B? I would just do it this way -- if point A is (27,93) and point B was (15,105) I would make my first move to (26,94) then to (25,95) and so on. You could make up some proportionality function to make it approximate a straight line better if you needed to. The slick geometric way would be to make the two points the hypotenuse of a right triangle, and at that point you could beat on it with trig functions.

I have two positions, say: (240, 500) and (270, 700), and I want to calculate the angle between these two points and then have a object move from the first to the second. I'm not entirely sure how I would do what you suggested though...

I was saying that as far as I can tell you don't need any angle. Why not just move from point A to point B, since they are both known.

Lora Twinblade

Greenhorn

Posts: 10

posted 2 years ago

Because I need to move towards point b, but I don't know how to do that without following an angle.

I move the object by setting its directionX and directionY values,

(0, 0) is the top left of the coordination field, if directionX is positive then the object moves towards the right, if it's negative it moves towards the left. Same goes for directionY, if it's positive it moves downwards, if it's negative it moves upwards, but in order to set these two values so that the object ultimately moves towards the destination, I need an angle for it to follow, no? That's what I gathered when I searched around. =/

Lora Twinblade wrote: As I understand it, if I wish for a object to travel from the starting point of locationX, locationY I would do so by advancing with:

However, the actual result appears to be quite random...

First, welcome to the ranch.

You have two possible issues. One. Sine and Cosine are ratios, so to get exact values, you will need to multiply it with the length between those two points. Two. The sin() and cos() methods takes radians. You converted it to degrees. So, you need to convert it back to radians, or better yet, don't convert it to degrees in the first place.

Lora Twinblade wrote: As I understand it, if I wish for a object to travel from the starting point of locationX, locationY I would do so by advancing with:

However, the actual result appears to be quite random...

First, welcome to the ranch.

You have two possible issues. One. Sine and Cosine are ratios, so to get exact values, you will need to multiply it with the length between those two points. Two. The sin() and cos() methods takes radians. You converted it to degrees. So, you need to convert it back to radians, or better yet, don't convert it to degrees in the first place.

Henry

Ah thank you!

After I stopped converting it into degrees but rather left it as ratios it started working flawlessly!

Thank you all for your help, I'll try to read up a bit more on this and see if I can understand it even better. =)

I've got no option but to sell you all for scientific experiments. Or a tiny ad: