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 4 of 4

Thread: need help with using trig functions

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with using trig functions

    First off I'm new here so I'd like to say nice to meet you all . Now down to business, I'm working on a (2d, birds i view) game but I'm having trouble with interacting with the avatar. I would like to make it so the avatar faces the mouse cursor (so it rotates so the front always is facing the mouse). This is what I have but the code just has the avatar spinning constantly. (Note: I changed some variable names and some of the syntax to make it easier to understand my code without knowledge of the game engine I'm using so any var names or brackets/parenthesis problems aren't an issue.) Thanks in advance for the any help!

    if (mousesX >= avatarsX)
            {
                if(mousesY >= avatarsY)
                    angle = (int)(Math.atan((mousesY - avatarsY) / (mousesX -avatarsX)));
                else
                    angle = (int)(Math.atan((avatarsY - mousesY) / (mousesX - avatarsX)));
            }
            else
            {
                if(mousesY >= avatarsY)
                    angle = (int)(Math.atan(mousesY -avatarsY)/(avatarsX - mousesX));
                else
                    angle = (int)(Math.atan((avatarsY - mousesY) / (avatarsX - mousesX));
     
            }
     
             if(angle != oldAngle)
            {(
                setImage(rotate(getImage() , angle + oldAngle + (180 - (angle + oldAngle)))); //rotate(image, int) rotates image (in this case the avitar)  by int degrees clockwise. setImage(image) displays image (the avatar) and getImage() returns the avatars image
     
                oldAngle += angle;
            }
     
            if (oldAngle == 360)
                oldAngle -= 360;


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with using trig functions

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with using trig functions

    Sorry, I'm not quite sure what you mean. Are you asking to upload an example program that reproduces the problem?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help with using trig functions

    Yes, something that could be used for testing the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. Help me understand when to use static on functions/methods
    By thenk24 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 1st, 2011, 11:42 AM
  3. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM
  4. Calling Existing DLL functions using java...
    By orihsoyk in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 07:23 AM
  5. callback functions? non-blocking socket help
    By Tomas in forum Java Networking
    Replies: 0
    Last Post: September 12th, 2009, 06:46 PM