-
Notifications
You must be signed in to change notification settings - Fork 3
fisica‐en
jMe3GL2 integrates Dyn4j as a physics engine, we initialize the physical space as follows:
Dyn4jAppState<PhysicsBody2D> dyn4jAppState = new Dyn4jAppState<>(ThreadingType.PARALLEL);
stateManager.attach(dyn4jAppState);
It can start in parallel (ThreadingType.PARALLEL
) or sequentially (ThreadingType.SEQUENTIAL
). If you require the physical bodies to have better synchronization with the models, the parallel way is recommended (this way can generate costs since it synchronizes the physics engine with jme3).
Do you remember the terrain? We use this model to generate the first physical body:
...
RigidBody2D floorBody2D = new RigidBody2D(new RectangleCollisionShape(floorSprite.getWidth(), floorSprite.getHeight()));
floorBody2D.setMass(MassType.INFINITE);
floorBody2D.translate(0, -4);
/* Add the physical body to the model. */
floor.addControl(floorBody2D);
/* Add shape to physical space. */
dyn4jAppState.getPhysicsSpace().addBody(floorBody2D);
...
In this body a rectangle was used as a physical shape, each body that the engine uses is a control object for the JME Spatial
, in turn a Body
Dyn4j. This way of working makes it easier for the physical body to access itself with control of the model.
NOTE: If you want to delete or perform an action that could cause an interruption to the core JME process, it is recommended to synchronize the processes to avoid the problem.
In the same way we add a physical body to the faced cube (the player), this time using a shape generated by the GeometryUtilities
utility class:
...
RigidBody2D playerBody2D = new RigidBody2D();
BodyFixture bodyFixture = new BodyFixture(GeometryUtilities.createRectangle(sprite.getWidth(), sprite.getHeight()));
playerBody2D.addFixture(bodyFixture);
playerBody2D.setMass(MassType.NORMAL);
geom.addControl(playerBody2D);
dyn4jAppState.getPhysicsSpace().addBody(playerBody2D);
...
GeometryUtilities
is a helper class to avoid confusion with the class com.jme3.scene.Geometry
(JME3) and org.dyn4j.geometry.Geometry
(Dyn4j).
At the moment your code should have the following:
...
/* Physical space */
Dyn4jAppState<PhysicsBody2D> dyn4jAppState = new Dyn4jAppState<>(ThreadingType.PARALLEL);
stateManager.attach(dyn4jAppState);
/* Player model */
Sprite sprite = new Sprite(1, 1);
Geometry geom = new Geometry("Geom", sprite);
geom.setMaterial(MaterialUtilities.getUnshadedMaterialFromClassPath(assetManager, "Textures/blue_body_square_01.png"));
geom.setQueueBucket(RenderQueue.Bucket.Transparent);
RigidBody2D playerBody2D = new RigidBody2D();
BodyFixture bodyFixture = new BodyFixture(GeometryUtilities.createRectangle(sprite.getWidth(), sprite.getHeight()));
playerBody2D.addFixture(bodyFixture);
playerBody2D.setMass(MassType.NORMAL);
geom.addControl(playerBody2D);
dyn4jAppState.getPhysicsSpace().addBody(playerBody2D);
rootNode.attachChild(geom);
// ------------------------------------------------------------------------------------------------------------//
/* Ground. */
Sprite floorSprite = new Sprite(20, 1);
floorSprite.scaleTextureCoordinates(new Vector2f(20, 1));
Geometry floor = new Geometry("Ground", floorSprite);
floor.setMaterial(MaterialUtilities.getUnshadedMaterialFromClassPath(assetManager, "Textures/ground.png"));
RigidBody2D floorBody2D = new RigidBody2D(new RectangleCollisionShape(floorSprite.getWidth(), floorSprite.getHeight()));
floorBody2D.setMass(MassType.INFINITE);
floorBody2D.translate(0, -4);
/* Add the physical body to the model. */
floor.addControl(floorBody2D);
/* Add shape to physical space. */
dyn4jAppState.getPhysicsSpace().addBody(floorBody2D);
rootNode.attachChild(floor);
// ------------------------------------------------------------------------------------------------------------//
/* Background */
Sprite spriteBG = new Sprite(20, 10);
spriteBG.scaleTextureCoordinates(new Vector2f(2, 1));
Geometry bg = new Geometry("Background", spriteBG);
bg.setMaterial(MaterialUtilities.getUnshadedMaterialFromClassPath(assetManager, "Textures/backgroundForest.png"));
bg.move(0, 0, -1);
rootNode.attachChild(bg);
...
Up to this point you already know how to create your '2D' models without any problem, you use physics in your scenes. With joints, Dyn4j provides support for this; you just need imagination (dyn4j samples)