Wednesday, February 11, 2015

Caplets

What Next?

In order to make an editor for the user to create their own shapes, we needed to make some sort of object comprised of smaller ones. We decided the best way to do it was to take spheres, and connect them with tubes, like in this photo. Using a name suggested by Professor Jarek, we'll call them Caplets.

To implement it, we started with the basics: how to make a tube or "skin" between spheres? By finding the planes tangent to both spheres, we can draw triangles and fill out the space, like this.

MATH

Summoning my old high school geometry skills and Processing, I was able to build a quick test.
The secret was using spherical coordinates: By drawing some quick lines, I was able to solve for Theta, and using spherical coords means we have all the information we need.

Solving for Theta

Converting that to xyz coords gives a vector (let's call it R) that points to from the center of a sphere to the point where the tangent plane intersects. By normalizing and multiplying by the radius of a sphere, and then adding it to the center's position you get the point of intersection. By incrementing Phi you can get the next point on each sphere, and the rest is history.

There was a large snag though: the formula I used is only valid in two dimensions, or when both spheres share the same Z and Y values. To fix this, calculate the vector formed by subtracting the two sphere's centers, and use that vector as a basis to form a rotation matrix. Transforming the calculated R's of each triangle with this before adding to their respective centers adjusts for this and fixes everything. 

Implementation!

In order to implement it into UE4, I began by looking into ways to draw custom geometry like quads or triangles. After seeing a lot of disheartening forum posts saying it was either impossible due to UE4's architecture or I would need to extend their geometrical primitive class and implement it myself, I found hints of a Plug-In by Epic themselves that does this. I found it and it works like a charm.

Now, to support all the things we want it to I did a lot of architect-ing. I made an object called Caplet Collection, and a Caplet object.  The Caplet Collection has a function that allows it to add Caplets as components using another as a parent if desired, and an Edge list that keeps track of the connections/order. Every update, it iterates through the Edge list and draws the Caplets.

The final blueprint Part 1 Part 2

Next post will be me combining the work down on the caplet spheres with the sound/collision work done on the SphereBP we've been using up till now, and hopefully initial work on a working editor!

No comments:

Post a Comment