Building Comanche 3D in less than 100 lines of code (UPDATE)


If you are a child of the 90s, you certainly will remember Comanche: Maximum Overkill. The graphics were awesome and way ahead of its time. Remember, we did’t have fancy 3D accelerated graph cards. With your Comanche you could fly over the landscape, diving towards the enemy. It was fun! I’ve must have spend hundreds of hours playing with this.

It is surprising how little code is needed to reproduce this experience. In the article on my blog, we’ll build the 3D world and will be able to fly, dive etc over the landscape with Xojo in less than 100 lines of code!

The full source code can be found here: http://alwaysbusycorner.com/2014/08/08/xojo-building-commanche-3d-in-100-lines/

Nicely done! That is remarkable efficient coding Alain!

Under what license are you releasing your code? Would you mind if I used your routines it in the open source X3 project, that I’m working on? Will give credit to Alwaysbusy’s Corner.

Code is free @Alwyn Bester . Only ABXVision has some licencing, all the rest of my blog code if free to use. Some credit is always nice of course :slight_smile:

Great stuff. Looking forward to play with the code :slight_smile:

Just a quick one, not sure if it’s a focus thing on Windows, but I had to move the KeyDown code from the Canvas event to the keydown event of the Form, for the navigation to work.

I’ve coded it on Windows in RB2007R4. Maybe the form got the focus, not the canvas when you start up. Will post your solution on my blog in case someone else has this problem. thx

Maybe something changed between Real and Xojo when it comes to the initial focus. I ran the code on X2014R2.

This is so awesome, great code! FYI, the OS X binary fails with a NOE here on OS X 10.9.4 - I haven’t had a chance to build from source or try the Windows version yet.

On OSX you need to switch it to build for Cocoa.

I too lost many hours in the cockpit of the Comanche. What a great trip down memory lane :smiley:

Thanks @Bill Gookin and @Gavin Smith Not being a mac man myself, I hadn’t tested it on OSX. Note that this is some old code I had laying around and a reader from reddit just pointed my back to the original coder (Sebastian Macke) who made it in javascript.

The problems with OSX should be resolved. If anyone could test it on a real Mac (I used VMware) Thanks!

Yes, the OS X binary is working now. Very impressive.

Alain this is pretty cool stuff and I am deeply Impressen how easy voxel algorithm is…
Runs perfect on Mac OS X here and I’ve found a snipped here in forum to speed up drawing of canvas a little bit and added this to the .Paint Event of the Canvas.

#pragma BackgroundTasks false
  
  #if TargetMacOS
    
    declare sub CGContextSaveGState lib "Cocoa" ( context as integer )
    declare sub CGContextSetInterpolationQuality lib "Cocoa" ( context as integer, quality as integer )
    declare Sub CGContextRestoreGState lib "Cocoa" ( context as integer )
    
    dim CGContextRef as integer = g.handle( g.HandleTypeCGContextRef )
    
    CGContextSaveGState CGContextRef
    CGContextSetInterpolationQuality CGContextRef, 1
    
  #endif
  
  ' draw the buffer to the canvas
  if mBuffer <> nil then
    g.DrawPicture mBuffer,0,0
  end if
  
  #if TargetMacOS
    CGContextRestoreGState CGContextRef
  #endif
  
#pragma BackgroundTasks true
  

I’ve also used the usual suspects of #pragma diectives (BackgroundTasks, Checks etc.) to speed this up a bit…

Have you tried drawing directly to the canvas, rather than caching it to a Picture object? This will give you a speed boost on the Mac, especially as the Mac OS is already double buffered?

If you must cache it on the Mac, then I would advise using CG commands directly and caching to a CGLayer, as this is lightning fast! CGLayers is how we managed to do animation at 245 fps in a full screen Retina ready application. But it does require using native CG as opposed to Xojo functions.

I am getting like 2-5 FPS on my iMac i5.

Yes there is plenty room for optimizations :wink:
When running compiled app I get a framerate around 10 fps…

Tonight (with my MacBook in bed) I’ve added two Timers, one for reading the cursor keys (Keyboard.AsyncKeyDown) every 100 ms and another one for updating the scene in order to add more functionality.

Cursor keys now sets a speed variable, and the Speed is indicated on HUD Screen. I wanted to add a collision detection next but hence… the time… it’s always the time… I am passing the baton to the next one…

https://dl.dropboxusercontent.com/u/30535865/xojo/ABComanche.zip

Cool that people are looking into this! Indeed, there is a lot that can be done to speed this up, but the subject of the article was the algorithm in pure Xojo in 100 lines of code.

With some fiddling, a lot of the original game can be created now that the engine is here. When I find the time, I’m going to try to emulate the ‘swing’ the heli does when turning.

I’m going to check out your versions (Nice, the HUD is already working!) May I use some of the code to do a follow up article? Credits will be given.

Hi Alain you’re completly right and of course you may use the code. There’s nothing special about these few lines I added. And I am sure somebody (with more time) will improve it more.

I’ve improved the steering a bit. Now the chopper lowers nose when moving forward. I’ve also changed the cockpit MFDs.
Makes a lot of fun!

When this project became an engine for the original game, this would be pretty cool. :slight_smile: