# Posts tagged ‘Math’

A lot of maths I use tends to be abstracted away either in libraries I use, or inside the application. I’m going to go back to basics starting with vector maths, and moving onto matrices – these, in my opinion are the back bone to doing what we do. I’ll cover from the ground up and then go into some more complex areas: determinants, inverse multiplication, decomposition etc. I’ll be learning a bunch of this stuff along the way. Lets get started:

Vectors

So a vector is basically a direction from the origin. [1, 2, 3] basically means we have a point thats moved 1 in the X direction, 2 in the Y and 3 the Z direction.

Vectors can be added together simply by adding the parts of each together. [1, 2, 3] + [4, 5, 6] = [(1+4), (2+5), (3+6)]. Subtraction follows a similar process.

Vectors can be multiplied against a scalar (float) value by multiplying each part by it: [1, 2, 3] * 5 = [(1*5), (2*5), (3*5)].

We can get the length of a vector by firstly, powering each part by 2, then summing (adding up) these parts, and finally getting the square root of the total. This looks like this len([1, 2, 3]) = sqrt((1^2) + (2^2) + (3^2)).

Using this length we can get the normal of the vector. Normalizing a vector keeps its direction, but its length becomes 1.0. This is important in finding angles, unit vectors and matrix scale. To do this we first get the vectors length, and then divide each part of the vector by it:

normal([1, 2, 3]) =

length = sqrt((1^2) + (2^2) + (3^2))

normal = [1/length, 2/length, 3/length]

The dot product of two 3d vectors (x, y, z), basically returns the magnitude of one vector projected onto another. If we have two vectors [1, 0, 0] and [1, 1, 0]; when we project the latter onto the former, the value along the formers length is the dot product. To get the dot product of two vectors we simply multiply the parts together:

[1, 2, 3] . [4, 5, 6] = (1*4) + (2 * 5) + (3 * 6)

We can use the dot product to get the angle between two vectors too. If we first normalize each vector, we can get the angle by getting the inverse cos (or acos) of this dot. This will return a radian, so we can convert it into degrees by multiplying it by (180 / pi):

