square roots and cube roots?

Posted By: Carlos3DGS

square roots and cube roots? - 10/15/11 11:50

ok so I found a function for the square root "sqrt(x)" usefull for calculating stuff with circles. But what if I want to use the cube root(3)? (for spheres)

So far I'm using these two function for circles:
Code:
var Area(var radius)
{
   return(pi*radius*radius);
}

var Radius(var area)
{
   return(sqrt(area/pi));
}


I cant seem to find a function to do cube roots (3) for spheres. Anyone know of such a function in 3dgs? If not... Anyone know how I could implement it?
Posted By: ventilator

Re: square roots and cube roots? - 10/15/11 12:19

what do you need this for?

i think it could work with some stupid iterative method? laugh but i am no mathematican and probably there is a better way?

edit:
there is cbrt() in the c standard library (which can be used from lite-c). and you can also use pow() with 1/3.0. laugh
Posted By: Carlos3DGS

Re: square roots and cube roots? - 10/15/11 12:38

to calculate the area of a sphere from its radius, and to calculate its radius from its area.
I found a fun indie game called "Osmos" and im trying to make my own 3D version with some ideas I have for it.

http://www.youtube.com/watch?v=pso6UBicLWU

basically what I am working on now is the basic gameplay so whenever I collide with a smaller sphere what I want is my.scale=Radius(My_Area+Your_Area);

or implemented with my functions in the above post...
my.scale=Radius(Area(my.scale)+Area(you.scale));

when I get these basics working ill do some basic velocity vectors for movement&inertia and then I can start to add all these ideas i'm getting based around this simple game's gameplay.
Posted By: Carlos3DGS

Re: square roots and cube roots? - 10/15/11 12:39

what do I have to "#include" to get "cbrt(x)" working?
Posted By: EvilSOB

Re: square roots and cube roots? - 10/15/11 12:41

After a quick dig on the net for c++ solutions, I came up with this lite-c variant

Code:
#define	cuberoot(xxx)	exp(log((float)xxx)/3)



Hopefully you have some KNOWN values to test with, as I was testing using
pretty random numbers and comparing them to results from the windows calculator.

Let us know how it goes...
Posted By: Carlos3DGS

Re: square roots and cube roots? - 10/15/11 12:58

Thanks alot EvilSoB!

I'm not sure what the xxx is thoug... only one number? or num*num*num?

Would it be something like this?:
Code:
var CubeRoot(var num)
{
	return(exp(log(num)/3));
}


Posted By: EvilSOB

Re: square roots and cube roots? - 10/15/11 13:04

You got it correct with your function.
Im just not too sure how accurate it will be using var's
You may need to go floats inside the functions...


XXX is just what I use in single-parameter macros.
Posted By: Slin

Re: square roots and cube roots? - 10/15/11 14:21

The easiest solution is pow(num, 1.0/3.0).
© 2023 lite-C Forums