CLLocation - Find magnetic declination / deviation at another location - iphone

I know how to find the true heading/magnetic heading for the location my phone is currently at, but is it possible to find the magnetic deviation/declination for a remote location?
What I would like to do is be able to drop a pin at a place on the map and find both the true bearing and the bearing with magnetic variance from that point.
Thanks!

The code to calculate this must already exist in a framework somewhere since it's used by CLHeading when location services are available.
If anyone can find that code or has Objective C for the world magnetic model posting it would be appreciated.
UPDATE: I found a great open source iOS wrapper! Thanks Crookneck!
https://github.com/stephent/ObjectiveWMM
Simple installation as a git submodule
run this command:
$git submodule add https://github.com/stephent/ObjectiveWMM.git ObjectiveWMM
Add these files to your Xcode project:
CCMagneticDeclination.h
CCMagneticDeclination.m
CCMagneticModel.h
CCMagneticModel.m
NSDate+DecimalYear.h
NSDate+DecimalYear.m
WMM/EGM9615.h
WMM/GeomagnetismHeader.h
WMM/GeomagnetismLibrary.c
WMM/WMM.COF

Have you solved it? Otherwise you can calculate the azimuth angle of the remote location. Firstly with magnetic north heading and then with true north heading. Finally you subtract the two to get the magnetic deviation.
Here is how to calculate an azimuth angle for a remote location:
-(float)azimuthFromLocations:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second{
float longitudeDifference = second.longitude - first.longitude;
float latitudeDifference = second.latitude - first.latitude;
float possibleAzimuth = (M_PI * .5f) - atan(latitudeDifference / longitudeDifference);
if (longitudeDifference > 0)
return possibleAzimuth;
else if (longitudeDifference < 0)
return possibleAzimuth + M_PI;
else if (latitudeDifference < 0)
return M_PI;
return 0.0f;
}

I needed the same solution in Swift, based on the World Magnetic Model.
Here is my implementation:
https://github.com/kanchudeep/Geomagnetism-Swift
It's a simple single class which can be dropped into any project and used.

Related

different RA/Decs returned by pyEphem

I using pyEphem to calculate RA/Decs of satellites and I'm confused by the different
values computed and described on
http://rhodesmill.org/pyephem/radec.html
this bit of code
sat=ephem.readtle("SATNAME ", \
"1 38356U 12030A 14148.90924578 .00000000 00000-0 10000-3 0 5678",\
"2 38356 0.0481 47.9760 0002933 358.9451 332.7970 1.00270012 3866")
gatech = ephem.Observer()
gatech.lon, gatech.lat = '-155.47322222', '19.82561111'
gatech.elevation = 4194
gatech.date = '2014/01/02 07:05:52'
sat.compute(gatech)
print 'a_ra=',sat.a_ra,'a_dec=',sat.a_dec,'g_ra=',sat.g_ra,'g_dec=',sat.g_dec,'ra=',sat.ra,'dec=',sat.dec
gives
a_ra= 0:52:40.75 a_dec= -3:15:23.7 g_ra= 1:14:10.55 g_dec= 0:06:09.8 ra= 0:53:23.57 dec= -3:10:50.5
if I change JUST the observers location to say
gatech.lon, gatech.lat = '-5.47322222', '19.82561111'
I get
a_ra= 1:15:36.95 a_dec= -2:32:29.9 g_ra= 1:14:10.55 g_dec= 0:06:09.8 ra= 1:16:19.75 dec= -2:28:04.6
I thought the observers position only came into the calculation of sat.ra and sat.dec
so was suprised to see a_ra and a_dec had changed.
What am I missing?
Thanks
Ad
Per the last paragraph of the “body.compute(observer)” section of the Quick Reference:
http://rhodesmill.org/pyephem/quick.html#body-compute-observer
For earth satellite objects, the astrometric coordinates [meaning a_ra and a_dec] are topocentric instead of geocentric, since there is little point in figuring out where the satellite would appear on a J2000 (or whatever epoch you are using) star chart for an observer sitting at the center of the earth.
And in the issue that has been opened about this behavior, the project is open to suggestions about where this text can appear more prominently to prevent future confusion for users:
https://github.com/brandon-rhodes/pyephem/issues/55

Monsters/Enemies on Platforms(like on Doodlejump)Cocos2d

