Augmented Reality in AppGameKit

A couple days back AppGameKit v2018.4.12 was released with the major new feature being AR (Augmented Reality) support.  I decided to give the new AR functionality a shot and it was really impressive how easy it was.  In order to get started with AR and AppGameKit you are going to need an AR compatible device.  On iOS, this means an ARKit compatible device, which basically means an iPhone 6S or newer device, while on Android device you need an ARCore compatible device from this list of phones.

I modified the AR example slightly, to remove a bit of functionality and to instead load a simple Tie Fighter model I downloaded off the web and converted to .X format.  AppGameKit can be coded using either C++ or their higher level Basic like script, which is what was used in this example.  Here is the slightly modified source code used:

// set window properties  SetWindowTitle( "AR Tie Fighter" )  SetWindowSize( 1024, 768, 0 )    // set display properties  SetVirtualResolution( 1024, 768 )  SetOrientationAllowed( 1, 1, 1, 1 )  SetScissor(0,0,0,0)  SetClearColor( 101,120,154 )  SetGenerateMipmaps( 1 )  UseNewDefaultFonts(1)  SetPrintSize(20)    // camera range from 0.1 meters to 40 meters  SetCameraRange( 1, 0.1, 40 )  SetAmbientColor( 128,128,128 )  SetSunColor( 255,255,255 )    // load tie fighter  LoadObject( 1, "tie.x")  SetObjectPosition( 1, 0,0.1,0 )  LoadImage(1, "diffuse.jpg")  SetObjectImage (1,1,0)   SetObjectRotation(1,270,0,0)    function ShowModel( show as integer )    SetObjectVisible( 1, show )  endfunction    ShowModel( 0 )    function ScaleModel( amount as float )    SetObjectScalePermanent( 1, amount, amount, amount )  endfunction    ScaleModel( 0.025 )    // create some planes to show detected surfaces, initially hidden  for i = 101 to 150    CreateObjectPlane( i, 1,1 )    SetObjectRotation( i, 90,0,0 )    FixObjectPivot( i )    SetObjectVisible( i, 0 )    SetObjectColor( i, 255,255,255,128 ) // 50% transparent    SetObjectTransparency( i, 1 )  next i    // add some buttons to control various features  AddVirtualButton( 1, 100,565,100 )  AddVirtualButton( 2, 100,665,100 )  SetVirtualButtonText( 1, "Scale +" )  SetVirtualButtonText( 2, "Scale -" )    AddVirtualButton( 3, 924,665,100 )  SetVirtualButtonText( 3, "Hide" )    function ShowHUD( show as integer )    SetVirtualButtonVisible( 1, show )    SetVirtualButtonVisible( 2, show )    SetVirtualButtonVisible( 3, show )    SetVirtualButtonActive( 1, show )    SetVirtualButtonActive( 2, show )    SetVirtualButtonActive( 3, show )  endfunction    // initialize AR, if possible  ARSetup()  while( ARGetStatus() = 1 )    // wait while user is being prompted to install ARCore    Sync()  endwhile    AnchorID as integer = 0  ShowPlanes as integer = 1  ambientScale# = 1.0    do    // get light estimation    ambient = ARGetLightEstimate() * 255 * ambientScale#    SetAmbientColor( ambient,ambient,ambient )        // check screen tap for plane hits, but only if buttons are visible    if ( GetPointerReleased() and ShowPlanes = 1 )      // check the point that the user tapped on the screen      numHits = ARHitTest( GetPointerX(), GetPointerY() )      if ( numHits > 0 )        ShowModel( 1 )        // delete any previous anchor, could keep it around instead        if ( AnchorID > 0 ) then ARDeleteAnchor( AnchorID )        // hit test results are ordered from closest to furthest        // place the object at result 1, the closest        AnchorID = ARCreateAnchorFromHitTest( 1 )        ARFixObjectToAnchor( 1, AnchorID )      else        // if the user didn't tap on any planes then hide the object        ShowModel( 0 )      endif      // clean up some internal resources      ARHitTestFinish()    endif        // place the buttons at the edge of the screen    // needs to be done regularly in case orientation changes    SetVirtualButtonPosition( 1, GetScreenBoundsLeft()+105, GetScreenBoundsBottom()-210 )    SetVirtualButtonPosition( 2, GetScreenBoundsLeft()+105, GetScreenBoundsBottom()-105 )    SetVirtualButtonPosition( 3, GetScreenBoundsRight()-105, GetScreenBoundsBottom()-105 )        // detect button presses if they are visible    if ( ShowPlanes = 1 )      if ( GetVirtualButtonPressed(1) )        ScaleModel( 1.05 )      endif      if ( GetVirtualButtonPressed(2) )        ScaleModel( 0.95 )      endif      if ( GetVirtualButtonPressed(3) )        ShowPlanes = 1 - ShowPlanes        ShowHUD( 0 )      endif    else      // screen tap whilst button are hidden shows them again      if ( GetPointerReleased() )        ShowPlanes = 1 - ShowPlanes        ShowHUD( 1 )      endif    endif        // hide old planes      for i = 101 to 150      SetObjectVisible( i, 0 )    next i        // show detected planes    if ( ShowPlanes )      numPlanes = ARGetPlanes(0)      // this demo stops at 50 planes, but there is no internal limit      if numPlanes > 50 then numPlanes = 50      for i = 1 to numPlanes        SetObjectPosition( i+100, ARGetPlaneX(i), ARGetPlaneY(i), ARGetPlaneZ(i) )        SetObjectRotation( i+100, ARGetPlaneAngleX(i), ARGetPlaneAngleY(i), ARGetPlaneAngleZ(i) )        SetObjectScale( i+100, ARGetPlaneSizeX(i), 1, ARGetPlaneSizeZ(i) )        SetObjectVisible( i+100, 1 )      next i      ARGetPlanesFinish()    endif            if ( ShowPlanes )      Print( "FPS: " + str(ScreenFPS()) )      select( ARGetStatus() )        case 2 :  Print( "AR Active" ) : endcase        case -1 :  Print( "AR Not Available" ) : endcase        case -2 :  Print( "AR Install Rejected" ) : endcase      endselect      Print( "Number of Planes Detected: " + str(numPlanes) )      Print( "Light Estimation: " + str(ARGetLightEstimate()) )      Print( "Light Boost: " + str(ambientScale#,1) )    endif          // draw the camera feed, and then the rest of the scene    ARDrawBackground()      Sync()      RotateObjectLocalZ(1,1)  loop  

You can see the results of this code and get a bit more detail by watching the video below:

If you are interested in learning more about AppGameKit, be sure to check out our Closer Look available here.

Programming Programming


Scroll to Top