Giter Site home page Giter Site logo

flame_2d_basic's Introduction

flame_2d_basic

Simple example project showcasing the basics of 2d transformations.

This game intentionally does not use the built-in Camera system from Flame; instead it has simple implementations of multiple types of coordinate transformations for basic 2d games. You can change the camera mode by pressing M.

It also includes two possible choices for physics system; top-down or platformer. Press P to switch between them.

2d Projections

These are the projections available in the game:

No camera

Just scales to screen size. Does not translate. Does not preserve aspect ratio.

    final scale = size.clone()..divide(worldSize);
    canvas.scaleVector(scale);

Or in matrix form:

    final scale = size.clone()..divide(worldSize);
    canvas.transform(
      (Matrix4.identity()..scale(scale.x, scale.y, 1)).storage,
    );

Fixed Aspect Ratio Camera

Setup:

    final scale = min(size.x / screenSize.x, size.y / screenSize.y);
    canvas.scale(scale);
    canvas.translateVector((size / scale - screenSize) / 2);
    canvas.clipRect(screenSize.toRect());

Basic version:

    canvas.translateVector(-cameraPosition + screenSize / 2);

Or in matrix form:

    canvas.transform(
      (Matrix4.identity()..translate2(-cameraPosition + screenSize / 2))
          .storage,
    );

Expand Camera

Expand to fit the canvas. Does not respect aspect ratio.

    final scale = size.clone()..divide(screenSize);

    canvas.scaleVector(scale);
    canvas.translateVector(-cameraPosition + screenSize / 2);

Or in matrix form:

    canvas.transform(
      (Matrix4.identity()
            ..scale(scale.x, scale.y, 1)
            ..translate2(-cameraPosition + screenSize / 2))
          .storage,
    );

flame_2d_basic's People

Contributors

luanpotter avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.