I'm new here so i hope you can help me.
I am following a tutorial on making simple cocos2d game
Ray Wenderlich's Tutorial
I implemented it on another game a jumping one like doodle jump.
in the said tutorial the monsters/targets are moving freely coming from the right to the left side of the screen. when i implement it on my app the monsters are like flying from left to right. What if i want the monsters to stand on the platforms just like the one on doodle jump? what particular things will i do?
PS:i tried some other things on google but none works
Here is the code of the monsters/targets:
- (void)initPlatforms {
// NSLog(#"initPlatforms");
currentPlatformTag = kPlatformsStartTag;
while(currentPlatformTag < kPlatformsStartTag + kNumPlatforms) {
[self initPlatform];
currentPlatformTag++;
}
[self resetPlatforms];
}
- (void)initPlatform {
CGRect rect;
switch(random()%2) {
case 0: rect = CGRectMake(608,64,102,36); break;
case 1: rect = CGRectMake(608,128,90,32); break;
}
AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
AtlasSprite *platform = [AtlasSprite spriteWithRect:rect spriteManager:spriteManager];
[spriteManager addChild:platform z:3 tag:currentPlatformTag];
}
-(void)addTarget {
Sprite *target = [Sprite spriteWithFile:#"komodo.png"];
target.position = ccp(300,200);
[self addChild:target];
CGSize winSize = [[Director sharedDirector]winSize];
int minX = target.contentSize.height/2;
int maxX = winSize.height -target.contentSize.height/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) +minX;
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
id actionMove = [MoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width,actualX)];
id actionMoveDone = [CallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)];
[target runAction:[Sequence actions:actionMove, actionMoveDone,nil]];
target.tag = 1;
[_targets addObject:target];
}
Thanks to those who will help... you are so nice.
This is a pretty broad question unfortunately, which makes it difficult to answer in any definitive terms. If you are hoping to create actual platforms that can be bounced on (in a doodle jump manner) you are going to need to implement collision detection between the Monsters and the Platform ccNodes. There are numerous tutorials online for cocos2d collision detection, both simple implementation and the more advanced box 2d/chipmunk based solutions.
If you are looking to clone doodle jump fairly closely, there is an open source version of a clone available on github here - though I've not actually looked at the code.
Finally, if you mean that you simply want to restrict the movement of the monsters to a particular area of the screen (so they don't keep running off the edge) you just need to position the target to an area on the screen and alter theccAction so that the ccMoveTo uses the left most point of the 'platform' as the furthest point left it can move to and the right most point as the furthest right. (I'll confess I've not played Doodle Jump so have no idea what the enemies actually do).
If the enemies run back and forth across the platform you should look into using ccRepeatForever on your movement sequence and have two destination positions in the CCSequence : one that moves the monster to the left of the platform, the other to move it to the right.
Additional Info
Ok, I see what you are trying to do. This should get you started:
Platforms are created in initPlatforms. This calls initPlatform a number of times. This grabs an image from the AtlasSprite for the platform, creates a ccSprite for each platform and assigns it a unique tag.
Then, in - (void)step:(ccTime)dt it loops through all the platforms and moves them to their correct location based on how far the bird has moved:
for(t; t < kPlatformsStartTag + kNumPlatforms; t++) {
AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];
//etc...
So, the bit you are waiting for:
If you want to add a monster to these platforms, you will have to follow a similar pattern. To get started try something like this (You will want to have a cleaner design than this though but it should put you on the right track)
in initPlatform add the following to the end of the function
// add a monster sprite
AtlasSprite *monster = [AtlasSprite spriteWithRect:CGRectMake(608,128,64,64) spriteManager:spriteManager];
[spriteManager addChild:monster z:3 tag:currentPlatformTag + 1000];
(I've just grabbed an image from the existing Atlas. You could replace the above with your actual 'Monster' sprite object. Notice I add 1000 to thecurrentPlatformTag. This is just for testing; you should have a monsterTag implementation eventually.
So now every platform has a 'monster' (Again, you will only want to target random platforms)
so we need to update the positions for the monsters.
In - (void)step:(ccTime)dt directly after you get the current platform
AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];
You now also need to get the current monster (remembering to use the updated tag value we created for 'monsters':
AtlasSprite *monster = (AtlasSprite*)[spriteManager getChildByTag:t + 1000];
Then, a few lines below where we reposition the platform we will need to reposition the monster
platform.position = pos;
// We update the monster and set it a 32 pixels above the platform:
monster.position = ccp(pos.x, pos.y + 32);
So now each platform has a monster on it whose y position moves with the the platforms :-)
Hope this helps

Drag sprite in cocos2d for the iPhone - with a max velocity

I'm trying to make a game where the user is supposed to drag a sprite up and down on the screen, avoiding incoming obstacles. The last answer here helped me to drag the sprite around on the screen, but I want to set a maximum speed the sprite can be moved (and hopefully with a natural-looking acceleration/deceleration), so it doesn't get too easy to avoid the objects.
Does anybody know how I can modify the code to achieve this, or is there another way to to it?
Thanks :)
You'll need to maintain a CGPoint destinationPosition variable which is the location of your finger and use an update loop to modify it's position:
-(void) update:(ccTime) dt
{
CGPoint currentPosition = draggableObject.position.x;
if (destination.x != currentPosition.x)
{
currentPosition.x += (destination.x - currentPosition.x) / 5.0f; // This 5.0f is how fast you want the object to move to it's destination
}
if (destination.y != currentPosition.y)
{
currentPosition.y += (destination.y - currentPosition.y) / 5.0f;
}
draggableObject.postion = currentPosition;
}
In the ifs, you might want to check if the objects are close to each other, rather than exactly the same number to allow for rounding errors.
You just need to have an if statement in whatever schedule updater you are using, like time, or touches, or whatever.
I'm presuming you have x/y velocities? Just inside your update statement, wherever your acceleration is -
if(acceleration.x > 20){
acceleration.x = 20;
}
if(acceleration.y > 20){
acceleration.y = 20;
}

