Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

I currently have this code:

The aim of this code is to correctly find the angle in degrees from a central point to the outside of the point. You can find more on the question here: http://math.stackexchange.com/questions/845063/how-to-calculate-the-rotation-between-2-coordinates

That question I linked you to has got a perfectly good mathematical answer, and I believe that is what I have implemented into my code, however it ALWAYS seems to be returning 1.5 no matter what I do? I could have potentially translated the math incorrectly into java code, or just got entirely the wrong thing.

Either way, this is specifically a programming issue as it is either an issue with turning a mathematical function into code or fixing code logic, which is why I decided to post the question here.

The desired result is that it returns the correct value. Wheras currently, it does not.

What is wrong? Have I translated the maths incorrectly into java? Do I have faulty code? Or what?

(I know that specific debugging questions are harder to solve, however I think that it is simply something simple like not translating the maths correctly, however I just cannot find it.)

Thanks!

P.S. I tried both StackExchange and GameDev for this question. Very unfriendly & unhelpful. I hope I can get some help here .

The aim of this code is to correctly find the angle in degrees from a central point to the outside of the point. You can find more on the question here: http://math.stackexchange.com/questions/845063/how-to-calculate-the-rotation-between-2-coordinates

That question I linked you to has got a perfectly good mathematical answer, and I believe that is what I have implemented into my code, however it ALWAYS seems to be returning 1.5 no matter what I do? I could have potentially translated the math incorrectly into java code, or just got entirely the wrong thing.

Either way, this is specifically a programming issue as it is either an issue with turning a mathematical function into code or fixing code logic, which is why I decided to post the question here.

The desired result is that it returns the correct value. Wheras currently, it does not.

What is wrong? Have I translated the maths incorrectly into java? Do I have faulty code? Or what?

(I know that specific debugging questions are harder to solve, however I think that it is simply something simple like not translating the maths correctly, however I just cannot find it.)

Thanks!

P.S. I tried both StackExchange and GameDev for this question. Very unfriendly & unhelpful. I hope I can get some help here .

Campbell Ritchie

Marshal

Posts: 56529

172

posted 3 years ago

- 1

Welcome to the Ranch

I have added code tags to your post: always use them. Don't they make it look better

It is difficult to assess the code because you are using lots of classes I am unfamiliar with, but I can see some serious errors.

You appear to have objects with non‑private fields. Unless you are using a field as a global constant, all fields should have private access. Access

You are writing empty catch blocks. That means you could have an Exception and know nothing about it.

Less serious: your variable names are poor; nobody can read the code and work out what cl means.

I suggest you start by running the algorithm with plain simple numbers. Not fields of other objects. Also write out the formula and then use that to work out the algorithm.

There is something peculiar about

I have added code tags to your post: always use them. Don't they make it look better

It is difficult to assess the code because you are using lots of classes I am unfamiliar with, but I can see some serious errors.

You appear to have objects with non‑private fields. Unless you are using a field as a global constant, all fields should have private access. Access

*via*getXXX methods is much better.You are writing empty catch blocks. That means you could have an Exception and know nothing about it.

Less serious: your variable names are poor; nobody can read the code and work out what cl means.

I suggest you start by running the algorithm with plain simple numbers. Not fields of other objects. Also write out the formula and then use that to work out the algorithm.

There is something peculiar about

*x*+ sin(20*x*); is that actually part of the formula?
Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

Could you please tell me what specifically is wrong? I am getting so many confusing/conflicting answers from so many different sites that I am no longer sure what is actually correct any more.

The "weird sine stuff" is just me getting a location in front of the ship. It is not part of the mathematical equstion.

cl is just a Client/Player. I can give you the entire application if you want, but I don't think you want to sift through 2000+ lines of code...

The terrible variable names is because I was getting the names from the mathematics forum. I should fix that.

I get your point about getXXX. Again, I should probably implement that, however it is not how I was taught to use code, and so stuff like that is often neglected out of habit.

The exception is because I can get an ArrayIndexOutOfBoundsException when the ship goes off the map, which is possible to happen. If one player somehow managed it, I would not want it to crash the server.

So, how exactly have I not done the equation correctly? I can't seem to work it out.

Thanks for the help!

The "weird sine stuff" is just me getting a location in front of the ship. It is not part of the mathematical equstion.

cl is just a Client/Player. I can give you the entire application if you want, but I don't think you want to sift through 2000+ lines of code...

The terrible variable names is because I was getting the names from the mathematics forum. I should fix that.

I get your point about getXXX. Again, I should probably implement that, however it is not how I was taught to use code, and so stuff like that is often neglected out of habit.

