I have an image of a bottle, whenever user shakes the device i want to move that image in that direction.
like up, down, left or right.
For example: if user is shaking the device in left direction i want to move that image in left direction.
i can detect shake event using
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake)
{/*some code*/} }
but i dont know how to detect the direction.
i dont even if this is possible or not.
please help me.
A shake is typically a back and forth movement. Use the accelerometer data to detect a sudden movement in one particular direction (e.g. left) or a tilt towards a different orientation.
You can get user motion values using CMMotionManager class.Please refer the following link
http://developer.apple.com/library/ios/#documentation/CoreMotion/Reference/CMMotionManager_Class/Reference/Reference.html#//apple_ref/occ/cl/CMMotionManager
Filter your acceleration and gyro dat detect direction of motion.
Related
Can someone point me in the right direction for how to simulate a multi touch tap in Instruments?
There are a few functions in which the number of touches is a parameter, but I don't understand how to actually define the coordinates of each touch.
For example, I need to simulate the user holding down a touch (or tap) at coordinate x1,y1 and x2, y2.
The application is not using standard accessible UI objects so I can only use coordinates.
I think the following should represent a held tap at specific coordinates, but it's only a single finger tap.
target.frontMostApp().mainWindow().scrollViews()[0].touchAndHold(1.7);
If you really need a two finger
Try performing a swipe action that doesn't move very far
target.frontMostApp().mainWindow().tableViews()[1].cells()[2].dragInsideWithOptions({startOffset:{x:0.0, y:0.1}, endOffset:{x:0.5, y:0.1}, duration:0.25});
I have a game where the main character is on the far left side of the screen (landscape) in the middle. He rotates using the accelerometer. Hes going to be shooting bullets constantly and I need a way to make the bullets fire depending on his rotation.
So basically I dont know the math to make the bullet fire straight depending on the sprites rotation.
Any help appreciated.
Edit:
I know my question is kind of vague, I dont really know how to ask it.
My character is a top view of a turret, he will rotate when you rotate your phone, and obviously as he rotates, the bullets still need to shoot straight. I just dont know the math to make this happen.
I assume you know how get values from accelerometer and the character "rotates" up and down (angry birds style).
If you fire your bullets by some amount of coordinates (moveBy(x,y) ) - just use more y to fire up. To fire down use more x.
For example: To fire in straight line - moveBy(10,10)
To fire up - moveBy(10,20);
To fire dowp - moveBy(20,10);
General case - moveBy(x*accelerometerVal, y*(1/accelerometerVal) );
I need to display cube in iphone which can be rotated and when touched on any side it should display which side has been touched
Here is the link for the demo example for cocos3d
You Can Use touches method instead of the accelerometer
I would like to use the accelerometer to move my player sprite.
If the sprite is going straight and the player tilts a little to the left, the sprite should rotate a little bit to the left, and same for the right.
I also want to detect how much the player has tilted the device and turn the sprite accordingly.
e.g. If the player tilts the device a lot the sprite should rotate 90 degrees rather than 45 for a quick tilt in a direction.
How does one do this. Detect the device movement in any direction, and for a small movement, the sprite should rotate less and for a larger rotation the sprite should rotate more.
I have experimented a little and dont get the results. Some times it works for clockwise rotations to the up, right and down movements, but not for the left movements.
What is the math behind this. An example would be the way a device detects its orientation and rotates the screen.
How does one do this correctly?
For accelerometer only detection chances are bad. You might look look at this question. If you don't need to rely on older iPhone versions (<4) or iPad, you should use the gyroscope instead. Take core motion API and start with teapot example from WWDC 2010 - you can find it here
I am developing one game where I want to move UIImageView based on accelerometer. When I rotate iphone device left to right or right to left the UIImageView have to rotate in particular angle. It's moving also but the problem occurs when I play background sound because of that sound, it sends some acceleration point even if my iphone is idle.
So my UIImageView is also moving. It should not happen. When I decrease the iphone sound volume it works very well. What I have to do for that.
And also if anyone knows how to get acceleration point only when iphone is moving from left to right or right to left. It should not detect when iphone is xz or yz plane.
If anybody knows the solution please reply.
Have you got any filtering on the input from the accelertometer? I would expect the noise from the speaker the accelerometer is picking up is vastly different in amplitude and frequency than the game control.
There is a simple low pass filter in the Apple accelerometer graph sample code.