Finally, here is a collection of small(ish) utility functions. None of them warrants a separate post. As usual, you can find this, and more, on my Bitbucket Repository. Read More »

## Nov, 2016

One of the more recent additions to my Utilities. I wrote this to calculate a parabolic trajectory for a thrown object (in this special case a grenade). More specifically, I calculate the required throw-velocity to accurately hit a specified point from a starting position and a given angle. This velocity can be capped and when the required speed exceeds this cap, the trajectory will not reach the target. The class can also calculate points on the trajectory in order to visualize them. Lastly, it provides a coroutine that can be used to make an object follow this trajectory. The calculation for this is done via a synchronized Leapfrog integration, and does not use Unity’s physics engine. Both this coroutine and the calculation for the trajectory-points can be specified to ‘bounce’ on surfaces, where it will take into account both the bounciness of the surface that is being hit and an optional bounciness value for the object. A bouncing object will only come to rest on a surface that is sufficiently flat.

Another datatype, or two if you want. Integer Vectors in 2D and 3D. They work nearly identically to the standard Vector2 and Vector3 types, but use integers only. I did not write these myself. The original idea was a request in the Unity3D forums and the original implementation was done there by Lysander. I only made some minor changes to it.

This time, I have a 3-by-3 matrix datatype. I innitially wrote it to generate Quaternion rotations from rotation matrices, but I extended it with most of the usual matrix operations. Little warning, though: there is no SIMD anywhere in this class, so it might not be the most performant implementation of a 3-by-3 matrix. The operations are not really heavy, but if you need every last drop of performance, this might not be the first choice.

To make use of the Bezier class from the last post, here is a Spline class. It is not the most grabage friendly class, so I might revisit it at some time, but it does what I wanted it to do when I wrote it. As I said it uses the Bezier class for smooth curves, but there is much automatisation, so no user-input is needed/possible to define the typical Bezier handles. This has the drawback that there is a risk of overshooting, if the distance between spline-vertices varies too much. Splines can be smooth or segmented, closed or open, and are visualized via a LineRenderer.

As usual, the most current code is found on my Bitbucket repository.

This time only a quicky, but it will be used in a later post. A small class to calculate a Bezier curve interpolation in 3D or 2D. Nothing special about it.

public class Bezier { public static Vector3 Interpolate(Vector3 start, Vector3 end, Vector3 controlPointA, Vector3 controlPointB, float t) { float tInv = 1 - t; float tSqr = t * t; float tInvSrq = tInv * tInv; return tInv * tInvSrq * start + 3 * t * tInvSrq * controlPointA + 3 * tInv * tSqr * controlPointB + t * tSqr * end; } public static Vector2 Interpolate(Vector2 start, Vector2 end, Vector2 controlPointA, Vector2 controlPointB, float t) { float tInv = 1 - t; float tSqr = t * t; float tInvSqr = tInv * tInv; return tInv * tInvSqr * start + 3 * t * tInvSqr * controlPointA + 3 * tInv * tSqr * controlPointB + t * tSqr * end; } public static float Length(Vector3 start, Vector3 end, Vector3 controlPointA, Vector3 controlPointB, int steps) { float length = 0; Vector3 fst = start; for (int i = 1; i <= steps; ++i) { Vector3 snd = Interpolate(start, end, controlPointA, controlPointB, (float) i / steps); length += (snd - fst).magnitude; fst = snd; } return length; } public static float Length(Vector2 start, Vector2 end, Vector2 controlPointA, Vector2 controlPointB, int steps) { float length = 0; Vector2 fst = start; for (int i = 1; i <= steps; ++i) { Vector2 snd = Interpolate(start, end, controlPointA, controlPointB, (float) i / steps); length += (snd - fst).magnitude; fst = snd; } return length; } }

Another day, another utility. This time: Ranged values. They were inspired from a Unite Europe 2016 talk on scriptable objects (and the Inpector code is copied nearly verbatim) by Richard Fine. The original had only a minimum and maximum float value with the fancy Inspector GUI, but I extended it with functionality nobody in their right mind would probably ever need. It started with a method to get a random value from within the interval and quickly escalated to two interpolation functions and tests to determine if an interval contains a given value or another interval, or if two intervals intersect. Finally I added operators to add, multiply or order intervals. And as if all that wasn’t already useless enough, I did the same thing again, with ints instead of floats and added a generic Range<T> class, that can create intervals from any orderable value-type, albeit a bit less powerfull, since adding and subtracting doesn’t necessarily have meaning outside of numbers. The inspector code also doesn’t work with the generic version.

Anyway, here is a example of how it looks in the Inspector, and a wall of code after the break.

The editor code can be found on my Bitbucket repository.

This seems to become a running theme on here, but I decided I would again at least pretend to have some content on this blog. Over the next couple of days or weeks I will post some of the Utility-Classes I wrote and use in my projects and write a bit about each. But first a couple of disclaimers: These classes are not perfect. They are prone to change without notice. The code I post here will be as it is at the time of writing, but you can get the latest versions from my Bitbucket repository.

The first utility is my generic Object Pool. I already posted it more or less in its entirety on the Unity3D forums, but here it is again. I wrote it for my current project and the motivation was to move all checks into the pool itself, so instead of returning a GameObject, where one has to then find the component of interest, it will directly return that component. Pools can be created by themselves or managed by a central static class to keep track of the pools. Each pool has an initial number of objects and a maximum size (which can be modified after creation), but it will still *always *return a valid object, even if the maximum size has been reached. Surplus-objects will not be destroyed instead of released back into the pool. That means it will (hopefully) never break any functionality, but when the maximum is reached and a new object is requested, the overhead from the pool will be added to the inherent processing required to instantiate/destroy a GameObject, so the maximum size should be chosen sensibly.