📓

SVector

public class SVector 
{
    public sfloat x, y, z;
//...

A vector in math represents direction and magnitude in one. In games we might use vectors for positons, velocity, acceleration or other and usually come with a bunch of useful functions to work on them. The way we’re representing vectors in this project is with x,y,z values rather then angle and distance. This is predominately for 2D projects, but it does help having vectors work in 3D space regardless.

This class uses sfloat to maintain mathematical determinism between platforms, which is why I needed to reinvent the wheel here rather then use Unity’s Vector classes.

NOTE: this class is incomplete, I(or you) need to make Dot and Cross Product functions to feel like we’re done here but the example didn’t need them so I didn’t write them in. Yet.

Methods

public SVector()

Constructor that builds a vector with 0,0,0 values

public SVector(Vector2 vec)

Constructor taking a Unity Vector2 and converts the x,y values over to this SVector. Fills z in as 0

public SVector(Vector3 vec)

Constructor taking a Unity Vector3 and converts the x,y,z values over to this SVector

public SVector(float x, float y, float z)

A constructor that takes 3 float values, x,y,z which it converts to sfloats and sets to the x,y,z attributes of the class respectively

public SVector(SVector original)

Constructor that copies the x,y,z values from another SVector

public Vector3 VecProp

A property so we can use it as a getter for getting a Unity Vector3(converts x,y,z values to floats) or setting using a Unity Vector3(sets class x,y,z by converting Vector3 x,y,z to sfloats)

public SVector Copy()

Returns a copy of this vector so you can do whatever math with it and not affect the original

public Vector3 ToVector()

Returns a converted SVector as a Unity Vector3

public sfloat Magnitude()

Returns the magnitude of this vector (currently only doing it for x,y values. If you need z factored in too, you’ll need to update the method)

public SVector Normalised()

Returns a Normalised version of this SVector. Normalised vectors have each of their components, xyz, with values between -1 and 1 and this is achieved by dividing each component by magnitude

public void RoundToZeroIfCloseEnough()

Calls the sfloat.RoundToZeroIfCloseEnough on each component, x,y,z which will round when close to zero. Useful to ensure physics eventually stop moving when close enough to being no movement

public virtual void FixAnyNaNValues()

NaN means Not a Number and sometimes certain actions on a float will put it into a NaN state. Trying to do more math when the value is NaN can crash the program. For example if x = NaN and we want to add 1 to it, we can’t. So just to be safe we set any NaN’s on our x,y,z values to zero if they crop up. Probably better the simulation does something a bit weird for a moment then crash the game. If you are trying to isolate a problem however, it might be worth commenting out any calls to this method in the project until you do.

public static SVector operator +(SVector b, SVector c)

Allows us to add 2 SVectors together and get a new SVector as a result. e.g a = b+c

public static SVector operator -(SVector b, SVector c)

Allows us to subtract a SVector from another SVector and get a new SVector as a result. e.g a =b-c

public static SVector operator *(SVector a, sfloat b)

Allows us to multiply a SVector by a sfloat and get a new SVector as a result. e.g c = a*15

public static SVector operator *( sfloat b, SVector a)

Same as above but multiplying the sfloat first e.g c = 15*a

public static SVector operator /(SVector a, sfloat b)

Allows us to divide a SVector by a sfloat and get a new SVector as a result. e.g c = a/15

public static sfloat Distance(SVector a, SVector b)

Return the distance between 2 SVectors

public static SVector Lerp(SVector a, SVector b, sfloat t)

Simple lerp function takes starting value(a) and end value(b) and time(t, values 0-1) and returns a new SVector with a value between vectors a and b(inclusive) using t. e.g if t is 0.7, then the value it returns is 70% of the way from vector a to vector b.

public static SVector Clamp(SVector value, SVector min, SVector max)

Pass a SVector in and have it clamped between the min and max vectors(inclusive), returned as a new SVector. This means if say value.x is greater then max.x, then value.x gets set to max.x