• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I think seeing the code where the calculation is made and used would be helpful, and be sure to UseCodeTags (← that's a link).
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The calculation is in the first post, but I can try to include the code that calls for the calculation and then makes use of it:






The object that is created inherits from the Projectile class, which handles all such objects and sets its direction based on the created angle:



The object is then finally moved in a class called MapObject, which Projectile inherits from:


Sorry if it's a bit much, I'm not entirely sure what code to show/not show since the most important part is in the calculateAim. =/
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. =/

Edit:
Found this piece:
webpage

It explains the right triangle that you were talking about, but as you can see, that's what I was already doing:




Which confuses me even more, as to why it works for them hm...
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lora Twinblade
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
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. =)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!