Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: cos & sin 2d movement

1. ## 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);
int addToY = (int)Math.round(y* fullspeed);

//this need to account for center
if(((x + addToX >= 50) && (x + addToX <=750)) && ((y + addToY >= 100) && (y + addToY <=550 )))
{
}
}

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.  Reply With Quote

3. ## Re: cos & sin 2d movement

Have you read the API doc for those methods to see how they work?  Reply With Quote

4. ## 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?

public static double toRadians(double angdeg)
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.  Reply With Quote

5. ## Re: cos & sin 2d movement

Math.sin(270)
This suggests that you have not read the API doc. Is the 270 degrees or radians?  Reply With Quote

6. ## 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  Reply With Quote

7. ## 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:
` System.out.println("cos(90)="+Math.cos(Math.toRadians(90)));`  Reply With Quote

8. ## The Following User Says Thank You to Norm For This Useful Post:

Johnharrisanglia (April 10th, 2013)

9. ## 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  Reply With Quote

10. ## 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.  Reply With Quote

#### Tags for this Thread 