## Calculating the gravity of the gravitational force or 0.00000000000667

Gravity is by far the best force in the universe. Despite being the weakest of the “main” four (strong nuclear force, weak nuclear force, electromagnetism, gravity). It is the one that dominates given enough time and distances. All this even though it falls of inversely to the square of the distance, meaning, if you double the separation between the two objects you quarter the force, or if you cut the separation in half you quadruple the force of attraction.

It’s also one of my favorite things when creating visualizations, because things move so beautifully when you simply let gravity take over.

So I thought I would give it it’s own tiny blog post, in honor of its value `0.00000000000667`

```    // Compute the net force acting between the invoking body a and b, and
// add to the net force acting on the invoking Body
Body a = this;
double G = 0.00000000000667;
double EPS = 0.0001;      // softening parameter
double dx = b.rx - a.rx;
double dy = b.ry - a.ry;
double dist = Math.sqrt(dx*dx + dy*dy);
double F = (G * a.mass * b.mass) / (dist*dist + EPS*EPS);
a.fx += F * dx / dist;
a.fy += F * dy / dist;
}```

## Concisely animating a local T value along a path

If you have a T value, in the range of [0.0-1.0] you can convert that to a local T value for each part of that curve.

```    var rangePerItem = 1.0 / items.length;
var t = 0.73;

for(var i = 0; i < items.length; i++) {
// determine which piece of the line this item falls into
var a          = i * rangePerItem;
var b          = (i + 1) * rangePerItem;

// normalize the value so that 0.51 on the third one becomes 1.0 for the first and second, and 0.2 for the third, and 0 for the 4th
var localT     = (t - a) / (b - a);
localT = clamp(localT, 0, 1);
item.update(localT);
}

```

Sometimes when developing in Swift if you’re trying to do seemingly simple things like divide 2 numbers:

``````let interval:Double = 1.0/fps;
``````

You’ll get errors like these:

``````binary operator '*' cannot be applied to operands of type 'CGFloat' and 'Double'
``````
``````binary operator '*' cannot be applied to operands of type 'Int' and 'CGFloat'
``````
``````cannot convert value of type 'Float' to expected argument type 'Double'
``````
``````binary operator '*' cannot be applied to operands of type 'CGFloat' and 'Int8'
``````

This is because Swift is more strict about how it treats numeric types, and you have to manually cast them to the same type in order for those operations to work.

However cause we can write our own operator in Swift, we can make it so that when it encounters 2 types, it will check to see if we have a matching operator and use that instead.

So as I’ve encountered them, I’ve been appending to a my little `NumericOperatorOverloadingExtensions.swift` file:

