1

When creating a line, a list of points is passed, but graphics::draw expects X/Y coordinates:

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),))?;

For rectangles, x, y, width and height are passed when creating a rectangle but graphics::draw expects X/Y coordinates:

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),))?;

Why are both coordinates needed?

  • If I had to guess, the point you pass gets used as the coordinate system for drawing the other objects (basically the x,y of the point is added to the x,y values of everything else). Have you experimented at all? – fintelia Jun 22 at 17:47
  • Yes i just did and it seems mesh xy coordinates is useful for things like rectangle centering – ducaale Jun 23 at 7:06
  • If you've found the answer to your question, you can actually post it in the answer field below so other people can benefit – fintelia Jun 23 at 17:37
0

From 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.