GLKIT Object Picking

For anyone who has played with OpenGL ES, its really hard work (iOS or Android). I have spent many hours on OpenGL ES 1.0 and 2.0, making my own frameworks etc.

Apple introduced GLKit in iOS 5 if I remembered correctly and it was designed to make the maths side easier. One of the things I struggle with is object picking, this is touching the screen and turning that in to a 3D position in the OpenGL world. Generally there is 2 ways to do this, Colour picking and Ray Picking.
Colour picking is when the user touches the screen, every object is rendered as a different colour and then you detect what colour that person touched to work out where they touched.

Ray Picking, is where you fire off a ray and work out what the first object is that it hits. This requires quite a bit of maths which people have already done for you but in different programming languages.

However, the point of this article is that I discovered that Apple have added functions to help with Ray Picking namely GLKMathUnproject

Code based from this StackOverFlow post http://stackoverflow.com/questions/20214498/updating-opengl-es-touch-detection-ray-tracing-for-ipad-retina

This works out when you touch the floor (self.plane) and then puts self.cube at that position

- (BOOL)didTouchObject: (CGPoint) tapLoc
{
tapLoc.x *= [UIScreen mainScreen].scale;
tapLoc.y *= [UIScreen mainScreen].scale;

bool testResult;

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

GLKVector3 nearPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-viewport[3])*-1, 0.0), _baseModelViewMatrix, _projectionMatrix, &viewport[0] , &testResult);
GLKVector3 farPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-viewport[3])*-1, 1.0), _baseModelViewMatrix, _projectionMatrix, &viewport[0] , &testResult);

//farPt = GLKVector3Subtract(farPt, nearPt);

float xDif = (farPt.x – nearPt.x) / 1000;
float yDif = (farPt.y – nearPt.y) / 1000;
float zDif = (farPt.z – nearPt.z) / 1000;

for (int i = 0; i < 100; i ++)
{
if ((nearPt.x + (xDif * i)) > self.plane.position.x – self.plane.scale.x && (nearPt.x + (xDif * i)) < self.plane.position.x + self.plane.scale.x &&
(nearPt.y + (yDif * i)) > self.plane.position.y – self.plane.scale.y && (nearPt.y + (yDif * i)) < self.plane.position.y + self.plane.scale.y &&
(nearPt.z + (zDif * i)) > self.plane.position.z – self.plane.scale.z && (nearPt.z + (zDif * i)) < self.plane.position.z + self.plane.scale.z)
{

NSLog(@”Hit cube”);
self.cube.position = GLKVector3Make(nearPt.x + (xDif * i), nearPt.y + (yDif * i), nearPt.z + (zDif * i));
return YES;
}

}

return NO;

}

A free location based Augmented Reality Engine for iOS

For an upcoming project I am working on, I needed an augmented reality camera view that overlays location based data which users can interactive with. It also needed to have a radar in the top corner indicating where these locations where.

When I first looked at Augmented reality back in 2011, the main examples where based on an open source project called iPhoneARKit. I was looking for an updated version and found this little gem:

iOSARKit by Carlos Alonso. I actually spoke to Carlos who was very helpful and friendly.

For more info, please check his blog

I have made a few tweaks to this which can be found here : Burf GitHub

Raywenderlich.com Podcast

For anyone who has ever searched Google looking for iOS tutorials, Raywenderlich.com is always near the top, if not at the top. The site offers some of the best tutorials out there on so many subjects.

Anyway, they are now doing a podcast which is currently on its 4th episode. It covers a wide range of things (iOS news, whats new on the site, tutorials, in depth discussions etc).

Check it out
Raywenderlich.com Podcast

Looking back at old code : SplatIT

After I had submitted Stupid Chicken I thought I would look back on SplatIT. This was the first ever game I made for iOS which led on to me making it for Android and Windows Phone 7. This simple whacker mole type game was purely a project to get something simple submitted and learn from it.

So I decided to revisit the code, boy have things changed. It was targeted at iOS3!! No blocks, storyboards, @2x etc, ARC.

So, for fun I thought I would spend a hour or so moving it through the ages. Getting it to work with an iPhone 5, using storyboards and ARC.

I deleted a lot more code than I added, by quite a bit! I was tempted to convert to SpriteKit but decided thats overkill and rather pointless. I could spend that time making a good game.

I have submitted the new version, added iAds, made it work with different screen resolutions etc.

Lets see what happens.

Stupid Chicken has been submitted to the Apple Appstore

So last night I set myself the target to release Stupid Chicken for the iPhone. This game was originally built at Dave’s Crazy Game Jam but was far from finished.

I used Trello to map out the minimum I needed to do.

– Sign backup as an Apple Developer as I can’t really release it under the work account. This led me to setting up all the certificates and provisioning profiles which is a lot easier in Xcode 5 than it used to be. Generally in my day to day job I don’t need to do this.

– Add all required graphics for all screen sizes (icons, backgrounds etc). I invested in iDraw which had really good reviews and was only £17.99 on the Mac store. It had a good tutorial on how to make nice buttons which I needed.

– Add game over screen, this for now is a simple alert.

– Add Facebook and Twitter sharing. This for the moment uses the native iOS social framework. Generally I would use the Facebook one but that felt a little over kill for the moment.

– Add Apple iAds. This was so simple to do. Well done Apple.

– Fix outstanding bugs!

I did this, and submitted the app. Hopefully it will be approved shortly.