An early description of the design and operation of the Path object in Slua. Path is intended to be the primary class that supports Slua movers. A bit rough, to get something down on “paper”.
Movement in our various vehicles consists of the object having a path in some representation, such that given a distance d along that path, we can compute the position and rotation of an object at that distance along the path. We have various ways of performing that computation, depending on the representation of the path.
The Slua Path object is intended to provide that “distance to position and rotation” calculation for all the kinds of path representations we have, or will have. This article begins to describe how that happens.
A discussion of the concepts of classes and instances is out of scope for this article, but we will encounter a few along the way, so we’ll take a quick run at it.
There are things even in Linden Scripting Language (LSL) that are “objects”. A list is an object. A vector or rotation is an object. Even integers and floats are objects. In LSL, however, the object notion is quite rudimentary and weak. Slua offers much more capability, including the ability to create one’s own objects.
Most of the objects Slua provides, such as vector and rotation, do not offer methods beyond the ability to add or multiply them together with the usual arithmetic operators. But we can create objects that have methods with names, and typically we do just that. Here is a snippet of code that creates and uses a Path. We’ll visit this in more detail but for now we just want to get the flavor.
local points = { vector(1,0,0), vector(1.5,0,0), vector(2.0,0,0)}
local path = Path(points):fixed()
local pos, rot = path:at(0.75)
The first line creates an array of three points as shown.
The second defines an object path, created by the class Path, and defined to be a fixed-length path with our default distance between points, 0.5.
The third line uses the path:at(distance) method to get the position and rotation at distance 0.75 along the path. After that line has executed. pos will contain vector(1.75, 0, 0) and rot will be the identity (zero) rotation, since we are moving simply in the x direction.
So the class Path created an instance path, which responds to the method at(distance) by returning both a vector and a rotation. To move along the path we just repeatedly call path:at with increasing values of the distance parameter and move to the provided location with the provided location.
Our purpose here is to begin to describe how the Path does that, so let’s get to it:
(It is my usual practice to show variables, methods and general code snippets in code font, and when I mention classes, not to wrap them. I’ll typically say Path, not Path. Consistency, thy name is not Janet.)
As we’ll see shortly, the Path can be created given an array of points, or a new kind of thing called an LSD_Array, which refers to a set of keys and values in LinkSet Data, where the keys are increasing string indices like ‘data0001’, ‘data0002’, and the values are string vectors (or other shapes to be determined).
The central idea is that the path just thinks it has an array of points, and we go from there.
Though our paths in Lexi are mostly defined in fixed-size steps with 0.5 meters between each vector, we’ll see below that there are alternatives.
The code Path(points):fixed() creates a Path instance containing the points and sends the message fixed() to that instance. We could have provided a different length, but fixed defaults to our standard 0.5 meter spacing. For 1 meter spacing, we’d have said fixed(1.0), etc.
We also provide variable spacing, as we’ll see shortly.
Path(points) returns a instance of Path. fixed, sent to an instance of Path, informs it that the path array has fixed spacing. fixed returns the instance, so that we can type the creation on one line as shown above. We could also say this:
local path = Path(points)
path:fixed()
Same effect.
We might as well mention here that a path can be open, that is, starts somewhere and ends somewhere else, or wrapped, that is, starts somewhere, wanders around and comes back to the starting point. Perhaps we should say open and closed. But in fact paths default to open. If a path wraps around, we define it like this:
local path = Path(wrapping_points):fixed():wrap()
The best practice is to have the last point in the array be exactly equal to the first point, bringing the path back to perfect closure. If the last point falls short of the beginning, that will usually be unnoticeable. If it were to go beyond the beginning, however, you’ll see a twitch in the path because the vehicle will go past the beginning, then flip around, go back, and flip again to go forward from the beginning. So don’t do that.
It is possible to use an array of points that are not a fixed distance apart. The notation is straightforward:
local path = Path(points):variable()
If you say that, the path will examine the array and calculate all the distances between the points, so that thereafter your calls to at will move smoothly and at constant speed along the path, since the distance will be properly calculated from the actual spacing.
Think of an array of points making up our path. We’ll assume fixed 0.5 for now. We want to move a given distance along the path, let’s say 1.8. Starting at the #1 point, the path distance is zero. At the #2 point the distance is 0.5, and #3: 1.0, #4: 1.5, #5: 2.0, and so on. The distance we’ve asked for, 1.8, is between #4 and #5. We’ll need to interpolate between #4 and #5.
Getting to #4 has consumed 1.5 of the distance, so we need to get 0.3 meters more. Since #4 and #5 are 0.5 meters apart, we will interpolate 0.3/0.5 or 0.6 of the distance between them. When you finish the multiplying and adding, we’ll interpolate (array[4], array[5], 0.6) to get the position. We’ll describe how we get the rotation below, but it is similar.
But the Path object can deal with variable length paths, and paths stored in the LSD. And it can deal with wrapped and unwrapped paths. How is all this magic accomplished?
This article will spare you the details, but we do want to describe things in general terms.
The path object, given a distance, wants to access the two vectors that surround that distances, so as to interpolate between them. The process is simple:
Figure out the index in the points array of the first point past the desired distance.
Fetch that point and the prior point.
Interpolate between them.
There’s a bit of complexity in the actual code ensuring that we don’t index outside the arrays and handling the wrapping around, but the key notion is to get the two vectors that we need.
The path has a choice of helper objects that provide the index into the array, and the fraction of the way across from the preceding point that the desired distance represents. (Since the distance can be variable, we need to use the actual distance between that pair of points to decide how far to interpolate.)
The FixedPathIndex and VariablePathIndex objects do the job of getting the desired index and fraction. The Fixed one, of course, is easy. Are you looking for distance d? We’ll just divide d by the step size and return the integer and fractional parts. The Variable case is just a bit more tricky, because we have to search the table, stepping down the distance d until we find the pair that encloses d.
The LinkSet Data currently needs to be fixed-step, not because it couldn’t work, but because we don’t want to troll through it calculating distances. Unless we need it to work, in which case I’ll make sure that it does.
So to use LSD, we tell the path that it is fixed, and we provide an LSD_Array instance. LSD_Array is a simple class that you can access with subscripts:
local v = lsd[23]
And internally the code for [] converts 23 to, say, ‘data0023’, then fetches that LSD entry and casts it to vector. The code is rather arcane, in that it uses Slua’s metatable and metamethods to catch [] and convert it to the LSD read, but it is actually pretty clean and simple:
__index = function(tbl, key): any
if type(key) == 'number' then
return self.convert(ll.LinksetDataRead(self:key_for(key)))
else
return LSD_Array[key]
end
end,
The code above intercepts accesses to the array and if they are attempts to access via a number, reads the LSD and converts it. Otherwise, it is a method access and we just return the value from the class.
self.convert is currently just tovector but when we support other values, there will be other settings for it.
I’m glad you asked. They’re not fully installed yet but they’ll work like this:
Split the Bezier to a polyline, which is an array of points at varying distances.
Feed the polyline to a Path: Path(poly).variable().
Profit!
The splitting process is quick and can be done on the fly, and once it is done, it becomes a variable path. The reason for the existence of the variable path is, of course, to support this conversion.
Suppose we had the ability to create curved track and wanted to use circular arcs to describe their paths. There are at least two ways to accommodate them:
With a fixed angle step, create N points along the circle, pass them to a fixed-length path of appropriate step size. Essentially same as Bezier but faster.
Using a similar calculation inside, create an ArcPathIndex class that, given a distance d, produces an index and fraction, and an ArcArray class that given an index, returns the arc point at that index. Probably slower but might save memory.
We have a single object, Path, that is given what it believes is an array of vectors, from which it computes position and rotation given distance along the path. Supporting that object, we have FixedPathIndex and VariablePathIndex, which handle converting distance to an index into the array. We have LSD_Array, which makes the LSD look like an array of vectors.
Using these simple objects, we should be able to partition our code such that a vehicle will operate on an kind of path without changes to most of our code, ideally only to the code that initializes the Path object. As we get closer to actual use, we’ll of course refine these initial ideas to make them easier to use and more capable of handling the cases we need to handle.