Augmented Reality apps - possible in Corona?

By the way, great tutorial!

Thank you Rob, its a great compliment especially coming from a legend like yourself :slight_smile:
 

AR is definitely possible in Corona, check out my Corona app Stickar. Though I eventually switched my AR code from lua to native for pixel perfect movement.

Here’s some really helpful code I’ve written in my year long exploration into AR. Haven’t tested in a while, changed some variable names around, but it should be a good starting point. Note: does not adjust to Landscape orientation.

Augmented Reality tracking in <100 lines of code

0: Quick setup

system.setAccelerometerInterval( 100 ) -- 100hz for best effect, higher battery drain local ar = {degrees = 0,height = 0,startDegrees = 0,startHeight = 0} -- degrees comes from the compass (for x AR movement) -- height comes from the accelerometer (for y AR movement) -- startDegrees is an offset for x AR movement (default is 0) -- startHeight is an offset for y AR movement (default is 0) -- AR camera local background = display.newRect(display.contentWidth\*.5,display.contentHeight\*.5,display.contentWidth,display.contentHeight) background.fill = {type = "camera"} -- gives you a fullscreen camera view background.width = display.contentHeight\*(3/4) -- get proper aspect ratio for camera

1: Augmented Reality tracking

local track\_degrees = nil local track\_degrees\_last = nil local track\_group = display.newGroup() -- group that moves with device orientation changes local test = display.newRect(track\_group,display.contentWidth\*.5,display.contentHeight\*.5,64,64) track = function (event) if not track\_degrees then track\_degrees = ar.degrees track\_degrees\_last = ar.degrees end -- flip tracking for full 360 deg rotation if ar.degrees-track\_degrees\_last \>= 180 then track\_degrees\_last = track\_degrees\_last + 360 elseif ar.degrees-track\_degrees\_last \<= -180 then track\_degrees\_last = track\_degrees\_last - 360 end track\_degrees = track\_degrees + (ar.degrees-track\_degrees\_last) track\_degrees\_last = ar.degrees local easex = 0 local easey = ar.height\*display.contentHeight if ar.startDegrees then easex = -(vt.track\_degrees-ar.startDegrees)\*16-(display.contentWidth\*.5) end if ar.startHeight then easey = (ar.height\*display.contentHeight)-(ar.startHeight\*display.contentHeight)-display.contentHeight\*.5 end local track\_speed = 3 -- lower value = faster tracking, more jittery track\_group:translate(((easex-track\_group.x)/track\_speed),((easey-track\_group.y)/track\_speed)) end Runtime:addEventListener("enterFrame",track)

2: Get ar.degrees

local compass = function( event ) if \_G.device == "Android" or event.geographic == nil then ar.degrees = event.magnetic else ar.degrees = event.geographic end if ar.degrees \< 0 then ar.degrees = ar.degrees + 360 elseif ar.degrees \>= 360 then ar.degrees = ar.degrees - 360 end end Runtime:addEventListener( "heading", compass )

3: Get ar.height

accelerometer = function ( event ) ar.height = event.zGravity end Runtime:addEventListener( "accelerometer", accelerometer )

4: insert AR object

local create\_AR\_object = function (obj) local xc,yc = obj:localToContent(0,0 ) track\_group:insert(obj) obj.x = -track\_group.x+(xc-W\*.5) obj.y = -track\_group.y+(yc-H\*.5) end local myObject = display.newCircle(0,0,64) create\_AR\_object(myObject)

Here are some other helpful pieces of code if you’re planning on making an AR/GPS game like Pokemon Go

1: Distance between two map coordinates, accurate whether you’re in China or the North Pole

Important because latitude and longitude distances change as you move away from the equator

local map\_distance = function (lat1,long1,lat2,long2) local R = 6371000; -- metres local x1 = math.rad(lat1); local x2 = math.rad(lat2); local deltax = math.rad(lat2-lat1); local deltalambda = math.rad(lon2-lon1); local a = math.sin(deltax/2) \* math.sin(deltax/2) + math.cos(x1) \* math.cos(x2) \* math.sin(deltalambda/2) \* math.sin(deltalambda/2); local c = 2 \* math.atan2(math.sqrt(a), math.sqrt(1-a)); local d = R \* c; return d end

2: Plot a point on a map using distance and angle

Simple, but a big time saver

local plotter = function (lat,lon,ang,dist) if lat and lon then local x = lat + dist\*math.deg(math.cos(math.rad(ang))) local y = lon + dist\*math.deg(math.sin(math.rad(ang))) return x,y end

I also have code for translating latitude and longitude to screen pixels (for AR camera) if anyone is interested.

Hope I helped and got someone started on a new Corona AR project!

Hi @AidanWolf, great tutorial. You also mention that you have some code for translating latitude and longitude to screen screen pixels ? Would you be open to share some of it please ?

regards.

Nick

Stickar looks interesting. Would like to try It but keep getting: “not compatible with this iPad” message on multiple iPads we’ve tried.

@nmichaud yes I do, though its currently a mess because it was for a specific app. I’ll make it “copy and paste ready” soon, and post it here.

@kilopop Stickar is an iPhone only app, but unknowingly the iTunes Store made it compatible for every device. This will be fixed once I figure out why haha.

