The Nature of Code

Randomness

Perlin noise – “Smooth Noise”
Gaussian distribution – Normalized distribution of random values.

Vectors

– An instruction that tells something where to move. Vectors have 2 properties, Magnitude & Direction.
>> Create a vector: Vector3D loc = new Vector3D(50,50); //This can be up to 3 dimensions and can be anything, accel, velocity, force, etc…
Magnitude, the length of the vector, the difference between 2 points, as described by the pythagoream theorem (could not only be distance but some other interesting variable).
>> Get magnitude of vector (distance): float d = loc.magnitude();
The main equation behind finding the magnitude is the Pythagorean Theorm:
Math.sqrt(x*x + y*y + z*z);
Normalizing a vector expresses the vector into one standard “unit vector”, this makes it easier to animate and work with over time.
>> Normalize a vector:
dir.normalize(); // Typically used when distance isn’t really the main issue but direction is.
Once normalized we can, for example set this value as an acceleration since it’s a set ‘unit vector’

– Vectors can be added, subtracted, divided or multiplied (called a scalar in multi.)
>> Multiplying a vector in the case of a force
float factor = 0.5f;
diff.mult(factor);

or
Use Shiffman’s add_force function:
Vector3D wind = new Vector3D(0.03,0.0);
t.add_force(wind);

>> Subtracting a vector, this gives us the direction:
Vector3D dir = Vector3D.sub(loc,t.getLoc());
Then use magnitude function to get the distance
float d = dir.magnitude();

Newtons Second Law
Force = Mass * Acceleration // determines how we execute force on an object.

Oscillations

– Oscillating movements are movements between 2 points charcterized by smooth easing motions, i.e. a pendulum
– These types of motions are calculated based on polar coordinate system, which has 2-dimensions, one for angle and one for distance from an origin point; rather than cartesian coordinates.

Cartesian vs. Polar coordinates
Cartesian coordinates describe a specific x,y location.
Polar coordinates desribe a location based on degrees/radians or angles rather than points.

2(PI) = 360 degrees = ~6.28… radians

– Oscillation can be described as a function of time involving amplitude, period and frequency

– All trigonometric functions in Processing require parameters to be in radians, which requires us to convert between degrees and radians.

Particles

The idea of a particle system in its simplest form is really just an animated ‘thing’ of some kind that is duplicated in a large quantity with a variety of properties.
Typically a class exists for this ‘thing’ with various properties which is then within another class ‘particle system’. The main technique to controlling a particle system is the use of an ArrayList, which is essentially a flexible array that can dynamically change its size over time.

Fractals

Fractals are self-similar shapes that can expand infinitely through a process called recursion.
The idea of factorials relates to this and ends up referencing itself over and over through an iterative process, however an exit condition must occur in order to get out of the loop.

Programming Concepts –

Encapsulation
Instance Method vs. Static Method
Inheritance – Inheriting traits of another class and then adding to them (i.e. – dog is a class that inherits from the animal class)
2D Array (an array of an array)
?Logarithms

Important Code –
Shiffman’s Vector3D.java Class

public class Vector3D {
public float x;
public float y;
public float z;

Vector3D(float x_, float y_, float z_) {
x = x_; y = y_; z = z_;
}

Vector3D(float x_, float y_) {
x = x_; y = y_; z = 0f;
}

Vector3D() {
x = 0f; y = 0f; z = 0f;
}

void setX(float x_) {
x = x_;
}

void setY(float y_) {
y = y_;
}

void setZ(float z_) {
z = z_;
}

void setXY(float x_, float y_) {
x = x_;
y = y_;
}

void setXYZ(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}

void setXYZ(Vector3D v) {
x = v.x;
y = v.y;
z = v.z;
}
public float magnitude() {
return (float) Math.sqrt(x*x + y*y + z*z);
}

public Vector3D copy() {
return new Vector3D(x,y,z);
}

public static Vector3D copy(Vector3D v) {
return new Vector3D(v.x, v.y,v.z);
}

public void add(Vector3D v) {
x += v.x;
y += v.y;
z += v.z;
}

public void sub(Vector3D v) {
x -= v.x;
y -= v.y;
z -= v.z;
}

public void mult(float n) {
x *= n;
y *= n;
z *= n;
}

public void div(float n) {
x /= n;
y /= n;
z /= n;
}

/*public float dot(Vector3D v) {
//implement DOT product
}*/

/*public Vector3D cross(Vector3D v) {
//implement CROSS product
}*/

public void normalize() {
float m = magnitude();
if (m > 0) {
div(m);
}
}

public void limit(float max) {
if (magnitude() > max) {
normalize();
mult(max);
}
}

public float heading2D() {
float angle = (float) Math.atan2(-y, x);
return -1*angle;
}

public static Vector3D add(Vector3D v1, Vector3D v2) {
Vector3D v = new Vector3D(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
return v;
}

public static Vector3D sub(Vector3D v1, Vector3D v2) {
Vector3D v = new Vector3D(v1.x – v2.x,v1.y – v2.y,v1.z – v2.z);
return v;
}

public static Vector3D div(Vector3D v1, float n) {
Vector3D v = new Vector3D(v1.x/n,v1.y/n,v1.z/n);
return v;
}

public static Vector3D mult(Vector3D v1, float n) {
Vector3D v = new Vector3D(v1.x*n,v1.y*n,v1.z*n);
return v;
}

public static float distance (Vector3D v1, Vector3D v2) {
float dx = v1.x – v2.x;
float dy = v1.y – v2.y;
float dz = v1.z – v2.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: