March 2012 Archives

Line / Plane Intersection in Unity3D

| | TrackBacks (0)

In a recent Unity3D game prototype I needed to determine the point of intersection between a line and plane. A player would click on the screen where they wanted their spaceship to go, and I needed to work out where that actually was in the world coordinates. The line is defined by the camera point and the point on the screen clicked (as given by camera.ScreenToWorldPoint). The plane was the spaceship (assuming no up or down movement).

If the line was intersecting an object in the scene then this could be easily achieved with Unity3D's built in functions. In this case by casting a ray from the camera and finding the objects it hits (see the Raycast help page). However, my prototype is set in space, there is no object there to hit other than a small spaceship.

First, let's work out the equation of the line from the camera (point 'c') and the mouse click (point 'm'). We can get the position of the camera with var c = camera.transform.position and the position of the mouse click with:
var s = camera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 100.0)); 
Note that in the above, the third vector parameter (the Z value) in the ScreenToWorldPoint call is the distance from the camera of the resulting point. Thus if 0 or mousePosition.z (which is also 0) is used then the resulting point is just the camera location itself. Here I have used a distance of 100 to make sure there is a good separation between the points describing the line. The general equation of a line in 3 dimensions is (for the points c & m):
[c.x + (m.x - c.x) * t, c.y + (m.y - c.y) * t, c.z + (m.z - c.z) * t]
So to find the point of intersection requires solving that simultaneously with the equation of the plane. Here is where my scenario gets easy. The plane this line intersects with in my game is the horizontal plane the spaceship is current on. That is the plane is described by:
y = spaceship.y
At some point on the line the following holds as long as the line intersects at some point:
spaceship.y = c.y + (m.y - c.y) * t
which means:
if (m.y == c.y) {
   t = 0; // no intersection
} else {
   t = (spaceship.y - c.y) / (m.y - c.y); 
}
The case m.y == c.y denotes the case where the line does not intersect the plane. What happens here is up to you. With the value of 't' known the equation for the line can be solved to give the point of intersection. Done.

La Trobe University Archaeology

| | TrackBacks (0)

Available from iTunes or from the La Trobe University Podcasts page (but they will have to be found individually as there does not appear to be a series webpage).

This is a series of talks and interviews from the La Trobe University Archaeology department. The topics vary wildly, from the traditional Ancient Egypt and Cyprus, to relatively modern digs in Melbourne city (founded in 1835). As the series is archaeologically focussed there is more emphasis on the extraction and identification of objects than history. At present there are 12 episodes - 2 are short videos (around 3 minutes) and the remainder are audio only. Two of the audio podcasts are hour long scholarly presentations, while the other 8 are interviews of between 15 to 20 minutes duration. There may be more episodes in the series as the entries so far have trickled in over the last 2 years. Production quality is high for the whole series.

There are two podcasts on archaeology in Oceania, particularly Hawaii. One is an academic presentation, while the other is an interview with the same presenter on the same topic. There is some talk on Jared Diamond's Collapse book. Diamond suggests that society on the Polynesian island Mangareva due to overforestation. However, the presenter's research suggests instead that the deforestation was caused by a lack of phosphorus in the soil due to the killing of sea birds. The discussion of Hawaii is similarly focussed on the affect of intensive cultivation on soil quality. Unsurprisingly the indigenous people started agriculture on the best land and as population grew, expanded out to less productive land. Once all arable areas were cultivated, the land became more subdivided and population growth slowed. Also there is evidence of political consolidation beginning around the same time.

Another paired presentation and interview discuss excavations at the ancient Egyptian capital Amarna. They are focussed on the industry of the time - in particular small metal work and glazed pottery. Details of the materials used are probed with synchrotrons and a bit of experimental archaeology is conducted to determine the techniques used to manufacture them.

Another episode details the search for ships lost in Vietnam by the Chinese emperor Kublai Khan at the Battle of Bach Dang. Three episodes (including both video podcasts) discuss the tools and evolution of early humans, especially the position in the evolutionary tree of some complete skeletons recently found in Sediba. There is also an interview about bronze age burials in Cyprus and other podcasts focus on the archaeological difficulties and discoveries underwater or in cities.

NavigableMap & time-based caches

| | TrackBacks (0)

NavigableMap

I often find myself writing time-based caches. Something like storing the last X hours of ticks from a data feed. Most teams I join usually have a utility class to help with such tasks. In a recent move I joined a team that didn't have such a utility, but did have a need for such a cache. Before coding, I thought I do a quick check to see if anything new had come along to help. At this point I discovered the Java 6 NavigableMap interface.

NavigableMap defines a type of sorted map with handy methods for obtaining submaps. So to get a map of the entries greater than a particular value use the tailMap method. The corresponding method for the submap less than a value is headMap. These methods return a view on the original map. So changes to the submap are reflected in the original map. The JDK provides concurrent and non-concurrent implementations of the interface.

This is very useful for a time-based cache. Create a NavigableMap sorted on the time (I used the epoch milliseconds returned by System.currentTimeMillis()) then tailMap returns the entries younger than a given time, and headMap those older. So to trim the cache of entries older than a certain time use headMap(System.currentTimeMillis() - maxAge).clear(). So easy!

Here is a complete time-based cache class for use as an example.

public class TimeSeriesCache<V> {

    private static final int MILLIS_IN_MINUTE = 60 * 1000;
    private static final int CLEAR_OLD_PERIOD_MINUTES = 1;

    private final NavigableMap<Long, V> series = new ConcurrentSkipListMap<Long, V>();
    private final Set<TickListener<K, Long, V>> subscribers;

    public TimeSeriesCache(int maxAgeMinutes) {
        if (maxAgeMinutes > 0) {
            final int maxAge = maxAgeMinutes * MILLIS_IN_MINUTE;
            Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
                    new Runnable() {
                        @SuppressWarnings("boxing")
                        @Override public void run() {
                            series.headMap(System.currentTimeMillis() - maxAge).clear();

                        }
                    }, CLEAR_OLD_PERIOD_MINUTES, CLEAR_OLD_PERIOD_MINUTES, TimeUnit.MINUTES);
        }
    }

    public Collection<V> getSeriesAfter(Long fromTimestamp) {
        return series.tailMap(fromTimestamp).values();
    }

    public void add(V value) {
        series.put(System.currentTimeMillis(), value);
    }

    public void add(Long timestamp, V value) {
        series.put(timestamp, value);
    }
}

Kuala Lumpur

| | TrackBacks (0)

I'm not sure how many people who read this blog actually know me in person - probably very few. However, for those that do, I am moving. Towards the end of the year (the exact date is still uncertain), I'm physically moving to Kuala Lumpur. Virtual location (email, website, etc) will remained unchanged. It is not yet known what I'll be doing there - there are some work visa issues to be sorted out.