Sunday, October 24, 2010

Predictive Collision Detection

The Problem

On certain applications, specially games, it is often necessary to determine if two moving objects collide. The standard approach is to check for overlap after each step of the animation. While this approach may be sufficiently accurate for some applications, sometimes you need to determine the exact point of collision so that you can apply collision forces or handle the situation more accurately. Also, if objects are moving too fast the overlap approach may miss the collision entirely.

Here we will have a look at a predictive, or a priori, technique specially suited for particles or small objects. It detects collisions even if the bodies are moving fast and it's pretty accurate at determining the collision point.


Wednesday, October 13, 2010

The Observer Pattern in Objective-C

The Motivation

I've always wondered why the Foundation and Cocoa frameworks in Objective-C are so biased towards the delegate pattern. Don't get me wrong, I don't have anything against it. But there are some cases where using the observer pattern would make more sense. Take, for instance, the NSAnimationDelegate protocol. Most of the 'delegate' methods are actually just state change notifications. Out of the five methods in this protocol only one is an actual delegate method: animation:valueForProgress:.

The absence of the observer pattern, especially in the UI classes, prompted me write a reusable observable class to use in my custom UI code. Similar to Java's Observable class.

Thursday, October 7, 2010

Making NSInvocations

The Problem

NSInvocation is a pretty useful class. It is Ovjective-C's version of a function object. It encapsulates a method call allowing you to pass it around, modify it, and call it later.

The problem with NSInvocation is how hard it is to initialize. Here is the simplest NSInvocation construction I can think of:
SEL sel = @selector(myMethod);
NSInvocation* inv = [NSInvocation invocationWithMethodSignature:
                     [self methodSignatureForSelector:sel]];
[inv setTarget:self];
[inv setSelector:sel];
Clearly this is unwieldy at best, especially if you just want to make a method call.

Even worse is trying to create an invocation when all you have is a protocol. I came across this problem when trying to implement the observer pattern in Objective-C. I wanted to call all observers in a loop, without having to copy and paste the loop code for every observer method I had. And I still wanted to support methods that take non-object parameters, which means performSelector: was not an option.

Sunday, October 3, 2010

Intro

Let me start by telling a bit about myself. I started programming when I was 14 years old, and have been programming ever since. I loved it then and I love it now. I can say I am a programmer by heart. Since I started learning about computers my continuing goal has been to learn more and do increasingly cool stuff. Even though I now work at a software company, I still spend some of my free time programing.

Now about this blog. I figured that oftentimes I start a project and never finish it, or even if I finish it I never do anything with it, it just collects virtual dust in some dark folder. So I decided to start a blog to give myself some sense of direction and some sense of achievement. And also sharing what I find, hopefully someone will help me improve or suggest new ideas.

So enjoy, and by all means comment and constructively critique!