Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How would you be able to change Math.sin, Math.cos and Math.tan into an int? This is because I heard that it is useful to have those Math resources in movement, gravity, etc. in gaming. Also, as a little inside question, does anyone know if there is a method where you can square or cube a number instead of doing: (Pseudo Example) 9 * 9 * 9 ?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You can convert any float or double to an int with a cast. For example:

int integerResult = (int)Math.sin(variable);

However, this really does not make sense. Since the result of Math.sin() will always be from -1 to 1, this cast will always make the result 0 or -1 or 1. From the sound of your question you're not very sure about how to use these. I would strongly recommend you brush up on your math skills before attempting anything too complicated with your programming. You can, of course, use these in floating point format instead of as an integer.

Your second question. 9 * 9 * 9 is equivalent to 9 cubed. You could make your own method for the clearest intent:

int cube(int input) {
    //return the input cubed
    return input*input*input;
}

Or use the built in function Math.pow() like so:

float nineCubed = Math.pow(9,3);

You can see check the java doc to see what functions Java has built in for math.

Finally, you can check out some of these math resources:

What math should all game programmers know?

Linear algebra for game developers part 1

share|improve this answer
    
The result of Math.sin() will be from -1 to 1. I would recommend looking at Math.pow(), which handles squaring, cubing and so forth. For example Math.pow(9,3) = 9*9*9. –  utdiscant Jun 17 '12 at 13:49
    
Thanks @utdiscant, I was still editing my answer :) –  Byte56 Jun 17 '12 at 13:53
    
Thank you so much for your help! –  Taylor Golden Jun 17 '12 at 15:35
    
@TaylorGolden No problem. If this answer was helpful to you, you can accept it as your answer. And welcome to GDSE! –  Byte56 Jun 17 '12 at 15:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.