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 6122427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:53:52+00:00 2026-05-23T15:53:52+00:00

I am attempting to simply make objects orbit around a center point, e.g. The

  • 0

I am attempting to simply make objects orbit around a center point, e.g.

Orbiting Objects

The green and blue objects represent objects which should keep their distance to the center point, while rotating, based on an angle which I pass into method.

I have attempted to create a function, in objective-c, but it doesn’t work right without a static number. e.g. (It rotates around the center, but not from the true starting point or distance from the object.)

-(void) rotateGear: (UIImageView*) view heading:(int)heading
{
    // int distanceX = 160 - view.frame.origin.x;
    // int distanceY = 240 - view.frame.origin.y;

    float x = 160 - view.image.size.width / 2 + (50 * cos(heading * (M_PI / 180)));
    float y = 240 - view.image.size.height / 2 + (50 * sin(heading * (M_PI / 180)));
    view.frame = CGRectMake(x, y, view.image.size.width, view.image.size.height);
}

My magic numbers 160, and 240 are the center of the canvas in which I’m drawing the images onto. 50 is a static number (and the problem), which allows the function to work partially correctly — without maintaining the starting poisition of the object or correct distance. I don’t know what to put here unfortunately.

heading is a parameter that passes in a degree, from 0 to 359. It is calculated by a timer and increments outside of this class.

Essentially what I would like to be able to drop any image onto my canvas, and based on the starting point of the image, it would rotate around the center of my circle. This means, if I were to drop an image at Point (10,10), the distance to the center of the circle would persist, using (10,10) as a starting point. The object would rotate 360 degrees around the center, and reach it’s original starting point.

The expected result would be to pass for instance (10,10) into the method, based off of zero degrees, and get back out, (15,25) (not real) at 5 degrees.

I know this is very simple (and this problem description is entirely overkill), but I’m going cross eyed trying to figure out where I’m hosing things up. I don’t care about what language examples you use, if any. I’ll be able to decipher your meanings.

Failure Update

I’ve gotten farther, but I still cannot get the right calculation. My new code looks like the following:

heading is set to 1 degree.

-(void) rotateGear: (UIImageView*) view heading:(int)heading
{
    float y1 = view.frame.origin.y + (view.frame.size.height/2); // 152
    float x1 = view.frame.origin.x + (view.frame.size.width/2); // 140.5

    float radius = sqrtf(powf(160 - x1 ,2.0f) + powf(240 - y1, 2.0f)); // 90.13

    // I know that I need to calculate 90.13 pixels from my center, at 1 degree.      
    float x = 160 + radius * (cos(heading * (M_PI / 180.0f))); // 250.12
    float y = 240 + radius * (sin(heading * (M_PI / 180.0f))); // 241.57

    // The numbers are very skewed.
    view.frame = CGRectMake(x, y, view.image.size.width, view.image.size.height);
}

I’m getting results that are no where close to where the point should be. The problem is with the assignment of x and y. Where am I going wrong?

  • 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-23T15:53:53+00:00Added an answer on May 23, 2026 at 3:53 pm

    You can find the distance of the point from the centre pretty easily:

    radius = sqrt((160 – x)^2 + (240 – y)^2)

    where (x, y) is the initial position of the centre of your object. Then just replace 50 by the radius.

    http://en.wikipedia.org/wiki/Pythagorean_theorem


    You can then figure out the initial angle using trigonometry (tan = opposite / adjacent, so draw a right-angled triangle using the centre mass and the centre of your orbiting object to visualize this):

    angle = arctan((y – 240) / (x – 160))

    if x > 160, or:

    angle = arctan((y – 240) / (x – 160)) + 180

    if x < 160

    http://en.wikipedia.org/wiki/Inverse_trigonometric_functions


    Edit: bear in mind I don’t actually know any Objective-C but this is basically what I think you should do (you should be able to translate this to correct Obj-C pretty easily, this is just for demonstration):

    // Your object gets created here somewhere
    
    float x1 = view.frame.origin.x + (view.frame.size.width/2); // 140.5
    float y1 = view.frame.origin.y + (view.frame.size.height/2); // 152
    
    float radius = sqrtf(powf(160 - x1 ,2.0f) + powf(240 - y1, 2.0f)); // 90.13
    
    // Calculate the initial angle here, as per the first part of my answer
    float initialAngle = atan((y1 - 240) / (x1 - 160)) * 180.0f / M_PI;
    if(x1 < 160)
        initialAngle += 180;
    
    // Calculate the adjustment we need to add to heading
    int adjustment = (int)(initialAngle - heading);
    

    So we only execute the code above once (when the object gets created). We need to remember radius and adjustment for later. Then we alter rotateGear to take an angle and a radius as inputs instead of heading (this is much more flexible anyway):

    -(void) rotateGear: (UIImageView*) view radius:(float)radius angle:(int)angle
    {
        float x = 160 + radius * (cos(angle * (M_PI / 180.0f)));
        float y = 240 + radius * (sin(angle * (M_PI / 180.0f)));
    
        // The numbers are very skewed.
        view.frame = CGRectMake(x, y, view.image.size.width, view.image.size.height);
    }
    

    And each time we want to update the position we make a call like this:

    [objectName rotateGear radius:radius angle:(adjustment + heading)];
    

    Btw, once you manage to get this working, I’d strongly recommend converting all your angles so you’re using radians all the way through, it makes it much neater/easier to follow!

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

Sidebar

Related Questions

I'm attempting to do something which should be relatively simple. I'm using a ListView
Using php coding, I'm attempting to make a script which will grab content from
I'm attempting to make a very simple 2-person chatroom for my Django site. I'm
Greetings! I am attempting to solve a few performance issues which are cropping up
I am attempting to make a registration-like page for a small forum. To register
I am attempting to make a game using java, and I need a plugin
I'm attempting to make a TinyURL clone in ASP.NET MVC as a learning project.
I am attempting to make a Facebook application with node.js, however I'm having trouble
I'm attempting to make a simple game of Pairs for Android. Program Structure Menu.java
I am attempting to make a dynamically typed extension HtmlHelper, but I am getting

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.