How to use Devicemotion/Gyroscope to move a dot from center to 8 directions of iphone screen
Posted
by
iSeeker
on Stack Overflow
See other posts from Stack Overflow
or by iSeeker
Published on 2013-10-28T09:33:59Z
Indexed on
2013/10/29
3:54 UTC
Read the original article
Hit count: 149
I am trying to move a dot at center to 8 directions of iphone screen using Devicemotion such that it moves faster when tilted at a faster rate. Rough sketch is below:
I could find a similar implementation in an app called Gyrododge in appstore, i cant figure out how to make it work...
Update:
This is what i have done so far, but the response is not smooth, and is very jittery..
xdiff and ydiff are the change of device attitude in x and y directions, derived from quatenion implementation.
if (currDeviceMotion.rotationRate.x < -2) {
NSLog(@"To Top");
if (195+800*ydiff >=193 && 195+800*ydiff <= 197) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (195+800*ydiff >=390 ) {
[self.pitchDot setFrame:CGRectMake(150, 390, 20, 20)];
}else if (195+800*ydiff <= 0){
[self.pitchDot setFrame:CGRectMake(150, 0, 20, 20)];
}else if(195+800*ydiff < 194){
[self.pitchDot setFrame:CGRectMake(150, 195+800*ydiff, 20, 20)];
}else if(195+800*ydiff > 196){
[self.pitchDot setFrame:CGRectMake(150, 195+800*ydiff, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.x > 2){
NSLog(@"To Bottom");
if (195+800*ydiff >=193 && 195+800*ydiff <= 197) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (195+800*ydiff >=390 ) {
[self.pitchDot setFrame:CGRectMake(150, 390, 20, 20)];
}else if (195+800*ydiff <= 0){
[self.pitchDot setFrame:CGRectMake(150, 0, 20, 20)];
}else if(195+800*ydiff < 194){
[self.pitchDot setFrame:CGRectMake(150, 195+800*ydiff, 20, 20)];
}else if(195+800*ydiff > 196){
[self.pitchDot setFrame:CGRectMake(150, 195+800*ydiff, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.y < -2){
NSLog(@"To Left");
if (150+500*xdiff >= 148 && 150+500*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+500*xdiff >= 300 ) {
[self.pitchDot setFrame:CGRectMake(300, 195, 20, 20)];
}else if (150+500*xdiff <= 0){
[self.pitchDot setFrame:CGRectMake(0, 195, 20, 20)];
}else if(150+500*xdiff < 148){
[self.pitchDot setFrame:CGRectMake(150+500*xdiff, 195, 20, 20)];
}else if(150+500*xdiff > 152){
[self.pitchDot setFrame:CGRectMake(150+500*xdiff, 195, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.y > 2){
NSLog(@"To Right");
if (150+500*xdiff >= 148 && 150+500*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+500*xdiff >= 300 ) {
[self.pitchDot setFrame:CGRectMake(300, 195, 20, 20)];
}else if (150+500*xdiff <= 0){
[self.pitchDot setFrame:CGRectMake(0, 195, 20, 20)];
}else if(150+500*xdiff < 148){
[self.pitchDot setFrame:CGRectMake(150+500*xdiff, 195, 20, 20)];
}else if(150+500*xdiff > 152){
[self.pitchDot setFrame:CGRectMake(150+500*xdiff, 195, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.x < -0.20 && currDeviceMotion.rotationRate.y > 0.20 ){
NSLog(@"To Diagonal Right Top");
if (150+650*xdiff >= 148 && 150+650*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+650*xdiff >= 300 ) {
[self.pitchDot setFrame:CGRectMake(300, 0, 20, 20)];
}else if(150+650*xdiff > 152){
[self.pitchDot setFrame:CGRectMake(150+650*xdiff, 195-1.3*650*xdiff, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.x > 0.20 && currDeviceMotion.rotationRate.y < -0.20 ){
NSLog(@"To Diagonal Left Bottom");
if (150+650*xdiff >= 148 && 150+650*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+650*xdiff <= 0 ) {
[self.pitchDot setFrame:CGRectMake(0, 390, 20, 20)];
}else if(150+650*xdiff < 148){
[self.pitchDot setFrame:CGRectMake(150+650*xdiff, 195-1.3*650*xdiff, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.x < -0.20 && currDeviceMotion.rotationRate.y < -0.20 ){
NSLog(@"To Diagonal Left Top and xdiff is %f",xdiff);
if (150+650*xdiff >= 148 && 150+650*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+650*xdiff <= 0 ) {
[self.pitchDot setFrame:CGRectMake(0, 0, 20, 20)];
}else if(150+650*xdiff < 148){
[self.pitchDot setFrame:CGRectMake(150+650*xdiff, 195+1.3*650*xdiff, 20, 20)];
}
}
}else if (currDeviceMotion.rotationRate.x > 0.20 && currDeviceMotion.rotationRate.y > 0.20 ){
NSLog(@"To Diagonal Right Bottom");
if (150+650*xdiff >= 148 && 150+650*xdiff <=152) {
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}else{
if (150+650*xdiff >= 300 ) {
[self.pitchDot setFrame:CGRectMake(300, 390, 20, 20)];
}else if(150+650*xdiff > 152){
[self.pitchDot setFrame:CGRectMake(150+650*xdiff, 195+1.3*650*xdiff, 20, 20)];
}
}
}else if((currDeviceMotion.rotationRate.x < 0.20 && currDeviceMotion.rotationRate.x < -0.20) ||(currDeviceMotion.rotationRate.y < 0.20 && currDeviceMotion.rotationRate.y < -0.20) ||(currDeviceMotion.rotationRate.z < 0.20 && currDeviceMotion.rotationRate.z < -0.20)){
[self.rollDot setFrame:CGRectMake(150, 195, 20, 20)];
[self.pitchDot setFrame:CGRectMake(150, 195, 20, 20)];
}
It could be great if i could make it move like the application i have stated above, called Gyrododge
This link also addresses a similar question:Link.
Any advice or help is greatly appreciated...
Thanks.
© Stack Overflow or respective owner