Finding if a given position (lat, lon) is within the BoundingBox of two coordinates (route-me)

I am working on an iPhone app using the Route-Me project.
I am using a method called 'latitudeLongitudeBoundingBoxForScreen' which returns a set of two cooördinates. Defining the Northeast and Southwest corners in lat/lon of your screen. This seems to work fine.
My question is how can I determine whether a given point (lat/lon) is within the boundaries of these two coordinates (northeast,southwest)?
Thank you very much in advance for your help.
For general point in a polygon, the Point Inclusion Test seems to work assuming a trivial Equirectanglular projection (x = lon, y = lat).
Since you know the north-east and south-west, you can compute the north-west and south-east corners. Also, keep in mind that your bounding polygon (box) must following a winding convention (you just can't throw points at the Point Inclusion Test).
This will break if the bounding box goes across the 180 lon line. The simple fix is if the box that crosses 180 lon line is to add 360 to the negative longitude. This isn't a valid navigation point but it makes the math work.
This also will not work around the poles.
I know that a long time has passedby, but maybe someone still interested.
Double lat_A, lat_B,lng_A, lng_B, curr_lat,curr_lng;
LatLng nw_corner, sw_corner;
Boolean lat_OK,lng_OK,all_OK;
int t_segs, points;
t_segs = segments.size()-1;
curr_lat = curr_LatLng.latitude;
curr_lng = curr_LatLng.longitude;
lat_A = list_segm_points.get(0).latitude;
lng_A = list_segm_points.get(0).longitude;
lat_B = list_segm_points.get(t_segs).latitude;
lng_B = list_segm_points.get(t_segs).longitude;
if (lat_A > lat_B ) {
lat_OK = curr_lat < lat_A && curr_lat > lat_B;
} else {
lat_OK = curr_lat > lat_A && curr_lat < lat_B;
}
if (lng_A > lng_B ) {
lng_OK = curr_lng < lng_A && curr_lng > lng_B;
} else {
lng_OK = curr_lng > lng_A && curr_lng < lng_B;
}
all_OK = lat_OK && lng_OK;
return all_OK;

cocos2d moving objects

Im trying to move an object around the screen like in the game geometry wars. I can rotate the object just fine, however I cant seem to get it to move based on the direction it is facing. I have this code here which i think is right for doing this but I keep getting syntax errors:
spriteObject.x = spriteObject.x + speed*cos(Angle)
spriteObject.y = spriteObject.y + speed*sin(Angle)
The errors are 'request for member x not in struct or union.' How do you do this in Objective-c/cocos2d syntax?
Looking at the documentation for the sprite class, you would need to do the following:
float angle = spriteObject.rotation
spriteObject.position.x = spriteObject.position.x + speed*cos(angle)
spriteObject.position.y = spriteObject.position.y + speed*sin(angle)
edit (in response to comment):
I see that you are programming for the iPhone, which means you need to be using the iphone cocos2d library, and not the one I linked to before.
The syntax will be different, as will the example code, since the iPhone version uses the Objective-C langugage, whereas the original cocos2d uses Python.
Google code has good documentation on the iPhone version of cocos2d, including sample code.
Based on that sample code, you will have to do the following:
float newX = spriteObject.position.x + speed * cos(angle);
float newY = spriteObject.position.y + speed * sin(angle);
spriteObject.position = ccp( newX, newY );