?
Solved

How to find the angle between two vectors without using acos() (J2ME)

Posted on 2005-03-22
9
Medium Priority
?
23,128 Views
Last Modified: 2013-12-26
I'm doing some J2ME programming using JSR 184. One of the things lacking from the CLDC 1.1 spec is any of the inverse trig functions. Does anyone have any suggestions for finding the angle between two vectors not using the inverse trig functions?
0
Comment
Question by:DanMacDonald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 13619334
a dot b / sqrt(a dot a * b dot b)
will give you the cosine of the angle between a and b
I don't think you can do much more without an inverse trig function of some form.
(otherwise whatever method you used could be used to implement an inverse trig function)
What do you want to do with the angle?  And why don't you want to use an inverse trig function to do it?
0
 

Author Comment

by:DanMacDonald
ID: 13619376
I want to find the angle between two vectors so that I can rotate a billboard about the Y axis to face the camera.  So I  find the angle between the camera vector and a vector lieing on horizontal X axis (rotation about the Y axis = 0) and from that I can figure out what angle of rotation to apply to my billboard. I ended up doing a acos( ) lookup table, generating it in VC. It worked quite nicely.

The reason I dont use an inverse trig function is that J2ME does not have inverse trig functions. :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 13619412
I wonder how J2ME can do camera rotations without trig functions.
A lookup table certainly works, otherwise, I supose you could take an approximate function, rotate it, then check dot product again to refine the approximation until you were close enough.
Or can you just directly take the transform from vector a to vector b and apply that transform to the rest of the billboard?
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 84

Expert Comment

by:ozo
ID: 13628373
A C grade is usually given only after expressing your reasons for your dissatisfaction with the answer given, and after the experts have been given ample time to address the issues you raise in your complaint.
http:/help.jsp#hi73
You didn't even give me enough time to see your response to my questions to try to clarify your need.

Since you're trying to rotate by an angle, and a 2-D rotation matrix looks like
 cos(r)  sin(r)
-sin(r)  cos(r)
if you know cos(r), you need not find r in order to generate the rotation matrix
you need only know that sin(r)² + cos(r)² = 1
0
 

Author Comment

by:DanMacDonald
ID: 13628654
I work with a number of different 3D API's for J2ME. Not all the devices are CLDC 1.1  and so most of my calculations are in FixedPoint math. At first I implemented billboarding the traditional way by creating a transformation matrix that oriented the billboard verticies to face the camera. However due to the number of intermediate operations involved in creating a new coordinate system and using those vectors to build a transformation matrix a lot of little precision errors along the way amounted to unacceptable visual artifacts.

Since the game I am working on is basically an FPS where the players camera is always a fixed elevation. I found it was simpler to just take the 2d Rotation formulas and rotate the points of the billboard in object space about it's Y axis. With far fewer operations it was much easier to keep track of precision and it produced much fewer artifacts.  To get the angle of rotation required, I would find the vector that was 0 degrees rotation about the billboards location, and then find the vector between the billboard location and the camera. Finding the angle between these two vectors would tell me how much to rotate the billboard to ensure it always faced the camera.
The graphics library I had been using previously implemented an acos( ) function. However when switching to JSR 184 I lost the acos function and needed a way to port my code. It turns out that JRS 184 has a "setOrientation ( )" function for mesh's that takes an angle of rotation. So I don't have to manually rotate the billboard verticies myself.  I'm familiar with the rotation equations, and the definition of the dot product, the answer I wanted was exactly what I asked.

The answer you supplied was exactly what I knew, how to get the cosine of the angle between two vectors, what I needed to know was how to convert that into an angle that I could use.  Your last comment is much closer to a solution

sin(r) = sqrt(1-cos(r)2) would have helped me build the 2d rotations I needed. Which is a good idea, but having the angle of rotation and supplying it to setOrientation works even better.

0
 
LVL 84

Expert Comment

by:ozo
ID: 13628993
What you asked for
finding the angle between two vectors not using the inverse trig functions
is impossible since any method for finding the angle between two vectors would be an inverse trig function.
It is not the fault of the answerer when the answer to a question is impossible.
If what you really wanted were ways to compute an inverse cosine function
then you could have gotten answers like using a lookup table, or
arccos(y) = arctan(sqrt(1-y²)/y)
and arctan(y) is approximately y/(1+.28y²) for -1<=y<=1
and arctan(y) = pi/2 - arctan(1/y) for y < -1 or y > 1
or arccos(y) is approximately sqrt(1-y)*(1.5707288-0.212114*y+0.0742610*y²-0.0187293*y³)
or sometimes languages have an atan2 function where arctan(y/x) = atan2(y,x)
but I had no idea whether you already knew any of that or whether any of that would address your real needs.
0
 
LVL 84

Expert Comment

by:ozo
ID: 13629039
You can also get the sin of the angle between two vectors, with appropriate sign, from the cross product, if you want to use it in a rotation matrix or in an arctangent function.
0
 

Author Comment

by:DanMacDonald
ID: 13632445
Subsequently you've done a great job of answering the question. I would change the grade if I could... I know the moderators dislike questions being left open. Since I had found a workable solution I wanted to close the question and move on as I would not be needing any subsequent answeres. I didn't realize that an average grade was such a large issue, I was under the impression that points where the primary metric.
0
 

Expert Comment

by:boulderco
ID: 14580864
There is a Maclaurin series expansion for the arc cos that will give you the answer to any desired degree of precision.  See

http://mathworld.wolfram.com/InverseCosine.html
  and
http://mathworld.wolfram.com/MaclaurinSeries.html

It's trivial to implement this as a function you can call.

(I didn't post the equation directly because it's a bit ugly without typesetting. )

Hope this helps.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question