cos(norm([1,2, 3] . norm([4, 5, 6]) )^-1 * (180/pi)

Next cross products..

I’ve added some updates to my research page with some links to physics simulation, collisions, parametric surfaces and linear algebra – such as find the Sagitta and in turn the center of an arc. (Important for motion capture solving)

The method ive found  to resolve spherical and circular collisions at high speed is borrowed from the spherical bounding box method. I first store the current distance between the two spheres/circles and then the  future distance between the two  .

$cd = |p0_{1} - p1_{1}|$

$fd = |(p0_{1} + (p0_{1} - p0_{1-1})) - (p1_{1} + (p1_{1} - p1_{1-1})) |$

if this future distance is smaller than the combined radius’s I re imagine the current distance

$cd = (cd \frac{cd}{fd})$

and continue to do the standard method.

The basic verlet algorithm im using is,

$x_{1+1} = x_{1} +(x_{1} - x_{1-1}) + a dt^2$

where x is the current and previous position a the accumulated forces, and dt, the timestep or period over frequency. This is great if the simulation is un-fluctuating i.e baked; if it was realtime i’d introduce the TCV (time corrected verlet) version,

$x_{1+1} = x_{1} + (x_{1} - x_{1-1}) (frac{dt_{1}}{dt_{1-1}}) + a dt_{1}^2$

The nice thing about verlet is that velocity is calculated from the current and old positions, and because of this you can keep throwing collsion and constraint methods at it. It handles the rest. My current approach is to fire the integrator ( verlet) then collisions and constraints – the latter two iterated several times.

I started to think about building a very simple framework for dynamics based on this article: http://www.gamasutra.com/resource_guide/20030121/jacobson_01.shtml

It uses the verlet method and then successive systems to check for constraints, collisions etc. – I like this approach as it seems pretty modular. I think the basic method ill go for is defining objects/verts/lines/etc with attributes, whether there masses or constraints connecting masses together. Then the main system calls the general functions on them.

Im not sure if this is correct but i think the aproach with the main system is to:

1. Calculate all the forces of all the masses
2. Apply verlet algorithm with a timeStep
3. Do a scene check i.e if the masses are in a box if its simple or/and
do some collision/friction checking
4. satisfy the constraints

Im not sure if 4 & 3 are the right order so, ill have to think on that – but this is my basic approach to building a simple system. I thought of attributes because they could be place on anything then, and i could add some function for display methods, like cylinders and spheres.

As I’m currently in the process of skinning many meshes for the current game I’m working on here are some rules I’ve learnt on the way:

• Don’t attempt to skin spherical deformation without having bones for deformation or quaternion based skinning methods. Key places where this must happen is the shoulders, thighs, elbows, knees, wrists and ankles.
• You don’t need lots of twist bones, three including the actual bone is enough e.g shoulder *deformer bone, main twist (100% to upper arm with a direction pointing to the deformer bone), 50% twist bone, 0% twist (100% to the upper arm)

*By deformer bone i mean a tweak bone/point, etc  has average deformations between the  shoulder and upper arm. Similar to elbow or wrist deformer bones.

• Don’t model the wrist/hand attached to the sleeve, tuck it inside and treat it as an element. The same applies to the ankle/foot and trouser leg.
• If you don’t see the underside of a mesh, don’t model it, cap it off. A good example of this is a skirt.
• The deformation of the wrist is not the same as the elbow or shoulder; the shoulder can be considered a one dimensional quaternion – in this i mean its twist is dictated by its direction.  The wrist could be considered two dimension as the first quaternions direction dictates the rotation space (one plane) for the second quaternion to ride on.  The wrist bone dictating the direction for the hand to ride on, as oppose to the upper arm bones direction dictating the entire deformation of the shoulder.
I’ll discuss more on the differences of the wrist compared to the shoulder in later posts.

Inconjunction with using the ‘unknown’ in this system – additional unknown could be stacked up for when combinations of combinations happen.

I’ve just started watching the Gilbert Strang MIT Courseware on iTunes, and highly recommend it – after watching it a light bulb went off in my head. Daniel Pook Kolb’s Blendshape system is very very impressive – but I did’nt and still dont understand it fully.

Thinking out loud here I thought that it might be essentially a form of finding out the ‘unknown’ in a linear combination of an nth dimensional space as the corrective of the combining shapes. For example the unknown of :

x[2,3] +  y [4,5]  = [6,7] would be [-1, 2] ( its hard to show as i’m not using LaTex atm)

Before any corrective is used the  ‘unknown’ is [0,0], this would be synomanous to  adding two blendshapes together without any corrective.

Just a quick theoretical idea – I’ve been thinking that a rotation is basically the reflection or product of a direction indirectly or directly controlled by its spin.  This seems simple to understand in theory but in practice its more complex. Im coming to terms that rotation is really an illusion of a direction or a side product.

Thinking about this more in my notes, we can hypothesize that if a rotation is really just a direction, then we can seperate it into two parts: the direction and the spin and more importantly have direction controlled by anything, namely the tangent of a spline or curve. This seems amazingly simple now, the curviture of a spline dictating the direction of a quaternion about a hemisphere. But detaching yourself to thinking this way is really hard.

In this way we can treat the wrist and spine articulation the same – the spin (or twist in the conventional sence) is just used to dictate the direction of the system, with a spine you’d use the tangent of the curve itself,  with the wrist the hand  is the direction.

So I’ve been looking into using quats to simulate joint rotation and deformation. What I’ve found out is that most joints of a human can fall into two systems: single and double quaternion systems. If a bone system doesn’t twist using its bone like the forearm then it can fall into a single quat system, which dictates just a direction , resolution of the twist happens naturally when two perpendicular axis’ come together at 90 degrees.

If a system has a twist dictated by bones in conjunction with muscle deformation, then one quaternion is needed to dictate the twist space the second ‘deformation’ quat rides on. With this we can say that twist is not only dictated by the resolution of the system it exists in, but by the spin this system dictates. The order of which system drives each space is important.