Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short explanation on how to interop with mint #789

Closed
BrettWitty opened this issue Jul 1, 2020 · 3 comments
Closed

Add short explanation on how to interop with mint #789

BrettWitty opened this issue Jul 1, 2020 · 3 comments

Comments

@BrettWitty
Copy link
Contributor

As a newcomer, the use of mint is slightly confusing. If I understand correctly, mint is a maths interoperability library so you can convert, say, nalgebra's Point2 to mint's, and maybe from mint's to cgmath's. But only as data, so you can't add two mint Vector2s together, let alone add a mint Vector2 to a nalgebra Vector2.

The solution seems to be for the library user to pick a mint-compatible library, use those types and operations they want in their code. Whenever you need to pass something to or from ggez, you use explicit .into()/.from conversions.

For example, if I wanted to grab the dest field from a DrawParam and create a new DrawParam with the dest offset by (4,4) pixels:

// dest is a mint::Point2 but I need it to be nalgebra::Point2
let d: nalgebra::Point2<f32> = drawparam.dest.into();

let delta: nalgebra::Vector2<f32> = nalgebra::Vector2<f32>::new(4.0,4.0);

// Do nalgebra's vector+point addition, but convert to mint::Point2
let new_dest: mint::Point2<f32> = (d + delta).into();

// Now DrawParam is happy.
let new_dp = DrawParam::default().dest(new_dest);

Is this correct? I feel this is what I should get out of the discussion in #344. Is it worthwhile adding a paragraph of explanation to the FAQ?

@icefoxen
Copy link
Contributor

Lots of functions, including all the setters in DrawParam, take Into<Whatever> where Whatever is the appropriate Mint type. So, you can pass them any type that implements From or Into that type, without needing to do the conversion explicitly.

Definitely worth adding to the FAQ.

@nobbele
Copy link
Member

nobbele commented May 28, 2021

Under Libraries (or Graphics and GUIs?)
Maybe like this? I am quite bad at explaining stuff

How do I use Into<mint::Point2<f32>> and other Into<mint::T> types?

mint stands for "Math INteroperability Types" which means that it provides types for other math libraries to convert to and from with. What you are supposed to do is to add a math library of your choice to your game such as glam or nalgebra, usually with a "mint" feature.
For example. You can add glam = { version = "0.15.2", features = ["mint"] } in your Cargo.toml, then when you try to pass something to say DrawParam::new().dest(my_point) you will be able to pass a glam type like DrawParam::new().dest(glam::vec2(10.0, 15.0)) to set the destination to x=10 and y=15.
Going the other way around is a bit more verbose, you need to do glam::Vec2::from(my_draw_param.dest)
Another example of moving a draw param's destination diagonally by 1 down and right.

let dest = glam::Vec2::from(my_draw_param.dest);
let new_dest = dest + glam::vec2(1.0, 1.0);
DrawParam::new().dest(new_dest)

or simply

DrawParam::new().dest(glam::Vec2::from(my_draw_param.dest) + glam::vec2(1.0, 1.0))

@nobbele
Copy link
Member

nobbele commented May 30, 2021

Added to FAQ

@nobbele nobbele closed this as completed May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants