How to use math to rotate around the center of a ggez image instead of the top left?

Viewed 417

I'm making a game using ggez, using a camera from ggez_goodies.

This camera only has rotation from the top left of an image, but I want it from the center. Is there a good way to position the image so that it rotates around the center instead?

I'd assume you'd just change the position to fix this, what I have currently is just the position

self.image
    .draw_camera(
        camera,
        ctx,
        graphics::Point2::new(self.position.0, self.position.1),
        self.rotation,
    )
    .unwrap();

I'm guessing a fix would look sort of like this:

self.image
    .draw_camera(
        camera,
        ctx,
        graphics::Point2::new(
            self.position.0 + self.rotation_offset.0,
            self.position.1 + self.rotation_offset.1,
        ),
        self.rotation,
    )
    .unwrap();

I think it would be possible with an offset, but I can't figure out how to set the offset based off of the rotation angle.

What offset/change could I make to the position to get the image to rotate based around the center instead of around the top left corner?

1 Answers

In following example:

use ggez;
use ggez::event;
use ggez::graphics;
use ggez::graphics::{DrawMode, MeshBuilder};
use ggez::nalgebra as na;
use ggez::timer::check_update_time;
use ggez::{Context, GameResult};
use std::env;
use std::path;

struct MainState {
    rotate: f32,
    sprite_batch: graphics::spritebatch::SpriteBatch,
}

impl MainState {
    fn new(ctx: &mut Context) -> GameResult<MainState> {
        let image = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
        let batch = graphics::spritebatch::SpriteBatch::new(image);
        Ok(MainState {rotate: 0.0, sprite_batch: batch})
    }
}

impl event::EventHandler for MainState {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        while check_update_time(ctx, 30) {
            self.rotate += 0.1;
        }
        Ok(())
    }

    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        graphics::clear(ctx, graphics::BLACK);

        let mesh = MeshBuilder::new().circle(
            DrawMode::fill(),
            na::Point2::new(400.0, 300.0),
            2.0,
            2.0,
            graphics::WHITE,
        ).build(ctx)?;
        self.sprite_batch.add(
            graphics::DrawParam::new()
                .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
                .rotation(self.rotate)
                .dest(na::Point2::new(400.0, 300.0))
                .offset(na::Point2::new(0.5, 0.5)),
        );

        graphics::draw(
            ctx,
            &self.sprite_batch,
            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
        )?;
        graphics::draw(
            ctx,
            &mesh,
            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
        )?;

        self.sprite_batch.clear();
        graphics::present(ctx)?;

        Ok(())
    }
}

pub fn main() -> GameResult {
    let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
        let mut path = path::PathBuf::from(manifest_dir);
        path.push("resources");
        path
    } else {
        path::PathBuf::from("./resources")
    };

    let cb = ggez::ContextBuilder::new("oc", "bux")
        .add_resource_path(resource_dir)
        .window_mode(ggez::conf::WindowMode::default().dimensions(800.0, 600.0));
    let (ctx, event_loop) = &mut cb.build()?;

    let state = &mut MainState::new(ctx)?;
    event::run(ctx, event_loop, state)
}

Where resources/sprite_sheet.png is: enter image description here

The which do rotation from the center of the image is:

.offset(na::Point2::new(0.5, 0.5))
Related