Okay... I'm rather sorry for posting this, but I've caught myself being way too busy with some other life things and needing a quick answer if at all possible.
What I'm looking for, is the simplest way to find the angle from the horizontal when given a starting coordinate and ending coordinate. I'm having an utter brain block and any nudge in the right direction would help.
EDIT - Okay, I'm going forward using some floats and the arctangent of slope. I'm fine with this, I guess just worried a little about slowness... so any advise is still appreciated.
Odd thing as well... I could use the atan() function from math.h only if I passed a constant number, giving it a variable made the compiler complain that atan was undefined. I found (pure luck) that linking the math lib with -lm solved that.
I'm retarded with Trig Functions...
Re: I'm retarded with Trig Functions...
There's probably a good reason that passing a constant value worked (C++ function parameter overloading?) but linking with the maths library is normally the right approach anyway.
If you're worried about speed then you could investigate the functions in trig_lut.h. You'd either need to use the acos or asin function (or fill in the atan function - see the library source code) but you'll need to deal with fixed point numbers and need to calculate the distance between the two coordinates. That may not be too bad if you use the square root hardware functions (see the arm9 math.h) but I've not used them myself.
Ultimately, the approach is going to be dictated by your application, what you're trying to do and how accurate you need to be.
If you're worried about speed then you could investigate the functions in trig_lut.h. You'd either need to use the acos or asin function (or fill in the atan function - see the library source code) but you'll need to deal with fixed point numbers and need to calculate the distance between the two coordinates. That may not be too bad if you use the square root hardware functions (see the arm9 math.h) but I've not used them myself.
Ultimately, the approach is going to be dictated by your application, what you're trying to do and how accurate you need to be.
Re: I'm retarded with Trig Functions...
Thanks for the reply. What I've got at this point is a sprite that rotates around pointing to the spot the stylus is touching and a sprite that is "fired" in the other sprites current direction. I have everything working rather well, but I'm using floats, degree-radian conversions (float division) and expensive math functions...
I may just keep it as is, as what I've got at this point is little more than a demo and since it's working... I guess it'll do. If I run into any slowness I know where to start, thanks!
I may just keep it as is, as what I've got at this point is little more than a demo and since it's working... I guess it'll do. If I run into any slowness I know where to start, thanks!
-
- Site Admin
- Posts: 1986
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: I'm retarded with Trig Functions...
Sylus101 wrote:
Odd thing as well... I could use the atan() function from math.h only if I passed a constant number, giving it a variable made the compiler complain that atan was undefined. I found (pure luck) that linking the math lib with -lm solved that.
Passing a constant number to atan creates a constant value at compile time which means that the atan function isn't needed at runtime. As you discovered, using a variable requires the function at runtime and so -lm is needed.
Re: I'm retarded with Trig Functions...
Okay, that makes sense.
I'm back at this and it would likely be best if I go ahead and try to make the conversion to fixed point before going any further but I need a little nudge. I'm not familiar at all with the Lerp functions... but what I need is a way to point a sprite to face a specified coordinate. I am currently using floats to:
Dividing the change in y by change in x to get the slope (I have checks to not divide by 0, etc...). Passing that to atan() to get the angle in degrees. Adjusting it depending on quadrant, etc... passing to degreesToAngle() and then into oamRotateScale().
Can someone kind of nudge me in the right direction? Is there an example out there I'm missing?
EDIT... nm I suppose. It didn't take too long after sitting down and really hashing this out to get it figured out.
I'm back at this and it would likely be best if I go ahead and try to make the conversion to fixed point before going any further but I need a little nudge. I'm not familiar at all with the Lerp functions... but what I need is a way to point a sprite to face a specified coordinate. I am currently using floats to:
Dividing the change in y by change in x to get the slope (I have checks to not divide by 0, etc...). Passing that to atan() to get the angle in degrees. Adjusting it depending on quadrant, etc... passing to degreesToAngle() and then into oamRotateScale().
Can someone kind of nudge me in the right direction? Is there an example out there I'm missing?
EDIT... nm I suppose. It didn't take too long after sitting down and really hashing this out to get it figured out.
-
- Posts: 1
- Joined: Fri May 15, 2009 4:43 am
Re: I'm retarded with Trig Functions...
You may have moved on from the original question already, but the application you're talking about sounds like a good case for using trigonometric lookup tables (since rotating a sprite doesn't need to be that precise). LUTs are much faster than calculating using the expensive math functions.
Sorry I don't have much more info about this, but maybe the wikipedia article on generating trig tables (http://en.wikipedia.org/wiki/Generating ... ric_tables) would be a good place to start. Google is your friend as always. www.stackoverflow.com is always a great place for programming questions.
Sorry I don't have much more info about this, but maybe the wikipedia article on generating trig tables (http://en.wikipedia.org/wiki/Generating ... ric_tables) would be a good place to start. Google is your friend as always. www.stackoverflow.com is always a great place for programming questions.
Who is online
Users browsing this forum: No registered users and 0 guests