在 ggez 库中网格坐标和绘制坐标如何相互关联?

How do mesh coordinates and draw coordinates relate to each other in ggez library?

创建直线时,会传递点列表,但 graphics::draw 需要 X/Y 坐标:

let (origin, dest) = (Point::new(0.0, 0.0), Point::new(0.0, 0.0));
let line = graphics::Mesh::new_line(ctx, &[origin, dest], 2.0, graphics::WHITE)?;
graphics::draw(ctx, &line, (Point2::new(0.0, 0.0),))?;

对于矩形,创建矩形时会传递 xywidthheight,但 graphics::draw 需要 X/Y 坐标:

let rectangle = graphics::Mesh::new_rectangle(
    ctx,
    graphics::DrawMode::fill(),
    [0.0, 0.0, 30.0, 30.0].into(),
    graphics::WHITE,
)?;
graphics::draw(ctx, &rectangle, (Point::new(0.0, 0.0),))?;

为什么需要两个坐标?

来自the author of the ggez library

The difference is whether it's in the mesh's coordinate space or in the screen's coordinate space. Sorry for the tautological answer, let me see if I can do better...

When you create the Mesh, imagine that you're drawing the points on a piece of transparent graph paper. That's the mesh coordinate system. Then when you call graphics::draw() you put that over another piece of graph paper, and the coordinates you pass to draw() is how much you offset the two by. But it's not just an offset, draw() takes options that can rotate, scale etc. the mesh's coordinate system. If you just create your mesh so that it's centered at 50,50 and then rotate it, it will by default rotate around its 0,0 point and not the center of the mesh. If you create your mesh so it's centered at its 0,0 coordinate, by default rotating it or scaling it will start from its own center. The DrawParam::offset() parameter can control where the "center" point is, but it's still kind of a pain.

So you can achieve exactly the same thing with both methods, but I'd say that draw() is better for position manipulation.