Skip to content

Vector math

module CP #

Extended modules

CP

Methods#

#lerp(f1 : Number, f2 : Number, t : Number) : Number#

Linearly interpolate (or extrapolate) between f1 and f2 by t percent.

View source

#lerpconst(f1 : Number, f2 : Number, d : Number) : Number#

Linearly interpolate from f1 to f2 by no more than d.

View source

#v(x, y) : Vect#

Convenience function to create a Vect.

View source

#vzero : Vect#

Zero Vect.

View source

struct CP::Vect
inherits Struct #

Chipmunk's 2D vector type.

Constructors#

.angle(a : Number) : self#

Returns the unit length vector for the given angle (in radians).

View source

.lerp(v1 : Vect, v2 : Vect, t : Number) : Vect#

Linearly interpolate between v1 and v2.

View source

.lerpconst(v1 : Vect, v2 : Vect, d : Number) : Vect#

Linearly interpolate between v1 towards v2 by distance d.

View source

.new(x : Number, y : Number)#

View source

.slerp(v1 : Vect, v2 : Vect, t : Number) : Vect#

Spherical linearly interpolate between v1 and v2.

View source

.slerpconst(v1 : Vect, v2 : Vect, a : Number) : Vect#

Spherical linearly interpolate between v1 towards v2 by no more than angle a radians

View source

Methods#

#*(s : Number) : Vect#

Scalar multiplication.

View source

#+(v2 : Vect) : Vect#

Add two vectors

View source

#-(v2 : Vect) : Vect#

Subtract two vectors.

View source

#- : Vect#

Negate a vector.

View source

#==(v2 : Vect) : Bool#

Check if two vectors are equal.

(Be careful when comparing floating point numbers!)

View source

#clamp(len : Number) : Vect#

Clamp the vector to length len.

View source

#closest_point_on_segment(a : Vect, b : Vect) : Vect#

Returns the closest point on the line segment a b, to the point stored in this Vect.

View source

#cross(v2 : Vect) : Float64#

2D vector cross product analog.

The cross product of 2D vectors results in a 3D vector with only a z component. This function returns the magnitude of the z value.

View source

#dist(v2 : Vect) : Float64#

Returns the distance between this vector and v2.

View source

#distsq(v2 : Vect) : Float64#

Returns the squared distance between this vector and v2.

Faster than dist when you only need to compare distances.

View source

#dot(v2 : Vect) : Float64#

Vector dot product.

View source

#length : Float64#

Returns the length of the vector.

View source

#lengthsq : Float64#

Returns the squared length of the vector.

Faster than length when you only need to compare lengths.

View source

#near?(v2 : Vect, dist : Number) : Bool#

Returns true if the distance between this vector and v2 is less than dist.

View source

#normalize : Vect#

Returns a normalized copy of the vector (unit vector).

View source

#perp : Vect#

Returns a perpendicular vector. (90 degree rotation)

View source

#project(v2 : Vect) : Vect#

Returns the vector projection of the vector onto v2.

View source

#rotate(v2 : Vect) : Vect#

Uses complex number multiplication to rotate the vector by v2.

Scaling will occur if the vector is not a unit vector.

View source

#rperp : Vect#

Returns a perpendicular vector. (-90 degree rotation)

View source

#to_angle : Float64#

Returns the angular direction the vector is pointing in (in radians).

View source

#unrotate(v2 : Vect) : Vect#

Inverse of rotate.

View source

#x : Float64#

View source

#x=(x : Float64)#

View source

#y : Float64#

View source

#y=(y : Float64)#

View source

struct CP::BB
inherits Struct #

Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)

Constructors#

.new(left : Number = 0, bottom : Number = 0, right : Number = 0, top : Number = 0)#

View source

.new_for_circle(p : Vect, r : Number) : self#

Constructs a BB fitting a circle with the position p and radius r.

View source

.new_for_extents(c : Vect, hw : Number, hh : Number) : self#

Constructs a BB centered on a point with the given extents (half sizes).

View source

Methods#

#area : Float64#

Returns the area of the bounding box.

View source

#bottom : Float64#

View source

