Skip to content

Is it possible to add support for rotating sprites? #90

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

Closed
vladest opened this issue May 17, 2019 · 13 comments
Closed

Is it possible to add support for rotating sprites? #90

vladest opened this issue May 17, 2019 · 13 comments

Comments

@vladest
Copy link

vladest commented May 17, 2019

Actually, more common is to add components animation support

@erickzanardo
Copy link
Member

We have this on our backlog, we will need to see prioritize that.

@erickzanardo
Copy link
Member

But, it may take a while to be done, you could do this by using the methods available on flutters's canvas

@luanpotter
Copy link
Member

Hi, @vladest, that's already achievable for children of position component, including animation component. Just update the angle variable in the update method by the dt parameter and angular speed. For example:

void update(double dt) {
    // other stuff
    angle += dt * ANG_SPEED;
}

Please let us know if you have any more questions, feel free to open issues :)

@vladest
Copy link
Author

vladest commented May 17, 2019

yes, I've done similar just subclassing SpriteComponent
the only problem: if you will not save Canvas state before call prepareCanvas(), whole scene will rotate

@erickzanardo
Copy link
Member

@vladest a couple of questions, which version of Flame are you using? Are you using the class BaseGame to handle your components?

Angle from a component should affect other components

@erickzanardo erickzanardo reopened this May 17, 2019
@vladest
Copy link
Author

vladest commented May 17, 2019

11.2
I'm subclassing fom Game class, which is subclassing BaseGame afaik, so yes

yes, thats correct, w/o canvas save/restore all the component changes angle around some point in the center of the screen (afaik)

@erickzanardo
Copy link
Member

@vladest when using BaseGame, components should not affect others, because it already restore the canvas state, check out this simples example:

import 'dart:math' as math;
import 'dart:ui';

import 'package:flame/anchor.dart';
import 'package:flame/components/component.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyGame().widget);

class Palette {
  static const PaletteEntry white = BasicPalette.white;
  static const PaletteEntry red = PaletteEntry(Color(0xFFFF0000));
  static const PaletteEntry blue = PaletteEntry(Color(0xFF0000FF));
}

class Square extends PositionComponent {
  static const SPEED = 0.25;

  final rotate;

  Square(double size, this.rotate) {
    width = height = size;
    anchor = Anchor.center;
  }

  @override
  void render(Canvas c) {
    prepareCanvas(c);

    c.drawRect(Rect.fromLTWH(0, 0, width, height), Palette.white.paint);
    c.drawRect(Rect.fromLTWH(0, 0, 3, 3), Palette.red.paint);
    c.drawRect(Rect.fromLTWH(width / 2, height / 2, 3, 3), Palette.blue.paint);
  }

  @override
  void update(double t) {
    if (rotate) {
      angle += SPEED * t;
      angle %= 2 * math.pi;
    }
  }
}

class MyGame extends BaseGame {
  MyGame() {
    final s1 = Square(64.0, true);
    s1.x = 50;
    s1.y = 150;

    final s2 = Square(64.0, false);
    s2.x = 50;
    s2.y = 250;

    add(s1);
    add(s2);
  }
}

Let me know if you still have doubts or problems.

@luanpotter
Copy link
Member

@vladest

I'm subclassing fom Game class, which is subclassing BaseGame afaik, so yes

no, that is absolutely not true. Game is the interface. BaseGame implements game. You can use the game interface, but it will do nothing for you, not even resetting the canvas. If you want to have things easy, use BaseGame and the canvas will be reset for you. If you want to use Game, please take a look at the BaseGame implementation just to be aware of what flame does for you that you will need to do yourself or be aware what things won't happen automatically.

@vladest
Copy link
Author

vladest commented May 18, 2019

that's make sense thanks. probably I was confused with naming. I'm usually use "Base" in abstract class
sorry for misunderstanding
@erickzanardo thanks for code sample

@luanpotter
Copy link
Member

@vladest if you have any more problems with rotating sprites, please let us know :)

@richikchanda1999
Copy link

Is there a way to rotate Sprites about the Z-axis?

@erickzanardo
Copy link
Member

Is there a way to rotate Sprites about the Z-axis?

Not sure what you mean by Z-axis on this case, Flame (and Flutter) don't support 3d.

@richikchanda1999
Copy link

Is there a way to rotate Sprites about the Z-axis?

Not sure what you mean by Z-axis on this case, Flame (and Flutter) don't support 3d.

I was asking about a similar implementation of Matrix4.rotationZ(radians) which can be used in combination with Transform widget, to rotate a widget by the specified radians about the Z axis.

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

4 participants