Homing missiles using PID Controller for Cosmosis

This is a quick update on progress of my “update” to Cosmosis. I have got a fair amount done, but find myself falling into the trap I got myself into when I first developed Cosmosis – the “just one more feature” syndrome! I am going to be a lot stricter this time around, and won’t spend too much time updating, but I just had to have homing missiles.

My first try was based on the code I used for a small space shooter prototype I created recently in XNA 4.0. I converted the code across to ObjectiveC / cocos2d and got basic “homing missiles” working in Cosmosis. The problem is they just don’t seem to have that cool “arcing” / “homing” feel to them!

I then stumbled across this excellent resource on basic PID controllers (Part I – Missiles), by a clever guy by the name of Matthew Klingensmith. I had an initial go at implementing his Proportional Controller model for homing missiles, but couldn’t quite get it right. I was lucky enough to get help from him over email and he explained the way it worked over a couple of e-mails and pointed out a couple of errors I had in my initial code attempt. Kudos to Matt!

I was then able to get the model implemented. The end result is this awesome Smart Missile object that I have now sub-classed from CCSprite in cocos2d. Anyway, enough of the talking. Here is a quick video I created of the results. The missiles still need some tweaking to be done, and I have commented out the collision detection so you can see them tracking enemies a little easier (hence the fact that they don’t explode on impact). Oh, I also added a little particle effect to them for their thruster effect, this could also do with a little tweaking.



2 thoughts on “Homing missiles using PID Controller for Cosmosis”

    • Hi Kyle,

      Good question, and one I don’t really have a solid answer for! I have also heard this argument, and it seems there are conflicting opinions. I personally am getting just what I need for the type of game I am making my subclassing CCSprite. There are quite a few threads on StackExchange and the cocos2d-iphone forums that go into details of why each is good/bad. For now I’m happy subclassing CCSprite, and if I run into any limitation or knock-on effects I’ll take to the forums to read these posts in detail.



Leave a Comment