#bottom=(bottom : Float64)#

View source

#center : Vect#

Returns the center of a bounding box.

View source

#clamp_vect(v : Vect) : Vect#

Clamp a vector to a bounding box

View source

#contains?(other : BB) : Bool#

Returns true if the other BB lies completely within this one.

View source

#contains?(point : Vect) : Bool#

Returns true if this BB contains the point.

View source

#expand(point : Vect) : BB#

Returns the minimal bounding box that contains both this BB and the point.

View source

#intersects?(other : BB) : Bool#

Returns true if this BB intersects the other.

View source

#intersects_segment?(a : Vect, b : Vect) : Bool#

Return true if the bounding box intersects the line segment with ends a and b.

View source

#left : Float64#

View source

#left=(left : Float64)#

View source

#merge(other : BB) : BB#

Returns a bounding box that holds both bounding boxes.

View source

#offset(v : Vect) : BB#

Returns a bounding box offseted by v.

View source

#right : Float64#

View source

#right=(right : Float64)#

View source

#segment_query(a : Vect, b : Vect) : Float64#

Returns the fraction along the segment query the BB is hit.

Returns INFINITY if it doesn't hit.

View source

#top : Float64#

View source

#top=(top : Float64)#

View source

#wrap_vect(v : Vect) : Vect#

Wrap a vector to a bounding box.

View source

struct CP::Mat2x2
inherits Struct #

2x2 matrix type used for tensors and such.

(row major: [[a b][c d]])

Constructors#

.new(a : Number, b : Number, c : Number, d : Number)#

View source

Methods#

#a : Float64#

View source

#a=(a : Float64)#

View source

#b : Float64#

View source

#b=(b : Float64)#

View source

#c : Float64#

View source

#c=(c : Float64)#

View source

#d : Float64#

View source

#d=(d : Float64)#

View source

#transform(v : Vect) : Vect#

View source

struct CP::Transform
inherits Struct #

Column major 2x3 affine transform.

Constants#

IDENTITY = new#

Identity transform matrix.

Constructors#

.axial_scale(axis : Vect, pivot : Vect, scale : Number) : self#

View source

.bone_scale(v0 : Vect, v1 : Vect) : self#

View source

.new(a : Number = 1, b : Number = 0, c : Number = 0, d : Number = 1, tx : Number = 0, ty : Number = 0)#

Construct a new transform matrix.

  • (a, b) is the x basis vector.
  • (c, d) is the y basis vector.
  • (tx, ty) is the translation.
View source

.new_transpose(a : Number, c : Number, tx : Number, b : Number, d : Number, ty : Number) : self#

Construct a new transform matrix in transposed order.

View source

.ortho(bb : BB) : self#

View source

.rigid(translate : Vect, radians : Number) : Transform#

Create a rigid transformation matrix. (translation + rotation)

View source

.rigid_inverse : Transform#

Fast inverse of a rigid transformation matrix.

View source

.rotate(radians : Number) : self#

Create a rotation matrix.

View source

.scale(scale_x : Number, scale_y : Number) : self#

Create a scale matrix.

View source

.translate(translate : Vect) : self#

Create a translation matrix.

View source

Methods#

#*(t2 : Transform) : Transform#

Multiply two transformation matrices.

View source

#a : Float64#

View source

#a=(a : Float64)#

View source

#b : Float64#

View source

#b=(b : Float64)#

View source

#c : Float64#

View source

#c=(c : Float64)#

View source

#d : Float64#

View source

#d=(d : Float64)#

View source

#inverse : Transform#

Get the inverse of a transform matrix.

View source

#transform(bb : BB) : BB#

Transform a BB.

View source

#transform_point(p : Vect) : Vect#

Transform an absolute point. (i.e. a vertex)

View source

#transform_vect(v : Vect) : Vect#

Transform a vector (i.e. a normal)

View source

#tx : Float64#

View source

#tx=(tx : Float64)#

View source

#ty : Float64#

View source

#ty=(ty : Float64)#

View source

#wrap(inner : Transform) : Transform#

View source

#wrap_inverse(inner : Transform) : Transform#

View source