-
post again at issues. I would like to eliminate the problem of objects vibrating minutely like in the video. (I don't know how to express this problem in words.) RPReplay_Final1696383265.MP4This 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
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/ Any advice would be appreciated. |
Beta Was this translation helpful? Give feedback.
Answered by
spydon
Oct 10, 2023
Replies: 1 comment 3 replies
-
First set bullet to false and allowSleep to true, if that doesn't work, lower the restitution. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can wake up the bodies when you detect a tilt, I guess the tilt is changing the gravity?