The exception is because I can get an ArrayIndexOutOfBoundsException when the ship goes off the map, which is possible to happen. If one player somehow managed it, I would not want it to crash the server.

So, how exactly have I not done the equation correctly? I can't seem to work it out.

Thanks for the help!

posted 3 years ago

I have told you where to look for one specific error but there is much more wrong with your code and you need to learn to double check your code against a specification.

P.S. Posting to multiple sites ( http://stackoverflow.com/questions/24392867/what-is-the-error-with-this-code ) is considered bad form since it means that I could spend effort helping you in one site and someone else could be wasting their time in another.

Joe Thatcher wrote:Could you please tell me what specifically is wrong?

I have told you where to look for one specific error but there is much more wrong with your code and you need to learn to double check your code against a specification.

P.S. Posting to multiple sites ( http://stackoverflow.com/questions/24392867/what-is-the-error-with-this-code ) is considered bad form since it means that I could spend effort helping you in one site and someone else could be wasting their time in another.

Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

In regards to your edit about me posting on multiple sites, that topic had already been put on hold before posting on here. I usually do not post on two sites at the same time unless one answer has either not been answered in 2 days or unless people are unable to answer from that site.

posted 3 years ago

All you have to do is come clean and indicate which other sites you have posted to then responders can check those sites to make sure they are not wasting their time.

P.S. I suspect you don't actually get 1.5 but something close to PI/2 which is equivalent to 90 degrees.

Joe Thatcher wrote:In regards to your edit about me posting on multiple sites, that topic had already been put on hold before posting on here. I usually do not post on two sites at the same time unless one answer has either not been answered in 2 days or unless people are unable to answer from that site.

All you have to do is come clean and indicate which other sites you have posted to then responders can check those sites to make sure they are not wasting their time.

P.S. I suspect you don't actually get 1.5 but something close to PI/2 which is equivalent to 90 degrees.

Campbell Ritchie

Marshal

Posts: 56529

172

posted 3 years ago

Let's get back to the original subject.

What is the exact formula? When I went to the link you posted, there was something with cos⁻¹ in; that obviously corresponds to the Math.acos call on 18. Why are you using toRadians after that? Surely the following formula doesn't give you an angle to convert to radians, but a cosine?

What is the exact formula? When I went to the link you posted, there was something with cos⁻¹ in; that obviously corresponds to the Math.acos call on 18. Why are you using toRadians after that? Surely the following formula doesn't give you an angle to convert to radians, but a cosine?

posted 3 years ago

There are at least two major things wrong with that implementation -

1) the calculation of 'alength' and 'blength' are wrong,

2) the toRadians() within the acos() is wrong

This second fault leads me to surmise that the OP wants the angle in degrees rather than radians and assumes that the toRadians() method applied within the inverse cosine will actually 'inverse' the radians and make them degrees.

The OP should note that -

a) this inverse cosine approach is error prone when the angle is close to zero and an implementation based on inverse sine or inverse tan is more accurate,

b) this will only give angles between -PI/2 and +PI/2 and it is likely that the OP will need angles between -PI and +PI (this can be achieved using a 'half angle' approach based on inverse tan using the atan2() function),

c) it is better to stick with radians throughout and only convert values to degrees when displaying,

d) it has been pointed out the code discards without comment any exceptions which may be a good thing since it deals with the situation where one (or both) of the points is at the origin since this will produce a divide by zero.

Implementation without understanding is always dangerous and this thread is a prime example.

- 2

Campbell Ritchie wrote:

What is the exact formula? When I went to the link you posted, there was something with cos⁻¹ in; that obviously corresponds to the Math.acos call on 18. Why are you using toRadians after that? Surely the following formula doesn't give you an angle to convert to radians, but a cosine?

There are at least two major things wrong with that implementation -

1) the calculation of 'alength' and 'blength' are wrong,

2) the toRadians() within the acos() is wrong

This second fault leads me to surmise that the OP wants the angle in degrees rather than radians and assumes that the toRadians() method applied within the inverse cosine will actually 'inverse' the radians and make them degrees.

The OP should note that -

a) this inverse cosine approach is error prone when the angle is close to zero and an implementation based on inverse sine or inverse tan is more accurate,

b) this will only give angles between -PI/2 and +PI/2 and it is likely that the OP will need angles between -PI and +PI (this can be achieved using a 'half angle' approach based on inverse tan using the atan2() function),

c) it is better to stick with radians throughout and only convert values to degrees when displaying,

d) it has been pointed out the code discards without comment any exceptions which may be a good thing since it deals with the situation where one (or both) of the points is at the origin since this will produce a divide by zero.

