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

  • SEARCH
  • Home
  • 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 8055839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:31:12+00:00 2026-06-05T08:31:12+00:00

This seems like a simple trig question, but for whatever reason, things aren’t working

  • 0

This seems like a simple trig question, but for whatever reason, things aren’t working out.

I’m trying to simply have an object rotate around a given point when the user presses the A/D keys (strafing around the mouse in a circular motion, while still facing the mouse).

Here’s the code I’ve tried so far (all Math functions take and return radians):

if (_inputRef.isKeyDown(GameData.KEY_LEFT))
{
    x += 2 * Math.cos(Math.atan2(mouseY - y, mouseX - x) - Math.PI * 0.5);
    y += 2 * Math.sin(Math.atan2(mouseY - y, mouseX - x) - Math.PI * 0.5);
}
else if (_inputRef.isKeyDown(GameData.KEY_RIGHT))
{
    x += 2 * Math.cos(Math.atan2(mouseY - y, mouseX - x) + Math.PI * 0.5);
    y += 2 * Math.sin(Math.atan2(mouseY - y, mouseX - x) + Math.PI * 0.5);
}

And a more elegant method which accomplishes the same thing:

if (_inputRef.isKeyDown(GameData.KEY_LEFT))
{
    x += 2 * Math.sin(Math.atan2(mouseY - y, mouseX - x));
    y -= 2 * Math.cos(Math.atan2(mouseY - y, mouseX - x));
}
else if (_inputRef.isKeyDown(GameData.KEY_RIGHT))
{
    x -= 2 * Math.sin(Math.atan2(mouseY - y, mouseX - x));
    y += 2 * Math.cos(Math.atan2(mouseY - y, mouseX - x));
}

Now, they both kind of work, the object rotates around the mouse while always facing the mouse, but given enough time of holding down the strafe button, it becomes increasingly apparent that the object is also rotating AWAY from the mouse, as if its being pushed away.

I have no idea why this is and how to fix it.

Any insight is appreciated!

  • 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-06-05T08:31:14+00:00Added an answer on June 5, 2026 at 8:31 am

    I think your current approach would only work if you take ‘infinitely small’ steps. As it is now, each movement is perpendicular to the “to-mouse vector” and thus increases the distance between mouse and object.

    A solution would be to calculate the new position while keeping the distance to the mouse unchanged, by rotating the position around the mouse:

    // position relative to mouse
    var position:Point = new Point(
        x - mouseX,
        y - mouseY);
    
    var r:Number = position.length; // distance to mouse
    
    // get rotation angle around mouse that moves
    // us "SPEED" unit in world space
    var a:Number = 0;
    if (/* LEFT  PRESSED */) a = getRotationAngle( SPEED, r);
    if (/* RIGHT PRESSED */) a = getRotationAngle(-SPEED, r);
    if (a > 0) {
    
        // rotate position around mouse
        var rotation:Matrix = new Matrix();
        rotation.rotate(a);
        position = rotation.transformPoint(position);
        position.offset(mouseX, mouseY);
    
        x = position.x;
        y = position.y;
    }
    
    // elsewhere...
    
    // speed is the distance to cover in world space, in a straight line.
    // radius is the distance from the unit to the mouse, when rotating.
    private static function getRotationAngle(speed:Number, radius:Number):Number {
        return 2 * Math.asin(speed / (2 * radius));
    }
    

    The above uses a Matrix to rotate the (x, y) position around the mouse position. Ofcourse you can apply the same principle without using Matrix if so desired.

    I had to do some trig to come up with the right equation for getting the correct angle. The angle depends on the radius of the movement arc, since a larger radius but constant angle would increase the movement distance (undesired behavior). My earlier solution (before edits) was to scale the angle by the radius, but that would still result in slightly more movement with larger radii.

    The current approach ensures that radius and speed remain constant in all cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a simple question, but I am unable to figure it out.
This seems like a simple question, but it is difficult to search for. I
this seems like a simple enough question but I can't seem to find a
This seems like a simple question, but has proven to be difficult to find
This seems like a simple question, but I can't find it documented anywhere. I'd
This seems like a simple question but I forget. When you have a low
This seems like a simple request. But my query in not working and I'm
This seems like a simple question...I am trying to allow users to 'load' a
This seems like a simple question, but I haven't been able to find the
This seems like a really simple question but I can't seem to find information

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.