``````import Foundation

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public func +(a: Float, scalar: Int)  -> Float  { return a + Float(scalar); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////// MULTIPLICATION  //////////////////////////////////////////////////////////////
// Int *
public func *(a: Int, scalar: CGFloat)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(a: Int, scalar: Float)    -> Float    { return Float(a) * scalar; }
public func *(a: Int, scalar: Double)   -> Double   { return Double(a) * scalar; }
public func *(scalar: CGFloat, a: Int)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(scalar: Float, a: Int)    -> Float    { return Float(a) * scalar; }
public func *(scalar: Double, a: Int)   -> Double   { return Double(a) * scalar; }

// Int8 *
public func *(a: Int8, scalar: CGFloat)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(a: Int8, scalar: Float)    -> Float    { return Float(a) * scalar; }
public func *(a: Int8, scalar: Double)   -> Double   { return Double(a) * scalar; }
public func *(scalar: CGFloat, a: Int8)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(scalar: Float, a: Int8)    -> Float    { return Float(a) * scalar; }
public func *(scalar: Double, a: Int8)   -> Double   { return Double(a) * scalar; }

// Int32 *
public func *(a: Int32, scalar: CGFloat)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(a: Int32, scalar: Float)    -> Float    { return Float(a) * scalar; }
public func *(a: Int32, scalar: Double)   -> Double   { return Double(a) * scalar; }
public func *(scalar: CGFloat, a: Int32)  -> CGFloat  { return CGFloat(a) * scalar; }
public func *(scalar: Float, a: Int32)    -> Float    { return Float(a) * scalar; }
public func *(scalar: Double, a: Int32)   -> Double   { return Double(a) * scalar; }

// Float *
public func *(a: Float, scalar: Double)  -> Float  { return a * Float(scalar); }
public func *(a: Float, scalar: Double)  -> CGFloat  { return CGFloat(a * Float(scalar)); }
public func *(a: Float, scalar: Double)  -> Double  { return Double(a) * scalar; }

// CGFLOAT  *
public func *(a: CGFloat, scalar: Double)  -> CGFloat  { return a * CGFloat(scalar); }
public func *(a: CGFloat, scalar: Double)  -> Float  { return Float(a) * Float(scalar); }
public func *(a: CGFloat, scalar: Double)  -> Double  { return Double(a) * scalar; }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////// DIVISION ////////////////////////////////////////////////////////////////
// Double /
public func /(a: Double, b: Int)  -> Double  { return a / Double(b); }
public func /(a: Double, b: Float)  -> Double  { return a / Double(b); }
public func /(a: Double, b: Int)  -> Int  { return Int(a) / b; }
public func /(a: Int, b: Double)  -> Double  { return Double(a) / b; }
public func /(a: Int, b: Double)  -> Int  { return Int(Double(a) / b); }

// Float /
public func /(a: Float, b: Int)  -> Float  { return a / Float(b); }
public func /(a: Float, b: Int)  -> Int  { return Int(a) / b; }
public func /(a: Int, b: Float)  -> Float  { return Float(a) / b; }
public func /(a: Int, b: Float)  -> Int  { return Int(Float(a) / b); }
public func /(a: Float, scalar: Double)  -> Float  { return a / Float(scalar); }
``````

## Getting the angle between two 3D vectors

Another short post related to a question I was asked, that I figure might be helpful to others.
Getting the angle between two vectors in 2D is as simple as:

```var angle = Math.atan2(vectorA.y - vectorB.y, vectorA.x - vectorB.x)
```

However that does not work in 3D space, however the angle between any two vectors (2D or 3D) is defined as the
``` cosine theta = (A dot B) / Normalized-A * Normalized-B ```
Where theta is the angle between them. To find theta, we can inverse the equation:
``` theta = acos( (A dot B) / Normalized-A * Normalized-B ) ```

In code getting the angle between two vectors in 3D space translates to:

```// Make up two vectors
var vectorA = new Vector3(5, -20, -14);
var vectorB = new Vector3(-1, 3, 2);

// Store some information about them for below
var dot = vectorA.dot(vectorB);
var lengthA = vectorA.length();
var lengthB = vectorB.length();

// Now to find the angle
var theta = Math.acos( dot / (lengthA * lengthB) ); // Theta = 3.06 radians or 175.87 degrees
```

## Rotate vector / line-segment / polygon around arbitrary point

Someone asked me this earlier today, and i made this code snippet and I figured i might as well share it and save it for myself.

To rotate a vector, or for that matter a linesegment (which is just two vectors), and again a polygon (which is just n linesegments). All you have to do is first translate the points so that their origin is at zero. Using a linesegment as an example, that means finding the midpoint between A and B, which is (A + B)/2 then subtracting A and B by -midpoint. Then applying cosine/sine for the x/y components respectively.

Here is an example of that – it’s in javascript but you get the idea.

```rotateLinesegment: function(pointA, pointB, angle, center) {

// Find the mid-point between A and B - i make a copy of pointA just to be safe
if( !center ) {
var center = pointA.Copy();
center.Multiply( 0.5 );
}

var na = pointA.Copy();
na.Subtract(center);

var nb = pointB.Copy();
nb.Subtract(center);

// Rotate each component in the vector by angle
var rotateVector = function( v, angle ) {
var s = Math.sin(angle);
var c = Math.cos(angle);

var nx = c * v.x - s * v.y;
var ny = s * v.x + c * v.y;

v.x = nx;
v.y = ny;
};

// Rotate, then untranslate it back to center
rotateVector(na, angle);

// Rotate, then untranslate it back to center
rotateVector(nb, angle);

pointA = na;
pointB = nb;

return {pointA: pointA, pointB: pointB}
}
```

That’s for rotating a linesegment.

To rotate a polygon you have to make a single tiny change.
Instead of calculating the center by finding the midpoint between line AB, pass the center of the polygon.

How do you find the barycenter/centroid of the polygon. It’s actually pretty simple. The barycenter for a polygon with N points, is found by simply adding up all the points, and dividing by the number of points.
For example `pointA.x + pointB.x + pointC.x / 3 = center.x`, repeat for Y

Once you have the barycenter, pass that as the value for `center`, and rotate as shown above, and then add all points by that value. If you want to rotate around an arbitrary point in the polygon simple subtract all the points by that value instead.

```var center = new Vector2d(0,0);

// Each polygon is made up of N linesegments, each linesegment is made up of 2 points
for(var i = 0; i < polygon.linesegments.length; i++) {
center.x += polygon.linesegments[i].a.x;
center.y += polygon.linesegments[i].a.y;

center.x += polygon.linesegments[i].b.x;
center.y += polygon.linesegments[i].b.y;
}
// averaging it out, will give us the center of the polygon - (note multiplying length by 2 because we did it twice for each linesegment)
center.x /= polygon.linesegments.length*2;
center.y /= polygon.linesegments.length*2;

// Rotate each linesegment
var angle = 45 * Math.PI/180; // 45 degrees in radians
for(i = 0; i < polygon.linesegments.length; i++) {
var line = polygon.linesegments[i];
var newAB = this.rotateLine(line.a, line.b, angle, center);

line.a = newAB.a;
line.b = newAB.b;
}
```