flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.23k stars 902 forks source link

How to remove slight vibration of object #2793

Closed Nishi05 closed 1 year ago

Nishi05 commented 1 year ago

I'm sorry. this is question. I wanted to ask a question on Discord or stack overflow but, I didn't know how to express this problem in words. So I will post the video on github to share it. If you have any problems posting to github, please let me know.

I would like to eliminate the problem of objects vibrating minutely like in the video.

https://github.com/flame-engine/flame/assets/40897913/419e0e1b-5d25-4cc2-9834-2dae6171fd9f

This problem will become smaller if i reduce the gravity and the restitution of the object, but if possible, i would like to solve this problem while keeping the values of gravity and the restitution.

this is my code

import 'dart:async';

import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/services.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GameWidget(game: BoxBodyExample()),
    );
  }
}

class BoxBodyExample extends Forge2DGame {
  BoxBodyExample()
      : super(
          world: DemoBodyWorld(),
          gravity: Vector2(0, 500),
        );
}

class DemoBodyWorld extends Forge2DWorld
    with TapCallbacks, HasGameReference<Forge2DGame> {
  @override
  Future<void> onLoad() async {
    super.onLoad();
    addAll(createBoundaries(game));
  }

  @override
  void onTapDown(TapDownEvent info) {
    super.onTapDown(info);
    final position = info.localPosition;
    add(FallingBox(position));
  }
}

List<Wall> createBoundaries(Forge2DGame game, {double? strokeWidth}) {
  final visibleRect = game.camera.visibleWorldRect;
  final topLeft = visibleRect.topLeft.toVector2();
  final topRight = visibleRect.topRight.toVector2();
  final bottomRight = visibleRect.bottomRight.toVector2();
  final bottomLeft = visibleRect.bottomLeft.toVector2();

  return [
    Wall(topLeft, topRight, strokeWidth: strokeWidth),
    Wall(topRight, bottomRight, strokeWidth: strokeWidth),
    Wall(bottomLeft, bottomRight, strokeWidth: strokeWidth),
    Wall(topLeft, bottomLeft, strokeWidth: strokeWidth),
  ];
}

class Wall extends BodyComponent {
  final Vector2 start;
  final Vector2 end;
  final double strokeWidth;

  Wall(this.start, this.end, {double? strokeWidth})
      : strokeWidth = strokeWidth ?? 1;

  @override
  Body createBody() {
    final shape = EdgeShape()..set(start, end);
    final fixtureDef = FixtureDef(shape, friction: 1);
    final bodyDef = BodyDef(
      userData: this,
      position: Vector2.zero(),
    );

    return world.createBody(bodyDef)..createFixture(fixtureDef);
  }
}

class FallingBox extends BodyComponent {
  final Vector2 _position;

  FallingBox(this._position);

  @override
  Body createBody() {
    final bodyDef = BodyDef(
      userData: this,
      type: BodyType.dynamic,
      position: _position,
      bullet: true,
      allowSleep: false,
    );
    final shape = PolygonShape()..setAsBoxXY(3, 3);

    final fixtureDef = FixtureDef(
      shape,
      userData: this,
      restitution: 0.6,
      friction: 1,
      density: 100,
    );

    return world.createBody(bodyDef)..createFixture(fixtureDef);
  }
}

flutter doctor

[✓] Flutter (Channel stable, 3.13.6, on macOS 13.6 22G120 darwin-arm64, locale en-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)
[✓] Network resources

pubspec.yaml

name: flame_demo
description: A new Flutter project.

publish_to: "none"

version: 1.0.0+1

environment:
  sdk: ">=3.1.1 <4.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  dashbook: ^0.1.12
  flame: ^1.9.1
  flame_forge2d: ^0.15.0+1
  flutter_sensors: ^1.0.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0
  flame_lint: ^1.1.1

flutter:
  uses-material-design: true
  assets:
    - assets/images/
ufrshubham commented 1 year ago

GitHub issues are for reporting problems in the repository or requesting for new features. That is the reason why we have some standard templates for the issues 😅

Nishi05 commented 1 year ago

Sorry, I got everything wrong... I will ask in the appropriate place.