iphone accelerometer: problem rotating image - iphone

Hi everyone I'm french so scue me for my english. So I have a problem.What I'm doing is when I turn device to left or right with accelerometer an image rotate in the opposite direction of the rotation of the device it create a cool effect but when I rotate the device with accelerometer the image is rotating but it is always trembling, vibrating the movement is not smooth. What can I do ? here is the code:
#import "QuartzCore/QuartzCore.h"
#define CONST_fps 100.
#define CONST_map_shift 0.05
#implementation MapViewRotationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// accelerometer settings
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / CONST_fps)];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static CGFloat ZZ = 0.;
CGFloat z = (atan2(acceleration.x, acceleration.y) + M_PI);
if (fabsf(ZZ - z) > CONST_map_shift)
{
viewToRotate.layer.transform = CATransform3DMakeRotation(ZZ=z, 0., 0., 10.);
}
}
#end

You need to do a trick that is called high pass filter. It filters trembling, only significant changes will pass.
You can google it, or search on StackOverflow. For example, here: How do you implement a Highpass filter for the IPhone accelerometer?

Related

Device motion detection in iOS

I want to detect the device motion like Panorama i.e. motion from my left side to Right side.
I tried with the accelerometer but i can't able to do it.
I like to move one image in a straight line as the device move.
My code is as follow:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
[[UIAccelerometer sharedAccelerometer]setDelegate:self];
}
pragma mark - Accelerometer Delegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
(UIAcceleration *)acceleration{
valueX = acceleration.x*100.0;
valueY = acceleration.y*30.0;
float newX = (float)(ball.center.x + valueX);
CGPoint newCenter = CGPointMake(newX, 50);
ball.center = newCenter;
}
How can I do this? Because there are many apps who uses this.
Please guide me.
Thanks..

AR - Annotation to a fix Location

I made an augmented reality Application, there I have an arrow points on the z-axis to the location (longitude & latitude) of one shop now I'd like to make an annotation in the "sky" which points direct on the shop coordinate. I found some projects on github, like iPhone-AR-Toolkit, but I don't understand this.
Is there a simple way to add such an annotation above the location?
Edit:
After searching a lot I find a pdf document in de web, which contains some code for that what I want, but the problem is, I don't understand it and it doesn't do the things right.
The Code:
// Artificial Horizon - compensate for rotation along x-axis
// need to know the field of view of the camera to find horizon position inside of camera view
// about 53 degrees vertical and 37.5 degrees horizontal
// for directional Horizon - artificial horizon pegged to a specific cardinal direction (North)
// in (void)viewDidAppear
// Load the image to show in the overlay
UIImage *overlayGraphic = [UIImage imageNamed:#"Punkt.png"];
overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(30, 100, 50, 50);
[self addSubview:overlayGraphicView];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
locationManager = [[CoreLocationMangager alloc] init];
[locationManager setDelegate:self];
[locationManager startUpdatingHeading];
- (void) updateUI {
CGPoint overlayCenter = [overlayGraphicView center];
overlayCenter.y = 240.0 - 537.8 * sin(vertAngle);
overlayCenter.x = 160.0 - 497.8 * sin((magCompassHeadingInDeg) * (M_PI / 180.0));
[overlayGraphicView setCenter:overlayCenter];
overlayGraphicView.transform = CGAffineTransformMakeRotation(angle);
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
vertAngle = -atan2(acceleration.y, acceleration.z) - M_PI/2.0;
vertAngleInDeg = vertAngle * 180.0f/M_PI;
angle = -atan2(acceleration.y, acceleration.x) - M_PI/2.0;
angleInDeg = angle * 180.0f / M_PI;
[self updateUI];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
magCompassHeadingInDeg = [newHeading magneticHeading];
[self updateUI];
}
There is an image View is added to a camera overlay, and this image should point to North - but my problem is it also points to South. How can i fix that? And can anybody explain me thr values from the overlayCenter.y and .x - I don't understand them.
You need to Specify an altitude to specify the height from sea-level that is z-axis.
Mixare SDK will allow you to specify and load your POI's with latitude, longitude and altitude.
You can refer it from here
I have used this sdk and it works great and allows you specify Altitude.
Hope this helps:)

box2d accelerometer how to make it work

I'm trying to make the box2d accelerometer work, I have a car sprite and want it to move left and right, when the iPhone is tilted.
Here is the code for the sprite:
- (void)spawnCar {
car = [CCSprite spriteWithSpriteFrameName:#"car.jpg"];
car.position = ccp(160, 250);
car.tag = 2;
[self addBoxBodyForSprite:car];
[_spriteSheet addChild:car];
}
How can implement the accelerometer to work for left and right?
Just do this...
in your init add
self.isAccelerometerEnabled = YES;
and then add this method...
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
b2Vec2 gravity(-acceleration.y * 15, acceleration.x *15);
world->SetGravity(gravity);
}

