Open AzkabanPresident opened 1 year ago
Can you share the code to reproduce this? Very odd that it only happens on android
Can you share the code to reproduce this? Very odd that it only happens on android
I found this issue when I create BodyDef set active to false.
I found this issue when I create BodyDef set active to false.
A small example just showing the bug would help a lot in debugging and fixing this 🙂
I found this issue when I create BodyDef set active to false.
A small example just showing the bug would help a lot in debugging and fixing this 🙂
Here is my code:
import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart' hide Draggable;
import 'boundaries.dart';
class TestForge2d extends StatelessWidget {
const TestForge2d({super.key});
@override
Widget build(BuildContext context) {
return GameWidget(
game: DraggableExample(),
);
}
}
class DraggableExample extends Forge2DGame with HasDraggables {
DraggableExample() : super(gravity: Vector2(0, 0));
@override
Future<void> onLoad() async {
double x = 200;
double y = canvasSize.y - 200;
Vector2 pos = screenToWorld(Vector2(x, y));
final boundaries = createBoundaries(this);
boundaries.forEach(add);
add(DraggableBall(pos, 10));
}
}
class DraggableBall extends BodyComponent with Draggable, ContactCallbacks {
final Vector2 position;
final double maxDistance;
bool isShot = false;
DraggableBall(this.position, this.maxDistance);
@override
onLoad() async {
await super.onLoad();
setColor(Colors.green);
}
@override
Body createBody() {
final shape = CircleShape();
shape.radius = 2;
final fixtureDef = FixtureDef(
shape,
restitution: 0.5,
density: 1.0,
friction: 0.4,
);
final bodyDef = BodyDef(
userData: this,
angularDamping: 0.8,
position: position,
type: BodyType.dynamic,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
@override
bool onDragUpdate(DragUpdateInfo info) {
Vector2 ballPos = info.eventPosition.game;
double distance = ballPos.distanceTo(position);
Vector2 targetPos = ballPos;
if (distance > 15) {
var rad = atan2(ballPos.y - position.y, ballPos.x - position.x);
double dx = 15 * cos(rad);
double dy = 15 * sin(rad);
targetPos = position + Vector2(dx, dy);
}
Vector2 deltaPos = ballPos - position;
double angle = deltaPos.angleTo(Vector2(0, -1));
if (deltaPos.x > 0) {
angle = angle - pi;
} else {
angle = pi - angle;
}
body.setTransform(targetPos, angle);
return true;
}
@override
bool onDragEnd(DragEndInfo info) {
body.gravityOverride = Vector2(0, 50);
Vector2 ballPos = body.position;
Vector2 deltaPos = position - ballPos;
body.applyLinearImpulse(deltaPos * 500);
return true;
}
}
@AzkabanPresident Please add your code to a code block and format it properly, it is close to unreadable now. Code blocks are three backsticks before and after the code.
I don't know if it's related, but I'm doing something very similar (bowls being pushed by a linear impulse when a drag gesture ends) and yesterday I couldn't get my head around an odd behaviour on the Android emulator. When I checked the game on a physical device, it was OK. This is the odd behaviour, checked by cleverly debugPrint'ing the linear velocity of the ball:
My conclusion is that flame_forge2d
is strongly dependent on the framerate being within a reasonable range, and when the FPS drops too low (which is often the case with the emulator) it just makes approximations that are too unreliable. This is very mildly documented as far as linearDamping
and angularDamping
are concerned, but I guess it applies to other parts of the physics engine too, and not only for unusually large values of those properties.
Reference code: https://github.com/maurovanetti/guidi-tu/blob/main/lib/games/boules/boules_module.dart
I don't know if it's related, but I'm doing something very similar (bowls being pushed by a linear impulse when a drag gesture ends) and yesterday I couldn't get my head around an odd behaviour on the Android emulator. When I checked the game on a physical device, it was OK. This is the odd behaviour, checked by cleverly debugPrint'ing the linear velocity of the ball:
I don't think this is related, since your bodies probably aren't sleeping?
* Large impulse: ball starts very quick, but after 1 second slows down. * Small impulse: ball starts much slower, but after 1 second it's got the same velocity of the fast ball, sometimes even faster.
You probably hit the maximum force per tick if the frame rate is dropping too much.
I don't think this is related, since your bodies probably aren't sleeping?
You're right. They are not.
You probably hit the maximum force per tick if the frame rate is dropping too much.
Oops. I didn't know about this limit, it can explain many such issues.
Still, in my case large impulses do cause a speed change proportional to their magnitude. It's the damping that seems off, and actually I just checked the code and the Padé approximant used is dt
-dependent: https://github.com/flame-engine/forge2d/blob/8e1b6bbcff5b373bee48d90c9e46912b29511ce2/packages/forge2d/lib/src/dynamics/island.dart#L210
Anyway, it's a different case.
@AzkabanPresident Please add your code to a code block and format it properly, it is close to unreadable now. Code blocks are three backsticks before and after the code.
I update my code , this issue still exists.Now I solve this bug by adding "body.applyForce" in update some frames,but not perfect
Current bug behavior
when first load forgegame scene,applyLinearImpulse looks like very small even though I set a very large vector2,but next trigger applyLinearImpulse will be ok. This bug is only appeared on android. Other platforms like windows and iOS are ok
Expected behavior
should same result first time and next times
Steps to reproduce
Flutter doctor output
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source! [√] Flutter (Channel stable, 3.10.5, on Microsoft Windows [版本 10.0.19045.3448], locale zh-CN) • Flutter version 3.10.5 on channel stable at D:\Program Files\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 796c8ef792 (3 months ago), 2023-06-13 15:51:02 -0700 • Engine revision 45f6e00911 • Dart version 3.0.5 • DevTools version 2.23.1 • Pub download mirror https://pub.flutter-io.cn • Flutter download mirror https://storage.flutter-io.cn
[√] Windows Version (Installed version of Windows is version 10 or higher)
[!] Android toolchain - develop for Android devices (Android SDK version 32.0.0) • Android SDK at D:\Program Files\Android\Sdk X cmdline-tools component is missing Run
path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Runflutter doctor --android-licenses
to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.[√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.6.3) • Visual Studio at D:\Program Files\Microsoft Visual Studio\2022\Community • Visual Studio Community 2022 version 17.6.33801.468 • Windows 10 SDK version 10.0.22000.0
[!] Android Studio (version 2021.2) • Android Studio at D:\Program Files\AndroidNew\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart X Unable to find bundled Java version. • Try updating or re-installing Android Studio.
[√] Android Studio (version 2022.1) • Android Studio at D:\Program Files\Android2022.1.1.21\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-9505619)
[√] Connected device (4 available) • SM P610 (mobile) • R52N804CK7J • android-arm64 • Android 10 (API 29) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [版本 10.0.19045.3448] • Chrome (web) • chrome • web-javascript • Google Chrome 114.0.5735.110 • Edge (web) • edge • web-javascript • Microsoft Edge 117.0.2045.31
[!] Network resources X A network error occurred while checking "https://github.com/": 信号灯超时时间已到
! Doctor found issues in 3 categories.
More environment information