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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: cos & sin 2d movement

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation 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 )))
    {
    changePos(addToX, addToY);
    }
    }

    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.
    Last edited by Johnharrisanglia; April 9th, 2013 at 09:19 AM. Reason: made mistake


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: cos & sin 2d movement

    Have you read the API doc for those methods to see how they work?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    toRadians

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: cos & sin 2d movement

    Math.sin(270)
    This suggests that you have not read the API doc. Is the 270 degrees or radians?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default 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)));
    If you don't understand my answer, don't ignore it, ask a question.

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

    Johnharrisanglia (April 10th, 2013)

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to control gain between mouse movement and cursor movement ?
    By DrPete in forum Java Theory & Questions
    Replies: 3
    Last Post: March 12th, 2012, 07:27 AM

Tags for this Thread