Accelerometer not working after uploading game to the App Store

I made a space game where you use the accelerometer to move a spaceship, When I try the game on my devices (iPad, iPhone) it work well with no errors or incorrect behavior.
I upload my game to the App Store and it was approved, but when I downloaded the game from the App Store, the accelerometer wasn't working and my spaceship was stuck in one direction.
I tried many devices and they all have same problem.
Note: I used the cocos2d and box2d frameworks.
If anybody have any idea, please help!
Use UIAccelerometerDelegate
UIAccelerometer *accelerometer;
In .m file
# pragma mark To Enable Acceleromete
self.isAccelerometerEnabled = YES;
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = 0.025;
self.accelerometer.delegate = self;
Use these delegates methods
#pragma Mark Accelerometer
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// Set up variables
CGSize winSize = [CCDirector sharedDirector].winSize;
#define kFilteringFactor 0.5
#define kShipMaxPointsPerSec (winSize.height*0.5)
#define kRestAccelX (xCallib)
#define kMaxDiff 0.2
#define kRestAccelY (yCallib)
#define kMaxDiffY 0.1
UIAccelerationValue rollingX=0;
float accelX;
// High pass filter for reducing jitter
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
accelX = acceleration.x - rollingX;
// Calculate movement for x and y axis
float accelDiffX = accelX - kRestAccelX;///
float accelFractionX = accelDiffX / kMaxDiff;//
movementX = kShipMaxPointsPerSec * accelFractionX;
// Thresh holds for x and y axis movement
willMoveX = YES;
if (((movementX < 45.0f) && (movementX > -45.0f)))
willMoveX = NO;
}
#pragma Mark Accelerometer Update Methods
-(void) update:(ccTime)dt
{
CCSprite *player =(CCSprite *) [self getChildByTag:objPlayerShipTag];
CGSize screenSize = [[CCDirector sharedDirector]winSize];
float oldX = [player position].x;
float newX;
if (willMoveX) {
newX = [player position].x + (movementX * dt);
} else newX = oldX;
if ((newX > (screenSize.width -45)) || newX < 45.0f ) {
newX = oldX;
}
[player setPosition:ccp(newX,90)];
// ++++++++ To generate Bullet and Bomb Power +++++++
[self checkForCollisionWithPowersBullets]; //NEW
[self checkForCollisionWithPowersBomb];
}

Cocos2d iPhone: Rotate Sprite using Accelerometer

I'm trying to rotate a sprite using the accelerometer. when I tilt right, I want him to rotate slightly to the right, and when I tilt left, I want him to rotate slightly to the left...
Thanks in advance,
Reed
Firs off - in your h file you need to make the following variables:
UIAccelerationValue accelerationX;
UIAccelerationValue accelerationY;
float currentRawReading;
float calibrationOffset;
Also ensure that your h file has:
#interface myViewName : UIViewController <UIAccelerometerDelegate>
Then in your .m file just below your imports at the top put:
#define kFilteringFactor 0.05
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
Then in your .m file on your viewDidLoad Function put:
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
also add the following function to your .m file:
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
accelerationX = acceleration.x * kFilteringFactor + accelerationX * (1.0 - kFilteringFactor);
accelerationY = acceleration.y * kFilteringFactor + accelerationY * (1.0 - kFilteringFactor);
// keep the raw reading, to use during calibrations
currentRawReading = atan2(accelerationY, accelerationX);
float rotation = -RadiansToDegrees(currentRawReading);
targetView.transform = CGAffineTransformMakeRotation(-(DegreesToRadians(rotation)));
//targetView.transform = CGAffineTransformRotate(targetView.transform, -(rotation * 3)); //if you want easing
}
you will have to tweak it slightly based on what view or object you are targeting -- but thats pretty much it.
Hope this helps,
Michael
Shouldn't be too difficult. Just have somewhere in your code that handles the UIAccelerometerDelegate class and apply changes to your sprites based on the values you receive through parameters to the – accelerometer:didAccelerate: callback.
Apple docs for the delegate class are available at...
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAccelerometerDelegate_Protocol/UIAccelerometerDelegate/UIAccelerometerDelegate.html
In the delegate function of accelerometer just write the code -->>
float angleRadians = atanf((float)X_Position / (float)Y_Position);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = 1 * angleDegrees;
sprite.rotation = cocosAngle;
and the sprite will get rotated to desired angle with changes in the values of X_position, Y_Position and angle.
Njoy.. :)