Posts Tagged 'geometry'



Gyroscope adventure

In case you can’t fully understand yet what Adobe means by focusing on (desktop) gaming and video and not general purpose apps, allow me to demonstrate. Suppose you are making mobile app and for whatever reason you want to track device orientation. How hard can that be? Check it out:

Apple way

If you’re native iOS app developer, you are dealing with ecosystem that does support general purpose apps. You have simple CMDeviceMotion class that performs sensor data fusion, filtering, bias removal, etc, and gives you orientation data in any form you want (roll + pitch + yaw, or matrix, or quaternion). Case closed. The same class conveniently splits accelerometer data into gravity and user applied acceleration, btw. In a word, it’s perfect.

Adobe way

If you’re AIR app developer, prepare yourself – you’re going on the adventure. You can start by looking at flash.sensors package – for some mysterious reason, it has accelerometer API for desktop, but no gyroscope API for mobile. Why? Because Adobe does not feel obliged to include every piece of code you might think of in their runtime. But don’t give up your hope yet – Adobe has the solution for this kind of problems – AIR native extensions. And, it turns out that Adobe made one for you to access gyroscope data. Despite of being abandoned in 2011, apparently just before Adobe’s focus shift kicked in, this ANE still works with current AIR 3.x.

Okay, this is the part where you’re happy that you don’t have to write ANE yourself, and it seems that getting your hands on device orientation data is now only a matter of few additional build settings. Well…

This extension API, designed to be Accelerometer class twin, has two major problems in the way. Number one is that it only exposes raw gyroscope data, i.e. rotation rate. Since this is not something that flash developers do every day, you probably have no idea how to go from radians per second to orientation quaternion, or transformation matrix. So to use this ANE, you will have to learn some new tricks, such as finding approximate differential equation solutions, Taylor expansion of matrix functions, series summation and evaluating indeterminate forms. If you are still with me, good guy Oliver J. Woodman will guide you through this hell (pages 21 to 23).

Ok, now you’re done reading that and ready for action, right? Not so fast – there is problem number two. Let me just quote this comment from ANE source code:

// The singleton ExtensionContext object listens for StatusEvent events that
// the native implementation dispatches. These events contain the device’s
// gyroscope x,y,z data.
//
// However, each Gyroscope instance has its own interval timer. When the timer
// expires, the Gyroscope instance dispatches a GyroscopeEvent that contains
// the current x,y,z data.

Thank you, Adobe engineer, for this brilliant idea. Because there just can’t be enough lag or noise in my data. Sigh… so what do you do about this? You bypass ANE AS3 wrapper, and access its internal methods over low level interface. Of course, this means that you have to study both wrapper and native code beforehand :(

And here, my friend, we come to the end of our wonderful journey. I will go back to work on my AIR app, and you will maybe go and write your own ANE, with blackjack and hookers. Or you may choose to continue where I left off – maybe add Kalman filter or something. And don’t forget to write back, if you do :)

Picking random points in elliptic arc or sector

This was the result of random flashkit thread encounter and has no much real life value, probably. The closest thing I found to what that guy was asking for was this 2009 post explaining the trick to pick random point in the circle, but it is remarkably easy to extend it to the ellipse, or even arbitrary elliptic arc or sector. It is interesting in that you pick correct point on 1st try versus, for example, 4/π ≈ 1.27-th try if you simply select random point in ellipse bounding box and reject outliers – and some extra Math.xxx calls pay for that.

How to draw numbered billiard balls

This blog desperately needs more posts, but I hardly have any subjects to write at length about. And this is where wonderfl comes to the rescue – some of those codes are so complicated that one could give two hours talk about them. This code is that kind of code, and tonight I am going to explain just one small part of it – drawing numbered billiard balls. Continue reading ‘How to draw numbered billiard balls’

Projector-Camera sync code on github

As you might know, I have finally tested my idea to use my old drawTriangles() snippet for projector perspective reconstruction from webcam video. The thing is that, unless you duct-tape your webcam to projector, there is significant difference in perspective between projector and webcam that needs to be compensated in one way or another before you could use webcam to do something like this:

or

So I borrowed this projector from Denis Radin and wrote a wrapper around webcam and math and shit that is now hosted on github. There you get a sprite that waits for Ctrl+Space to show 4 white arrows for a second, and then you mark those arrows in webcam image. When you’re done, Ctrl+Space again to hide it. Using this “calibration” info, your code gets corresponding region of webcam video automatically transformed into rectangle – the way your projector would see it, if it was a webcam itself. What you do with that image next is up to you; I’ve included flashlight “painting” example above as well as basic edge highlighting example.

WTF is Transformation Matrix?

A transformation matrix transforms a vector of point coordinates in given frame of reference into coordinates in another one (e.g. from child to parent in our beloved display lists or scene graphs) – Captain Obvious.

Many people think of transformation matrices as impenetrable black boxes filled with random numbers that are impossible to make sense of while fiddling with your application. So this 1st post of the year is my attempt to help those people, by providing as short and simple explanation for those numbers as possible.

P.S. Since matrices transform vectors, you need to know a thing or two about vectors 1st. Continue reading ‘WTF is Transformation Matrix?’

Grab my balls

So, I have started cross-engine 3D primitives project at github. Just like as3dmod, it plugs seamlessly into your favorite engine to help you out whenever you feel limited by it. Initial commit supports Alternativa3D 8.12, Away3D 4 Alpha, Minko 1 and contains two spheric primitives: UniformSphere and Globe.

UniformSphere is a mesh built from the spiral of vertices uniformly distributed across the sphere. You have two options here: Rakhmanov formula (default, the spiral joins sphere poles) or Bauer formula (the spiral does not contain poles). This primitive was ported from my 3D metaballs code.

Globe is a mesh that minimizes texture distortions for classic plate carré projection. It does so by placing equilateral triangles along the equator and gradually compressing triangles towards each pole (where high triangle density is actually good property in terms of texture distortions). This primitive was ported from my globe component for FP9, which is now also hosted at github.

I plan to add few more primitives over time, both useful and not really, and maybe more engine proxies if there will be any interest.

Inverse homography using drawTriangles()

Flash player version 10+ native drawTriangles() method is perfect for programming homography (2D transformation that preserves lines) because of its support for UVT data. In general case, however, homography requires solving 8×8 linear equations system, so noone can be arsed to code that. Instead they implement one of two special cases – mapping rectangle to or from quadrilateral. So far, only 1st case was implemented using drawTriangles, while 2nd case was done using filters. Well, no longer so – it turned out, inverting T values in Zeh code was sufficient to make it work backwards:

One interesting thing about this code is that noone really knows exactly how it works :) I didn’t have to study all the math behind Zeh code in order to “invert” it, and Zeh… well, let me quote:

I was lucky enough to get what I wanted working after a good amount of crazy trial and error (and beer)


Old stuff

May 2016
M T W T F S S
« Jan    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Oh, btw…


Follow

Get every new post delivered to your Inbox.