Skip to content

Commit

Permalink
Added real gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
fferegrino committed Dec 25, 2016
1 parent 97db903 commit a85de82
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions DropIt/DropItView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UIKit;
using CoreGraphics;
using System.Collections.Generic;
using CoreMotion;

namespace DropIt
{
Expand Down Expand Up @@ -51,6 +52,52 @@ public bool Animating
}
}

private bool _realGravity;
public bool RealGravity
{
get { return _realGravity; }
set { _realGravity = value;
UpdateRealGravity();
}
}

CMMotionManager motionManager = new CMMotionManager();
void UpdateRealGravity()
{
if (_realGravity)
{
if (motionManager.AccelerometerAvailable && !motionManager.AccelerometerActive)
{
motionManager.AccelerometerUpdateInterval = 0.25;
motionManager.StartAccelerometerUpdates(NSOperationQueue.MainQueue, (data, error) =>
{
if (dropBehavior.DynamicAnimator != null)
{
var dx = data?.Acceleration.X;
var dy = data?.Acceleration.Y;
if (dx != null && dy != null)
{
switch (UIDevice.CurrentDevice.Orientation)
{
case UIDeviceOrientation.Portrait: dy = -dy; break;
case UIDeviceOrientation.PortraitUpsideDown: break;
case UIDeviceOrientation.LandscapeLeft: // Swap break;
case UIDeviceOrientation.LandscapeRight: // Swap - dy = -dy; break;
default:
break;
}
dropBehavior.gravity.GravityDirection = new CGVector((System.nfloat)dx, (System.nfloat)dy);
}
}
});
}
}
else
{
motionManager.StopAccelerometerUpdates();
}
}

UIAttachmentBehavior _attachment;
UIAttachmentBehavior Attachment
{
Expand Down
2 changes: 2 additions & 0 deletions DropIt/DropItViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ DropIt.DropItView gameView
new UIPanGestureRecognizer(gameView.GrabDrop);
gameView.AddGestureRecognizer(pgr);

_gameView.RealGravity = true;

}
}

Expand Down
2 changes: 1 addition & 1 deletion DropIt/FallingObjectBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DropIt
public class FallingObjectBehavior : UIDynamicBehavior
{

UIGravityBehavior gravity = new UIGravityBehavior();
public UIGravityBehavior gravity = new UIGravityBehavior();
UICollisionBehavior _collider;
UICollisionBehavior collider => _collider ?? (_collider = new UICollisionBehavior
{ TranslatesReferenceBoundsIntoBoundary = true });
Expand Down

0 comments on commit a85de82

Please sign in to comment.