cos & sin 2d movement

• April 9th, 2013, 09:18 AM
Johnharrisanglia
cos & sin 2d movement
This is a bit of a maths question as much as java i think.
i have been stuck for about 3 weeks trying to make a silly racing car game. everything was going well apart from controlling the little things:

public void move()
{
//place checks in here as well and move from center of object

double radi = Math.toRadians(angle);// left over from one of many failed experiments

double x= Math.cos(angle);
double y= Math.sin(angle);

double fullspeed = speed * 1.5; //makes positive number change name

int centerX = x + 25;
int centerY = y + 25;

int addToX = (int)Math.round(fullspeed * x);

//this need to account for center
{
}
}

private void changePos(int xMove, int yMove)
{
x = x + xMove;

y = y + yMove;
}

my cars are going all over the place. they start ok then start reversing and all sorts very un predictable.
when i do cos(270) on calculater i get 0 and sin(270) i get -1; cos(90) = 0 and sin(90) = 1. this is the pattern i want and expected.

but i am getting from the code:

Math.sin(270) = -0.1760459464712114
Math.cos(270) = 0.9843819506325049

on my calculater there is just "cos" & "sin" + "cosh" & "sinh" is this why and how do i get the results i need.
• April 9th, 2013, 09:35 AM
Norm
Re: cos & sin 2d movement
Have you read the API doc for those methods to see how they work?
• April 10th, 2013, 01:28 PM
Johnharrisanglia
Re: cos & sin 2d movement
lol thats a helpful answer? course i have, but i will check again now

--- Update ---

what bit other then this do i need to read?

Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.
Parameters:
angdeg - an angle, in degrees
Returns:
the measurement of the angle angdeg in radians.
Since:
1.2

cos

public static double cos(double a)
Returns the trigonometric cosine of an angle. Special cases:
If the argument is NaN or an infinity, then the result is NaN.
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters:
a - an angle, in radians.
Returns:
the cosine of the argument.
• April 10th, 2013, 01:30 PM
Norm
Re: cos & sin 2d movement
Quote:

Math.sin(270)
This suggests that you have not read the API doc. Is the 270 degrees or radians?
• April 10th, 2013, 03:45 PM
Johnharrisanglia
Re: cos & sin 2d movement
it was degrees. if you look i had a line just above with a variable called radi that i was substituting for angle n other failed attempts.

any way it now works but im not sure why instead of zero values of cos/sin(radian) * speed for the axis not in use it is sin/cos(-2. loads of numbers) * speed(at least 5) = 0; which works fine. I dont know why but ive had a good old drive round of my space ships and there fine

Thank you for your time n e way

John Harris
• April 10th, 2013, 03:54 PM
Norm
Re: cos & sin 2d movement
When I called toRadians() with degrees and used its value for the cos() and sin() methods, the results were as expected:
Code :

` System.out.println("cos(90)="+Math.cos(Math.toRadians(90)));`
• April 10th, 2013, 04:10 PM
Johnharrisanglia
Re: cos & sin 2d movement
im trying to do full 360 though maybe my radian knowledge needs revision after 90 what happens in your test? like 180 degrees 270 degree 360/0 degree? cos and sin on each? just to check (and i am sure u know what i mean) but just to check i wanted one axis to say either 1/-1 and the other 0 at these 0/360 90 180 270.

like i say its doing what i ask it but i dont under stand why, please dont waste any more of your time on it though. does that answer return zero? i wonder about the rest but I'm not putting any more time in to it, so please dont worry n e more il come back to it again at some point
• April 10th, 2013, 04:23 PM
Norm
Re: cos & sin 2d movement
To see what the values are, write a loop that increments the degrees by 30 and goes from 0 to 360
and prints out three columns of degrees, sin and cos.