-
-
Notifications
You must be signed in to change notification settings - Fork 559
Physics World
FXGL, like many other 2D game frameworks, uses jbox2d to deal with physics. We will only cover how to use jbox2d within FXGL, since the library has its own documentation.
In order to add an entity to the physics world, all we need to do is configure PhysicsComponent
and attach it to the entity:
PhysicsComponent physics = new PhysicsComponent();
physics.setBodyType(BodyType.DYNAMIC);
// these are direct jbox2d objects, so we don't actually introduce new API
FixtureDef fd = new FixtureDef();
fd.setDensity(0.7f);
fd.setRestitution(0.3f);
physics.setFixtureDef(fd);
Entity entity = ...
entity.addComponent(physics);
getGameWorld().addEntity(entity);
Once the entity is added to the game world, the PhysicsComponent
will be automatically picked up by the physics world.
It is important to note that an entity with such component is now managed by the physics world.
Hence we cannot directly modify components like PositionComponent
, RotationComponent
, BoundingBoxComponent
, etc.
Instead we have to modify the PhysicsComponent
.
Say you want to move a physics supported entity.
You can do that as follows:
PhysicsComponent physics = entity.getComponentUnsafe(PhysicsComponent.class);
physics.setLinearVelocity(10, 0);
This will set entity's velocity to 10
pixels per second.
You can use meters with the following call:
physics.setBodyLinearVelocity(10, 0);
Here 10
means meters.
To get a better feel for the API have a look at this sample.
It shows the basic code you are likely to need in your physics based games.