I'm retarded with Trig Functions...

Post Reply
Sylus101
Posts: 179
Joined: Wed Dec 24, 2008 5:08 am

I'm retarded with Trig Functions...

Post by Sylus101 » Thu Apr 30, 2009 5:00 am

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.
-Sylus "Not Stylus..." McFrederickson

Come visit my web site.

Al_Bro
Posts: 15
Joined: Wed Apr 08, 2009 10:33 pm

Re: I'm retarded with Trig Functions...

Post by Al_Bro » Thu Apr 30, 2009 10:10 pm

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.

Sylus101
Posts: 179
Joined: Wed Dec 24, 2008 5:08 am

Re: I'm retarded with Trig Functions...

Post by Sylus101 » Fri May 01, 2009 3:28 am

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!
-Sylus "Not Stylus..." McFrederickson

Come visit my web site.

WinterMute
Site Admin
Posts: 1989
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: I'm retarded with Trig Functions...

Post by WinterMute » Fri May 01, 2009 7:28 pm

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Sylus101
Posts: 179
Joined: Wed Dec 24, 2008 5:08 am

Re: I'm retarded with Trig Functions...

Post by Sylus101 » Mon May 04, 2009 1:52 am

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.
-Sylus "Not Stylus..." McFrederickson

Come visit my web site.

talklittle
Posts: 1
Joined: Fri May 15, 2009 4:43 am

Re: I'm retarded with Trig Functions...

Post by talklittle » Fri May 15, 2009 4:54 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests