TIC-80 icon indicating copy to clipboard operation
TIC-80 copied to clipboard

Built-in Vector library

Open BuoYancYdabl opened this issue 2 years ago • 8 comments

It would be great, if TIC had vector2, 3 and 4 libraries built in the same as math or table

BuoYancYdabl avatar Mar 06 '23 00:03 BuoYancYdabl

Are those part of Lua stdlib?

joshgoebel avatar Mar 06 '23 00:03 joshgoebel

i feel like that's huge overkill but i might be wrong

darltrash avatar Mar 06 '23 01:03 darltrash

What are these libraries? Just 2d/3d/4d vector math or?

joshgoebel avatar Mar 06 '23 01:03 joshgoebel

Are those part of Lua stdlib?

Not sure

What are these libraries? Just 2d/3d/4d vector math or?

Almost, its just regular vectors that can help to calculate different values without need to search for the vector code yourself.

BuoYancYdabl avatar Mar 06 '23 10:03 BuoYancYdabl

for python there is a built-in module linalg for tic80 that provides common 2d/3d vector operations and 2d transformations via mat3x3.

from linalg import vec2, vec3, mat3x3

Inspection below.

from typing import overload

class vec2:
    x: float
    y: float

    def __init__(self, x: float, y: float) -> None: ...
    def copy(self) -> vec2: ...
    def __add__(self, other: vec2) -> vec2: ...
    def __sub__(self, other: vec2) -> vec2: ...
    def __mul__(self, other: float) -> vec2: ...
    def __truediv__(self, other: float) -> vec2: ...
    def dot(self, other: vec2) -> float: ...
    def cross(self, other: vec2) -> float: ...
    def length(self) -> float: ...
    def length_squared(self) -> float: ...
    def normalize(self) -> vec2: ...
    def rotate(self, radians: float) -> vec2: ...

class vec3:
    x: float
    y: float
    z: float

    def __init__(self, x: float, y: float, z: float) -> None: ...
    def copy(self) -> vec3: ...
    def __add__(self, other: vec3) -> vec3: ...
    def __sub__(self, other: vec3) -> vec3: ...
    def __mul__(self, other: float) -> vec3: ...
    def __truediv__(self, other: float) -> vec3: ...
    def dot(self, other: vec3) -> float: ...
    def cross(self, other: vec3) -> float: ...
    def length(self) -> float: ...
    def length_squared(self) -> float: ...
    def normalize(self) -> vec3: ...

class mat3x3:
    _11: float
    _12: float
    _13: float
    _21: float
    _22: float
    _23: float
    _31: float
    _32: float
    _33: float

    @overload
    def __init__(self) -> None: ...
    @overload
    def __init__(self, _11, _12, _13, _21, _22, _23, _31, _32, _33) -> None: ...
    @overload
    def __init__(self, a: list[list]): ...

    def set_zeros(self) -> None: ...
    def set_ones(self) -> None: ...
    def set_identity(self) -> None: ...

    def copy(self) -> mat3x3: ...
    def __getitem__(self, index: tuple[int, int]) -> float: ...
    def __setitem__(self, index: tuple[int, int], value: float) -> None: ...
    def __add__(self, other: mat3x3) -> mat3x3: ...
    def __sub__(self, other: mat3x3) -> mat3x3: ...
    def __mul__(self, other: float) -> mat3x3: ...
    def __truediv__(self, other: float) -> mat3x3: ...
    @overload
    def __matmul__(self, other: mat3x3) -> mat3x3: ...
    @overload
    def __matmul__(self, other: vec3) -> vec3: ...
    @overload
    def matmul(self, other: mat3x3) -> mat3x3: ...
    @overload
    def matmul(self, other: vec3) -> vec3: ...
    def determinant(self) -> float: ...
    def transpose(self) -> mat3x3: ...
    def inverse(self) -> mat3x3: ...

    @staticmethod
    def zeros() -> mat3x3: ...
    @staticmethod
    def ones() -> mat3x3: ...
    @staticmethod
    def identity() -> mat3x3: ...

    # affine transformations
    @staticmethod
    def trs(t: vec2, r: float, s: vec2) -> mat3x3: ...

    def is_affine(self) -> bool: ...
    def inverse_affine(self) -> mat3x3: ...
    def matmul_affine(self, other: mat3x3) -> mat3x3: ...

    def translation(self) -> vec2: ...
    def rotation(self) -> float: ...
    def scale(self) -> vec2: ...

    def transform_point(self, p: vec2) -> vec2: ...
    def transform_vector(self, v: vec2) -> vec2: ...

blueloveTH avatar May 26 '23 08:05 blueloveTH

for python there is a built-in module

linalg is not a Python built in, you need to install NumPy to get it. Which is a huge package.

miratcan avatar Aug 08 '23 12:08 miratcan

Are those part of Lua stdlib?

No, but there is a library that can be used: https://github.com/anaef/lua-linear/tree/master

BTW I agree that most of the people implementing at least 2d vectors to create something serious.

miratcan avatar Aug 08 '23 12:08 miratcan

for python there is a built-in module

linalg is not a Python built in, you need to install NumPy to get it. Which is a huge package.

Yes, it is not a standard built-in. linalg module is only available in tic-80's python version. Check https://pocketpy.dev/modules/linalg/. Create a python project and it is available.

blueloveTH avatar Aug 08 '23 12:08 blueloveTH

@blueloveTH I'm confused, is this already implemented?

aliceisjustplaying avatar May 02 '24 16:05 aliceisjustplaying

@blueloveTH I'm confused, is this already implemented?

Yes. Available.

blueloveTH avatar May 02 '24 23:05 blueloveTH

Closing this in that case :)

aliceisjustplaying avatar May 02 '24 23:05 aliceisjustplaying