Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:08:45+00:00 2026-05-24T20:08:45+00:00

This is my first post, so please bear with me. I’ll try and be

  • 0

This is my first post, so please bear with me. I’ll try and be as clear as I can here about my problem.
First off, let me say that I suck at math. I failed it in school, and it pains me to no end that I can’t grasp simple math concepts, especially since I love to code. Someone who wants to program, and can’t perform basic math operations? Bad combo.
Aaaanyway, on to the problem.

Some context. I am writing an asteroids-type game. It’s coming along quite nicely, and I’ve overcome all my hurdles so far thanks to this site (thank you!), and google. I’ve searched high and low to a solution to my problem, but it always seems like I run into a solution that either doesn’t work, or I just don’t understand and can’t incorporate it into my code.

The issue involves rotation of the ship. I have an onscreen joystick class that returns the angle that the joystick is being pushed. I use that angle to point the ship in that same direction.

What I want to do is gradually turn the ship towards the angle the user wants to go, using the shortest turn, left or right. In my mind I’m thinking “How the hell do I go to say.. 350 degrees from 5 degrees, going left?”. I don’t know…

Here is my draw code:

public void draw(Canvas canvas){
    canvas.save();
    canvas.rotate((float) (fAngle + 90), (float) (dX + (mShip.getIntrinsicWidth() / 2)), (float) (dY + (mShip.getIntrinsicHeight() / 2)));
    mShip.setBounds((int)dX, (int)dY, (int)dX + mShip.getIntrinsicWidth(), (int)dY + mShip.getIntrinsicHeight());
    mShip.draw(canvas);
    canvas.restore();
}

The angle is passed to the fAngle variable from the joystick getangle method. The angle is then increased by 90 degrees because of the image facing.

user.fAngle = oJoystick.getAngle();

So on each game tick, I want to turn the ship towards whatever direction the player wants to go, degree by degree. Any help with this would be greatly appreciated!

Thanks for reading!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:08:46+00:00Added an answer on May 24, 2026 at 8:08 pm

    In your tick function, instead of:
    user.fAngle = oJoystick.getAngle();

    Use this (adjust increment to your liking – this controls the rotation speed):

    const float increment = 1.0;
    float direction;
    float joy = oJoystick.getAngle();
    float ang = user.fAngle;
    float fudge = 5.0;
    
    if (abs (joy - ang) > fudge) {
        if (joy > ang) {
            if (joy - ang < 180)
                direction = 1;
            else
                direction = -1;
        } else if (joy < ang) {       
            if (ang - joy < 180)
                direction = -1;
            else
                direction = 1;
        }
    } else // already pointing right direction
        direction = 0;
    
    user.fAngle = ang + direction * increment;
    if (0 > user.fAngle)
        user.fAngle += 360;
    if (360 < user.fAngle)
        user.fAngle -= 360;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.