gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Cos, Sin, Tan, aCos, aSin, aTan

#130443 - Dan2552 - Sun Jun 03, 2007 1:50 pm

I'm not the most experienced person in this at all, so I need help.

I'm using PAlib, which has Cos and Sin functions, but not Tan and not any of the inverse functions.

After searching I've read theres a bios call specifically for aTan, how do I go about calling things from the bios - is it simple, or is it something I shouldn't touch (seeming as I am a noob and using PAlib)?

How would I go about using a math libary?

#130444 - tepples - Sun Jun 03, 2007 2:31 pm

tan(theta) = sin(theta)/cos(theta)

Do you know what a lookup table is? Do you know what linear interpolation is? Read those articles a couple times, come back with your questions, and I will teach you how to write trig subroutines that are both fast and accurate.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#130447 - Darkflame - Sun Jun 03, 2007 4:09 pm

hmz..best not overkill here.

Dan needs this to work out a distance a certain amount away from one point in the direction between the point and the stylus press.

I showed him how to do it with Sin / Cos / Tan ect.

But I wasnt aware that there wasnt a coding for Tan. (or the ds needed a different lookup table based stuff).
_________________
Darkflames Reviews --
Make your own at;
Rateoholic:Reviews for anything, by anyone.

#130448 - Dan2552 - Sun Jun 03, 2007 4:18 pm

What I posted on the PAlib forums:

Quote:
I want to display a sprite proportional to where the stylus is pressed; at a certain distance from another sprite.

for example:
[Images not permitted - Click here to view it]
(RED= where the Stylus is pressed)
(BLACK= the sprite comparing distance - the circle symbolises the distance)
(BLUE= where I want the new sprite to show)

The stylus should be able to be pressed anywhere, including inside the circle and the (BLUE)sprite should always be on the circle.

Help?


(not to scale, the black sprite is the center of the circle)

#130457 - DekuTree64 - Sun Jun 03, 2007 7:51 pm

Vectors work nicely for this sort of thing:
Code:
Vector2 delta = touchPos - spritePos;
Fixed length = sqrt(delta.x * delta.x + delta.y * delta.y);
delta /= length;   // delta is now a unit vector

Vector2 newSpritePos = spritePos + delta * desiredDistance;

Actually, this particular problem could be done in pure integer math, if you don't feel like writing fixed-point/vector classes:
Code:
int deltaX  touchX - spriteX;
int deltaY = touchY - spriteY;
int length = sqrt(deltaX * deltaX + deltaY * deltaY);
deltaX *= desiredDistance;
deltaX /= length;
deltaY *= desiredDistance;
deltaY /= length;

int newSpriteX = spriteX + deltaX;
int newSpriteY = spriteY + deltaY;

Basically the only difference is that the multiply by desired distance is done first, so the divide by length doesn't kill the accuracy.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku