Game Design: Checking for object intersection or getting values from accelerometer - iphone

I'm currently developing an iPhone game where the player needs to tilt the device to do something. The game is somewhat of a memory game with the four corners of the screen being possible targets. The object of the game is to remember the order and then move the device to the right place.
My question is more about the design of the moving mechanic. The two options that I thought of were to get the values from the accelerometer directly and when the are greater than a specific value return whether that was the correct place to go (ie the right corner for the given instruction). My second idea is that each corner would have its own CGrect and the accelerometer would move an other CGrect and when the two intersect it would return whether the move was right or wrong.
In your opinion which one would be best? I think that the accelerometer data would be quicker but it might be affected by sudden movements while the other way might be slower but more accurate. Let me know what you think.

I think you should try both, test each of them on players, and see which works better. Drive game design decisions from user testing whenever possible.
My speculation is that you are going to need to damp or accumulate the accelerometer data somehow, since it is noisy; and if you are integrating that data into a moving average, then you should show where that moving average is with eg an onscreen sprite.
You probably don't even need to use the CGRect's intersection -- if you're just trying to determine whether <x0,y0> is within r units of <x1,y1> then you can do it via a simple Pythagorean distance. But the important thing is that if there is some internal state in the algorithm calculating what the accelerometer data has integrated to, then you need to show that state onscreen to feel responsive.

Related

Get distance height with accelerometer

I have searched for hours now and still didn't find a definitive answer to my problem.
The scenario is this: the user throw an iPhone as high as he can and I want to measure the height that the iPhone has done.
I want to use the accelerometer with Core Motion and I successfully implemented a simple system that gives me the acceleration on the 3 axis. This is an acceleration though.
Based on my physics knowledge, the formula to calculate the maximum height is (V0^2)/2*g where V0 is the starting velocity.
I have the acceleration velocity though.
Any idea how can I convert the acceleration to velocity or directly get the velocity from my accelerometer?
I know it's not a completely programming related question, but I just want to have some help on this :)
First of all there is alredy an app that does exactly what you are up to: Send Me To Heaven. You won't find it in Apple's App Store because it never passed the review, guess why ;-)
As you stated you only have access to accelerations. h = v02/(2*g) is correct. To get the starting velocity v0 you need to integrate the acceleration numerically over the time. The trickiest part will be to find necessary and sufficient conditions to determine the time interval [t1, t2]. When did the acceleration phase start and when did it stop.
Another thing to consider is to avoid cheating users who just perform a rotation around there axis for a couple of seconds, then simulate the flying phase. There you might consider the landing phase too: when the user grabs the device you should register a strong deceleration.
However, don't expect this app to ever get in the store and at Google Play the competitors were faster.
Problems:
There's a number of problems physics impose on you (other than the velocity), to get the result.
The angle of the throw, relative to the direction of gravity. You cant know the relative vertical distance, unless you know this angle.
Orientation of reference throughout the throw (you cannot deduct the speed from the acceleration, from the device itself, unless you account for the changes in rotation while the phone accelerates).
However! You can decide to assume certain things, which will make these annoying problems go away!
Reasonable assumption:
The device is caught again, at the same relative height it was thrown.
This assumption reduce the problem to a much simpler one, in which we only really need to find the duration of time, where the device is in free fall, in order to determine the relative height of the throw.
All you have to do:
To determine if the device is in free fall, is relatively easy, since the total gravity would be near 0 m/s^2.
However, there's still one smallish problem to this, because the accelerometer is probably not located at the center of mass of the phone, so it will experience a constant acceleration (if the phone rotates around itself) in exactly one particular direction, throughout the free fall.
The maths of determining the height of a vertical throw, based on the airtime duration is left as an exercise to the reader :-)

Execute action on tilt with Accelorometer

I want to create an iPhone application that executes certain actions based on what direction you tilt your device. For instance if you tilt the device so your phone screen is pointing towards the floor execute action x, if you tilt your phone so the phone screen is pointing towards the sky execute action y. I found some examples using the iPhones accelerometer in order to detect the tilt of the phone, but the values the accelerometer generated were so sporadic it was hard to execute a certain action based on specific values.
I'm relatively new to using the accelerometer within applications so I might be going about this completely the wrong way, any help would be appreciated.
The raw data out of the accelerometer is pretty jittery. You'll want to apply a high pass filer or a low pass filter at a minimum to the raw data. See the Apple sample code AccelerometerFilter.m for some basics on how to use it. I found that that wasn't sufficient and I keep a time-moving average of the data to accomplish what I need. You'll certainly need to play around with this to get it to do what you want it to do.

How to handle a game world that wraps, using cocos2d on the iPhone

I have a game world that's much bigger than the view port, the main character stays in the center of the view port at all times and the background layer is moved around to give the impression of the character moving. I want to make it so that the game world wraps, meaning if the the character keeps traveling either left or right they will eventually end up back at the starting position. There will be moving entities in the game world so the biggest problem I foresee is that if you go to the far right of the map you should be able to see any of the moving entities that are within the first small section of the far left of the map.
I've thought a bit about this and any solution I've come up with seems far too complicated. Like creating two identical game worlds side by side and moving them around accordingly. I live in hope that there is an elegant solution to this. Any expertise you can share would be greatly appreciated.
I'm using cocos2d on the iPhone just in case that makes any difference.
An example might be to have an x,y offset for your camera, and a multidimensional array of sprite objects.
As the player moves, the offset value changes, e.g., xMove = -1.4 and yMove = +2.6.
Then you would iterate and change the positions of all the tiles by that amount.
Next, you would identify the sprites that are too far away from the center of the screen (0,0) and re-position them to the opposite side, so they will always be visible.
This would all be done on the same scheduled 'tick' so no graphical artifacts occur.
I'm pretty surprised no one has made a wrappable tile map yet for cocos2d.
I can't give cocos2d specific advice, but I would say the most common way to do this is to create one game world, draw (parts of it) multiple times and make sure that your logic for things like collisions and AI checks for wrap-around where appropriate.
So if your player character is close to the corner of the world, you'd draw the world four times with different offsets. This needn't actually draw every single thing in the world four times any more than you would normally need to draw the entire world when only a small part of it is on-screen.

Gravity as frame of reference in accelerometer data in iOS

I'm working on an iPhone app for motorcyclist that will detect a crash after it has occurred. Currently we're in the data acquisition process and plotting graphs and looking at data. What i need to log is the forward user acceleration and tilt angle of the bike relative to bike standing upright on the road. I can get the user acceleration vector, i.e. the forward direction the rider is heading by sqrt of the x,y and z accelerometer values squared. But for the tilt angle i need a reference that is constant, so i thought lets use the gravity vector. Now, i realize that deviceMotion API has gravity and user acceleration values, where do these values come from and what do they mean? If i take the sqrt of the x,y and z squared components of the gravity will that always give me my up direct? How can i use that to find the tilt angle of the bike relative to an upright bike on the road? Thanks.
Setting aside "whiy" do this...
You need a very low-pass filter. So once the phone is put wherever-it-rides on the bike, you'll have various accelerations from maneuvers and the accel from gravity ever present in the background. That gives you an on-going vector for "down", and you can then interpret the accel data in that context... Fwd accel would tip the bike opposite of braking, so I think you could sort out fwd direction in real time too.
Very interesting idea.
Assuming that it's not a "joke question" you will need a reference point to compare with i.e. the position taken when the user clicks "starting". Then you can use cos(currentGravity.z / |referenceGravity|) with |referenceGravity| == 1 because Core Motion measures accelerations in g.
But to be honest there are a couple of problems for instance:
The device has to be in a fixed position when taking the reference frame, if you put it in a pocket and it's just moving a little bit inside, your measurement is rubbish
Hmm, the driver is dead but device is alive? Chances are good that the iPhone won't survive as well
If an app goes to the background Core Motion falls asleep and stops delivering values
It has to be an inhouse app because forget about getting approval for the app store
Or did we misunderstand you and it's just a game?
Since this is not a joke.
I would like to address the point of mount issue. How to interpret the data depends largely on how the iPhone is positioned. Some issues might not be apparent to those that don't actually ride motorcycles.
Particularly when it comes to going around curves/corners. In low speed turns the motorcycle leans but the rider does not or just leans slightly. In higher speed turns both the rider and the motorcycle lean. This could present an issue if not addressed. I won't cover all scenarios but..
For example, most modern textile motorcycle jackets have a cell phone pocket just inside on the left. If the user were to put there phone in this pocket, you could expect to see only 'accelerating' & 'braking'(~z) acceleration. In this scenario you would expect to almost never see significant amounts of side to side (~x) acceleration because the rider leans proportionally into the g-force of the turn. So while going around a curve one would expect to see an increase in (y)down from it's general 1g state. So essentially the riders torso is indexed to gravity as far as (x) measurements go.
If the device were mounted to the bike you would have to adjust for what you would expect to see given that mounting point.
As far as the heuristics of the algorithm to detect a crash go, that is very hard to define. Some crashes are like you see on television, bike flips ripping into a million pieces, that crash should be extremely easy to detect, Huh 3gs measured up... Crash! But what about simple downs?(bike lays on it's side, oops, rider gets up, picks up bike rides away) They might occur without any particularly remarkable g-forces.(with the exception of about 1g left or right on the x axis)
A couple more suggestions:
Sensitivity adjustment, maybe even with some sort of learn mode (where the user puts the device in this mode and rides, the device then records/learns average riding for that user)
An "I've stopped" or similar button; maybe the rider didn't crash, maybe he/she just broke down, it does happen and since you have some sort of ad-hoc network setup it should be easy to spread the news.

How to detect height of iPhone (for use in augmented reality game)?

I'm working on locating an iPhone device in 3D space.
I can use lat/long to detect physical location, I can use the magnetometer to figure out the direction they're facing, and I might be able to use the accelerometer to figure out how their device is oriented, but I can't figure out a way to get height of the device off the floor.
Specifically, I need to know if the user is squatting down, or raising their hand toward the ceiling (a different of about 2 meters/6 feet).
I posted a more detailed description of what I'm trying to do on my blog: http://pushplay.net/blog_detail.php?id=36
I would love any suggestions as to how to even fake this sort of info. I really want the sort of interactivity and movement that would require ducking and bobbing, versus just letting someone sit back and angle the phone -- kind of the way people can "cheat" playing with a Wii...
The closest I could see you getting to what you're looking for is using the accelerometer/magnetometer as an inertial tracker. You'd have to calibrate the user's initial position on startup to a "base" position, then continuously sample the sensors on a background thread to build a movement model. This post talks about boosting the default sample rate of the accelerometer functions so that you can get a pretty fine-grained picture of the user's movements.
I'm not sure this will solve your concern about people simply angling the device to produce the desired action, but you will have to strike a balance between being too strict in interpreting movements and allowing for differences in movement
The CoreLocation stuff gives you elevation aswell as lat/long, so you could potentially use that although there are some significant problems with this:
Won't work well indoors (not a problem for Sat Nav, is a problem for games)
Your users would have to "calibrate" (probably by placing the phone on the floor) each location they use!
In fact, you'd need to start keeping a list of "previously calibrated locations"... which could vary hugely just in one house (eg multiple rooms and floors). Could get in the way of the game.
Can't be used on moving transport (tranes, planes, automobiles... even walking) because elevation changing so frequently.
Therefore I'd have thought that using the accelerometer as a proxy for height is a substantially more preferable route than determining absolute elevation.
I am not intimately familiar with the iphone. But it might require a hardware add-on. (which you probably don't want to do). After thinking on this the only way I know how is through light or more specific laser. You shoot out a laser on the floor and record the time it takes to get back. It's actually not a lot to put this hardware together and I am sure the iphone has connections for peripherals. Unless osmeone can trump me, I say ther eis no way to do that with an image.