lcnr/crow

matrix + vector types

lcnr opened this issue · 5 comments

lcnr commented

This crate currently uses [[f32; 4]; 4] as a 4x4 Matrix and (f32, f32, f32, f32) as a 4 component vector. This is simple and should be usable with any algebra / mathematics crate.

It does however prevent matrix and vector multiplication and addition, which is kind of annoying.

I am currently considering the following posibilities:

  • keep using these simple types and add some helper methods like fn mat_mul.
  • add matrix and vector types and implement the basic operations.
  • use an existing algebra crate, while this seems like the best option, there are some disadvantages:
    • increases compilation time by ~25 % (nalgebra)
    • forces the user to use this crate, even if they want to use another one.
    • 2d pixel art games rarely need a lot of complex algebra so the additional complexity is not really worth it imo.
lcnr commented

Note: both [[f32; 4]; 4] and (f32, f32, f32, f32) is only used when dealing with colors,
as all positions are based on integers.

Vector types with methods, you mean like basic trigonometry?

lcnr commented

jup. To get a color_modulation matrix for a simple color one has to use something like this:

crow/examples/rectangles.rs

Lines 122 to 129 in 4449983

fn mat((r, g, b): (f32, f32, f32)) -> [[f32; 4]; 4] {
[
[r, 0.0, 0.0, 0.0],
[0.0, g, 0.0, 0.0],
[0.0, 0.0, b, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
}

If we added a matrix + vector type this would just be identity_mat * color_vec which is a lot nicer.
To get an image which is toned red, one can use RED multiplied with GREYSCALE
As this is only relevant when depending on color_modulation I don't want to add too much complexity + compile time for this.

Could we add nalgebra as a default, but have an opt out feature like:
crow = { version = "x", features = ["no_nalgebra"]}

lcnr commented

you can enclose code segements with ``` to prevent the default text formatting.

crow = { version = "x", features = ["no_nalgebra"]}

which makes things a lot clearer.

Afaik this does not work well. features in rust should be additive and not break code.
I would like to allow e.g. GRAYSCALE * a_very_nice_color to work without requring explicit conversions.
This prob means changing the type of GRAYSCALE to nalgebras matrix type. This means that GRAYSCALE has a different type depending on the active feature. Thereby breaking any used library which also uses these consts but don't have the same active features.

I also kind of dislike nalgebra tbh 😆 That's just personal preference though