Struct b2Vec2
Box2D's 2-D column vector, float32, the type every position, velocity, force and vertex travels in, and the value
pybox2d hands Python as b2Vec2. Arithmetic on it is float32, as in pybox2d, whose vector operators run in
C++: v * 2.5 rounds 2.5 to float32 first and multiplies in float32.
public struct b2Vec2
- Inherited Members
- Extension Methods
Remarks
Footgun: reading x and y into Python-style double arithmetic is exact, but multiplying
two components in C# (v.x * v.y) stays float32, where Gymnasium's Python computes a float64 product. Write
(double)v.x * v.y wherever the Python operands are plain floats.
Reference: Box2D/Common/b2Math.h.
Constructors
b2Vec2(float, float)
A vector of the given coordinates.
public b2Vec2(float x, float y)
Parameters
Fields
x
The x coordinate.
public float x
Field Value
y
The y coordinate.
public float y
Field Value
Properties
this[int]
public float this[int i] { readonly get; set; }
Parameters
iint0 or 1.
Property Value
- float
The coordinate.
Exceptions
- IndexError
Thrown for any other index, with pybox2d's message
Index must be in (0,1).
length
The length (pybox2d's length property).
public readonly float length { get; }
Property Value
lengthSquared
The squared length (pybox2d's lengthSquared).
public readonly float lengthSquared { get; }
Property Value
skew
The skew vector (-y, x) (pybox2d's skew).
public readonly b2Vec2 skew { get; }
Property Value
tuple
The coordinates as a tuple of Python floats (pybox2d's tuple); each float32 widens exactly.
public readonly (double x, double y) tuple { get; }
Property Value
valid
Whether both coordinates are finite (pybox2d's valid).
public readonly bool valid { get; }
Property Value
Methods
Deconstruct(out float, out float)
Unpacks the coordinates, as Python's x, y = vec iterates a b2Vec2.
public readonly void Deconstruct(out float x, out float y)
Parameters
Equals(object?)
Equality with another boxed vector, by operator ==(b2Vec2, b2Vec2).
public override readonly bool Equals(object? obj)
Parameters
objobjectThe other object.
Returns
GetHashCode()
A hash of both coordinates' bits.
public override readonly int GetHashCode()
Returns
- int
The hash code.
IsValid()
Whether both coordinates are finite (not NaN or infinite).
public readonly bool IsValid()
Returns
- bool
True for a finite vector.
Length()
The vector's length, sqrtf(x * x + y * y).
public readonly float Length()
Returns
- float
The length.
LengthSquared()
The squared length, cheaper than Length() when only comparisons are needed.
public readonly float LengthSquared()
Returns
- float
x * x + y * y.
Normalize()
Converts this vector into a unit vector in place; a vector shorter than FLT_EPSILON is left as it is.
public float Normalize()
Returns
- float
The length before normalizing, or 0 for a vector too short to normalize.
Set(float, float)
Sets this vector to some specified coordinates.
public void Set(float x_, float y_)
Parameters
SetZero()
Sets this vector to all zeros.
public void SetZero()
Skew()
The skew vector, (-y, x), such that dot(skew_vec, other) == cross(vec, other).
public readonly b2Vec2 Skew()
Returns
- b2Vec2
The skew vector.
ToString()
pybox2d's repr: b2Vec2(%g,%g).
public override readonly string ToString()
Returns
- string
The text.
copy()
A copy (pybox2d's copy); C# structs copy on assignment, so this exists for readability.
public readonly b2Vec2 copy()
Returns
- b2Vec2
The same coordinates.
cross(b2Vec2)
The 2-D cross product with another vector, in float32 (pybox2d's cross).
public readonly float cross(b2Vec2 other)
Parameters
otherb2Vec2The other vector.
Returns
- float
x * other.y - y * other.x.
cross(double)
The cross product with a scalar, a vector (pybox2d's cross(float)).
public readonly b2Vec2 cross(double s)
Parameters
sdoubleThe scalar, rounded to float32 as pybox2d's argument conversion does.
Returns
- b2Vec2
(s * y, -s * x).
Exceptions
- OverflowError
Thrown when
slies beyond the float32 range.
dot(b2Vec2)
The dot product with another vector, in float32 (pybox2d's dot).
public readonly float dot(b2Vec2 other)
Parameters
otherb2Vec2The other vector.
Returns
- float
x * other.x + y * other.y.
Operators
operator +(b2Vec2, b2Vec2)
Adds two vectors coordinate by coordinate in float32.
public static b2Vec2 operator +(b2Vec2 a, b2Vec2 b)
Parameters
Returns
- b2Vec2
(a.x + b.x, a.y + b.y).
operator /(b2Vec2, double)
Divides by a Python float, as pybox2d's truediv: the divisor rounds to float32 first.
public static b2Vec2 operator /(b2Vec2 a, double s)
Parameters
Returns
- b2Vec2
(a.x / (float)s, a.y / (float)s).
Exceptions
- OverflowError
Thrown when
slies beyond the float32 range.
operator /(b2Vec2, float)
Divides a vector by a scalar in float32 (pybox2d's truediv).
public static b2Vec2 operator /(b2Vec2 a, float s)
Parameters
Returns
- b2Vec2
(a.x / s, a.y / s).
operator ==(b2Vec2, b2Vec2)
Exact coordinate equality, as C++'s operator == and pybox2d's eq (NaN never equals).
public static bool operator ==(b2Vec2 a, b2Vec2 b)
Parameters
Returns
- bool
True when both coordinates compare equal.
implicit operator b2Vec2((double x, double y))
A Python sequence of two numbers becoming a vector, as pybox2d's typemap converts a tuple argument: each element is rounded to float32.
public static implicit operator b2Vec2((double x, double y) value)
Parameters
Returns
- b2Vec2
The vector.
Exceptions
- TypeError
Thrown for an element beyond the float32 range (an infinity included), with pybox2d's message
Converting from sequence to b2Vec2, expected int/float arguments index 0(or index 1).
operator !=(b2Vec2, b2Vec2)
The negation of operator ==(b2Vec2, b2Vec2).
public static bool operator !=(b2Vec2 a, b2Vec2 b)
Parameters
Returns
- bool
True when a coordinate differs (or either is NaN).
operator *(b2Vec2, double)
Scales by a Python float, as pybox2d's mul: the scalar rounds to float32, then float32 products.
public static b2Vec2 operator *(b2Vec2 a, double s)
Parameters
Returns
- b2Vec2
(a.x * (float)s, a.y * (float)s).
Exceptions
- OverflowError
Thrown when
slies beyond the float32 range.
operator *(b2Vec2, float)
Scales a vector in float32 (pybox2d's mul, and C++'s *=).
public static b2Vec2 operator *(b2Vec2 a, float s)
Parameters
Returns
- b2Vec2
(a.x * s, a.y * s), the same bits ass * a.
operator *(double, b2Vec2)
Scales by a Python float from the left, as pybox2d's rmul (same bits as a * s).
public static b2Vec2 operator *(double s, b2Vec2 a)
Parameters
Returns
- b2Vec2
(a.x * (float)s, a.y * (float)s).
Exceptions
- OverflowError
Thrown when
slies beyond the float32 range.
operator *(float, b2Vec2)
Scales a vector in float32.
public static b2Vec2 operator *(float s, b2Vec2 a)
Parameters
Returns
- b2Vec2
(s * a.x, s * a.y).
operator -(b2Vec2, b2Vec2)
Subtracts two vectors coordinate by coordinate in float32.
public static b2Vec2 operator -(b2Vec2 a, b2Vec2 b)
Parameters
Returns
- b2Vec2
(a.x - b.x, a.y - b.y).
operator -(b2Vec2)
Negates both coordinates.
public static b2Vec2 operator -(b2Vec2 v)
Parameters
vb2Vec2The vector.
Returns
- b2Vec2
(-x, -y).