Implementation without understanding is always dangerous and this thread is a prime example.

Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

Thanks for the great replies . This has currently been the only site that has actually give me friendly, helpful replies.

I currently have the following code, but it still isn't working:

This does still not work, and returns something like 62. I am probably still doing something obviously stupid/wrong, but I can't seem to find it now. I think that the distance and the rotation thing is now fixed, though I am somewhat confused about this Radians business. Which methods actually are requiring Radians? How can I find out?

While not directly ontopic for this thread, I also fixed an error where the graphical world was a slightly different size than the collision world, making irregularities.

*About 10 minutes of testing later*

It varies between about 59 and 66. I still cannot find out what the actual issue is.

As you can see, it isn't very realistic-looking currently.

I currently have the following code, but it still isn't working:

This does still not work, and returns something like 62. I am probably still doing something obviously stupid/wrong, but I can't seem to find it now. I think that the distance and the rotation thing is now fixed, though I am somewhat confused about this Radians business. Which methods actually are requiring Radians? How can I find out?

While not directly ontopic for this thread, I also fixed an error where the graphical world was a slightly different size than the collision world, making irregularities.

*About 10 minutes of testing later*

It varies between about 59 and 66. I still cannot find out what the actual issue is.

As you can see, it isn't very realistic-looking currently.

posted 3 years ago

Without any idea of what your test data is we don't stand a change of helping you any further. you should test this code outside your application using an SSCCE ( http://sscce.org/ ) then you can post that when you have problems. Your Big Bang approach make debug by you difficult and impossible for us.

Everything is radians within the trig functions and if you can't visualise radians ( few can ) then only convert to degrees when you need to view a value.

Joe Thatcher wrote:

I currently have the following code, but it still isn't working:

Without any idea of what your test data is we don't stand a change of helping you any further. you should test this code outside your application using an SSCCE ( http://sscce.org/ ) then you can post that when you have problems. Your Big Bang approach make debug by you difficult and impossible for us.

Which methods actually are requiring Radians?

Everything is radians within the trig functions and if you can't visualise radians ( few can ) then only convert to degrees when you need to view a value.

Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

Err ... why should I do that?

Joe Thatcher wrote:Here is some code you should potentially be able to put directly into your IDE without classes you do not understand.

Err ... why should I do that?

**You**should write an

**SSCCE**that includes that code and also includes a set of

**test data**with

**values you expect it to produce**and

**you**should indicate what you actually get.

Joe Thatcher

Greenhorn

Posts: 15

Campbell Ritchie

Marshal

Posts: 56529

172

posted 3 years ago

- 1

What the SSCCE page means is that you should provide short simple code which actually runs. We still can't run your last example because we don't have access to the Vector3f class.

I think you need to take drastic action, turning your computer off and getting pencil paper and eraser. The latter is very important.

Write down the formula and very slowly work it into a format which you can convert to code. Also take the advice you were given on SO aboutdoing the calculations with a calculator. When you get to this sort of thing you are winning

I think you need to take drastic action, turning your computer off and getting pencil paper and eraser. The latter is very important.

Write down the formula and very slowly work it into a format which you can convert to code. Also take the advice you were given on SO aboutdoing the calculations with a calculator. When you get to this sort of thing you are winning

top =x1×x2 +y1×y2 + z1×z2

Campbell Ritchie

Marshal

Posts: 56529

172

Joe Thatcher

Greenhorn

Posts: 15

posted 3 years ago

- 1

Problem has been fixed now with an entire code rewrite.

The help from this forum has been crucial to fixing this issue, so thank you all for spending your time to help me. This is the first forum focused on game development which has been friendly and inviting, so I should hopefully stick around for a while.

Thanks!

They heavily downvote and give no reason, and it and locked it with an invalid reason. IMO, that is unfriendly. The most amusing thing is that when asking on GameDev, they actually said I should go to StackOverflow, even though when I asked there I got downvoted and locked!!

The help from this forum has been crucial to fixing this issue, so thank you all for spending your time to help me. This is the first forum focused on game development which has been friendly and inviting, so I should hopefully stick around for a while.

Thanks!

Campbell Ritchie wrote:I have seen one of the discussions and I did not perceive any unfriendliness myself.Joe Thatcher wrote: . . . P.S. I tried both StackExchange and GameDev for this question. Very unfriendly & unhelpful. . . .

They heavily downvote and give no reason, and it and locked it with an invalid reason. IMO, that is unfriendly. The most amusing thing is that when asking on GameDev, they actually said I should go to StackOverflow, even though when I asked there I got downvoted and locked!!

Consider Paul's rocket mass heater. |