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.
I think seeing the code where the calculation is made and used would be helpful, and be sure to UseCodeTags (← that's a link).
All things are lawful, but not all things are profitable.
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 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 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...
 1
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
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. =)
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
