2

Is there any way to attract two masses to each other as if in free space using UIKit Dynamics?

I know by default the gravity behaviour acts as if the gravity is pointing down to the earth. But this seems only useful for a small class of behaviours. The only way I can think of doing this is by giving the two items huge mass and no initial gravity vector (but applying the UIGravityBehavior to them, errrr adding them to the UIGravityBehavior? hah).

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    

//first item with heavy mass.
self.dynamic = [[UIDynamicItemBehavior alloc] initWithItems:@[self.heavyCircle]];
self.dynamic.density = 1000000000;

self.gravity = [[UIGravityBehavior alloc] initWithItems:@[self.heavyCircle]];
self.gravity.gravityDirection = CGVectorMake(0, 0);

[self.animator addBehavior:self.gravity];
[self.animator addBehavior:self.dynamic];    

//second item with less mass
self.dynamic = [[UIDynamicItemBehavior alloc] initWithItems:@[self.smallCircle]];
self.dynamic.density = 10;
//also tried with a huge value here as well. 
//HUGE_VALF seems to make the app choke



self.gravity = [[UIGravityBehavior alloc] initWithItems:@[self.smallCircle]];
self.gravity.gravityDirection = CGVectorMake(0, 0);

[self.animator addBehavior:self.gravity];
[self.animator addBehavior:self.dynamic];

But this doesn't do anything at all. :/ I was all excited about the physics in UIKit Dynamics and I'm hoping this type of behaviour is possible.

If this is not possible with UIKit Dynamics, is there another way that is equally integrated (retaining interactivity, and functions as a UIView/Button/Whatever)? Sprite Kit?

Thanks for any help!

Arjun Mehta
  • 2,500
  • 1
  • 24
  • 40

1 Answers1

2

The UIDynamicItemBehavior density does not affect the UIGravityBehavior.

The UIGravityBehavior is a constant acceleration vector applied to all items it's initialized with.

To simulate gravity between two items, you need a dynamically changing force that depends on the position of the two interacting objects.

To use UIGravityBehavior, you would need to dynamically change the direction and magnitude at each animation step.

Fortunately, UIDynamicBehavior has an action block which runs on every animation step.

In that action block, you can get the vector between the two items to use as the direction, and use the square of the distance between the items to change the magnitude of the force.

That simulates the force of one object on another. To get the force going in the other direction you need a second gravity behavior.

Unfortunately, adding two gravity behaviors results in this error: Multiple gravity behavior per animator is undefined and may assert in the future

You may be able to simulate gravity using other kinds of dynamic behaviors, though.

divergio
  • 2,000
  • 13
  • 22
  • This actually works for my problem, since I just had one mass to attract to the other and the other could be a fixed point. The UISnapBehavior also does something similar. – Arjun Mehta May 22 '14 at 01:29
  • It's cool that it looks possible to do "real" gravity for the two object case, but this is probably not a very efficient approach. :) – divergio Jun 04 '14 at 05:18
  • @divergio, What about his last question? – Iulian Onofrei Jun 04 '15 at 08:18