I have a simple project below where I have created a physics body rectangle in the centre of the screen. The following documentation states that when using the Forge2D-specific FlameGame class, Forge2DGame, you can use super(zoom: yourZoom) in the constructor to zoom into the world without changing the dimensions of the physics objects. I want to do this becausewhen I add more objects to this world, I don't want things to reach terminal velocity that quickly.
However, when I change the zoom value nothing happens. What am I missing here? I might be being rather stupid but really appreciate any help.
import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
void main() {
runApp(GameWidget(game: SimpleGame()));
}
class SimpleGame extends Forge2DGame {
SimpleGame()
: super(
gravity: Vector2(0, 10),
zoom: 100,
);
@override
Future<void> onLoad() async {
super.onLoad();
add(JarComponent(position: Vector2(200, 400), size: Vector2(40, 80)));
}
}
class JarComponent extends BodyComponent {
final Vector2 position;
final Vector2 size;
JarComponent({required this.position, required this.size});
@override
Body createBody() {
final shape = PolygonShape()
..setAsBox(size.x, size.y, position, 0);
final bodyDef = BodyDef()
..type = BodyType.static;
return world.createBody(bodyDef)..createFixtureFromShape(shape);
}
}
Turns out I was being slightly dumb. I was adding adding the components directly to the game rather than to the world which the camera is viewing. So just have to do world.add instead. Thank you Spydon for helping me out with this one :) Below full code that works:
import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
void main() {
runApp(GameWidget(game: SimpleGame()));
}
class SimpleGame extends Forge2DGame {
SimpleGame()
: super(
gravity: Vector2(0, 10),
zoom: 2,
);
@override
Future<void> onLoad() async {
super.onLoad();
// Add the component to the world for camera settings to apply
world.add(JarComponent(position: Vector2(1, 2), size: Vector2(20, 40)));
}
}
class JarComponent extends BodyComponent {
final Vector2 position;
final Vector2 size;
JarComponent({required this.position, required this.size});
@override
Body createBody() {
final shape = PolygonShape()
..setAsBox(size.x, size.y, position, 0);
final bodyDef = BodyDef()
..position = position
..type = BodyType.static;
final body = world.createBody(bodyDef);
body.createFixtureFromShape(shape);
return body;
}
}