Stickar doesn’t download on iPhone 5s iOS 9.3.2

  1. General Augmented Reality - Yes some apps that would be considered AR can be made w/ Corona.  I wrote a game for a client last year that cared where in the world and what physical features were nearby to make game decisions and present you with ‘encounters’.  (Game not released unfortunately as he got sidetracked by a bigger opportunity which I’m also coding for him.)

Note: I can’t give any more details that the above general statement till I get the OK from client or it is finally released.

  1. AR the way you described it (a very specific application of the concept).  No. I don’t think you could do that exact kind of app in Corona.  At least now without doing some Enterprise coding and using some sophisticated image analysis code, and likely other bits like tracking phone orientation and position.

Wasn’t Vuforia specifically designed for AR?  If so, we’re talking apples and oranges comparison.  Corona is a general SDK.  

Maybe you could integrate elements of Vuforia w/ Corona via enterprise?

Note: AR is much more than a specific usage like Pokemon Go which is what it sounds like you’re describing.  Corona can be used for a number of games and apps that would be considered AR.

PS - Some will disagree w/ my last statement which is cool, as I’ve read definitions of AR, but I tend to think any app that cares where you are in the world and adds additional data on top of that is enough to be AR.  

However, with the current Pokemon Go craze and the strong interest in various visualization technologies, most people will think that an app/game is only AR if it involves the overlaying of virtual elements onto a real-time video or camera capture.  Fair enough.

TL;DR  No I don’t think you can make a Pokemon Go style game in Corona Pro, but you might be able to w/ Enterprise.

Thanks roaminggamer.

Yes, Vuforia is specifically designed for AR. So instead of having to create something from the ground up in Corona, would a Vuforia plugin be possible in Corona? Like it is with Unity: http://www.marcofolio.net/other/introduction_into_augmented_reality_with_vuforia.html

I don’t write native plugins any more.  However, I imagine if I did and could get my hands on the vuforia libs and docs in a format compatible / build-able with Corona Enterprise, then the answer would be yes.

Sounds like you need to find a Enterprise developer.

Great, that’s encouraging. I will post something over in the Corona Jobs forum at some stage. For now, it would be interesting to hear if any Enterprise developers have achieved this to date or any other thoughts about Vuforia with Corona. It would be fantastic to avoid having to dive into Unity for this and maintain our Corona framework.

Corona cannot render 2.5, or so called 3D models?

As far as I can understand, that is required for game like Pokemon Go.

Furthermore, how to handle 2.5 (3D) collisions?

Rendering - Correct, Corona is 2D only.  You can however implement a variant of projection styles on your own using math, properly constructed artwork and a solid understanding of the projection style you’re using.

Collisions - In most cases, you’ll need to use math tricks, rules-of-thumb based in the projection style’s layout, and a throrough understanding of Box 2D collision beyond the basic collision filter rules.  In some cases, you’ll have to roll-your-own COLDET and Response system.

Probably not a satisfying answering, but… an answer. :expressionless:

I am not so skilled programmer as you Ed, so if I decide to move over to 3D, it is easier to go with Unity  :smiley:

Thanks.

Hey, if you’re going to actually make a 3D game I’d go with a 3D engine/SDK too.  However, I will say, 2.5D ain’t 3D, so you have to decide which way to go:

One MTE usage:

https://www.youtube.com/watch?v=kXKHlhMDlfg

Note: I think we’ve strayed a bit here… so my apologies to all, but I did want to finish answering the ‘appended’ questions about projections and Corona with samples (above).

Thanks Ed for all clarifications.

2.5D is good enough (like Zig-Zag and Crossy Road).

I think 3D is too much for indie (you need a team and graphic designer I think).

Is there any 2.5D relatively successful/known game made in Corona so far?

It seems that a lot of this conversation has focused on 3D imaging concerns.  What about simple, flat (2D) overlays on the camera at a given location?  Imagine I had people collecting “stickers” at specific geographic locations using their GPS and camera (with the overlay in the camera).  How difficult would that be in CoronaSDK?

Also, how precise would the GPS be overall?  And how does that translate to indoors?  Thanks for any clarification you might provide.

Corona SDK can capture input from the video camera and use it to fill a Corona display object (newRect for example) but on iOS only. We do not and have to no plans to support this on Android. It’s simply too difficult. You could display stickers on top of the camera input with little difficulty.

Now the GPS accuracy: You’re really at the mercy of the GPS system. They are as accurate as they are. It has to do with how many satellites are in view. The more sats your phone can see the more accurate it will be. You will be less accurate indoors since building block the relatively low power GPS signal and it’s harder to pick up enough, or it will fall back to using location data from WiFi routers.

Sometimes the GPS will put you with in a meter or so of reality other times it could be tens of meters off. This is the nature of GPS.

Rob

@gogigoranic8

Hi.  This is my last answer re: 2.5D here because this is not really related to the original question.  If you have further questions, please start a new thread and link back to this one to supplement the question/discussion.

Is there any 2.5D relatively successful/known game made in Corona so far?

  • Successful game?  I don’t know.
  • Successful example?  See my MTE links above.

@staypuffinpc

Adding to Rob’s last statement, I’d say that it is especially the nature of civilian GPS receivers to be inaccurate sometimes. Civilian GPS receivers are still not (but steadily improving) as accurate as military receivers.

This is an interesting read that talks about some of the reasons for this: http://www.gps.gov/systems/gps/performance/accuracy/