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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:26:41+00:00 2026-05-31T12:26:41+00:00

I am creating an app where there will be ~10 circles on screen, that

  • 0

I am creating an app where there will be ~10 circles on screen, that can all be pushed and dragged around the screen via touch movements. These circles (which are simply UIView’s with a circle drawn in them) also have collision detection on them, and the idea is that they push other circles out of the way when they collide. There is also no acceration or gravity in this app, it’s more like pushing coins around, where they are simply pushed out of the way, and when you stop moving, so do they.

I have touch-to-circle interaction working, and the fundamentals of circle-to-circle working. The code however gives Circle01 all the power. IE, it can be pushed into other circles and they will move out of the way as hoped. But when the other circles are pushed into it, they move around Circle01, instead of pushing it out of the way.

I’ve read dozens of pages about collision detection, but I’m just stumped as to how I’d modify this code to give all the circles equal power.

Relevant code:

MyCircle.m

- (void)drawRect:(CGRect)rect
{
 CGPoint viewCenter = CGPointMake(50,50);
 bpath = [UIBezierPath bezierPathWithArcCenter:viewCenter radius:50 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];

 [[UIColor blueColor] setFill];
 [bpath fill];
}

CircleVC.m

- (void)viewDidLoad
{
    Circle01 = [[MyCircle alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
      Circle01.backgroundColor = [UIColor redColor];
      Circle01.alpha = 0.5;
      [self.view addSubview:Circle01];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  // Testing circle collision detection
  for (UIView *aView in self.view.subviews) 
  { 
    if (Circle01 == anotherView) 
    continue; 
    if (CGRectIntersectsRect(Circle01.frame, aView.frame)) 
    { 
      float xDistance = aView.center.x - Circle01.center.x;
      float yDistance = aView.center.y - Circle01.center.y;
      float distance = xDistance * xDistance + yDistance * yDistance;
      float angle = atan2(yDistance, xDistance);
      CGPoint newCenter;

      if (distance < 100 * 100) 
      {
        newCenter.x = Circle01.center.x + (Circle01.frame.size.width * cos(angle));
        newCenter.y = Circle01.center.y + (Circle01.frame.size.width * sin(angle));
        aView.center = newCenter;
      }
    }
  }
}

Thanks for any help.

  • 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-31T12:26:43+00:00Added an answer on May 31, 2026 at 12:26 pm

    It may be easier to offload this to a physics engine such as Box2d. Cocos2d is packaged with Box2d, but you will want the stand alone version since you are only using UIView based animations

    _

    Option 1

    Here is a good, though slightly dated tutorial on using Box2d with UIKit.
    http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/

    Some caveats: if you are using ARC you will need to use the god-awful bridging compiler hints that tell the ARC pre-compiler when your C objects are being put into and out of its control.

    For example:

    bodyDef.userData = (__bridge void *)physicalView;
    UIView *oneView = (__bridge UIView*)b -> GetUserData();
    

    _

    Option 2

    You can use Chipmunk (which would also require the above “god-awful” bridging to ARC statements). Here is a good tutorial: http://www.alexandre-gomes.com/articles/chipmunk/

    _

    Option 3 – Easiest (Not Cheapest)

    Alternatively, you could use the Objective-Chipmunk library that comes with Chipmunk Pro which does work with ARC. Here is a recent and “to-the-point” tutorial on using Obj-Chipmunk with UIImageViews. This would be the same for any UIView based code. http://www.ayarsanimation.com/frankayars/chipmunk

    You can buy Obj-Chipmunk costs $89 when you by an Indie version license for Chipmunk Pro.
    http://chipmunk-physics.net/chipmunkPro.php

    I am not advocating a paid solution, but it may save you time.

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

Sidebar

Related Questions

I'm creating and app that will rely on a database, and I have all
I'm creating an app with a number of videos/large images that will use appcache
I'm creating a mobile website that will include a page from which people can
I'm creating an app that will import products from several XML feeds. In the
I'm creating an app (C#) that is going to send some messages around the
In the app im creating there are many pages that look mostly the same
I am creating an app that will require authentication which I have already gotten
I'm creating an SL4 app that needs to query certain data via a webservice
I am creating an iphone app that will run an SQLite database. I want
The app I am creating has lots of pictures that the user will be

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.