In this code:
package game;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;
import com.jme3.system.AppSettings;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
public class BrickGame extends SimpleApplication implements AnimEventListener {
private BulletAppState bulletAppState;
private Material wall_mat;
private Material stone_mat;
private Material floor_mat;
private AnimChannel channel;
private AnimControl control;
private RigidBodyControl brick_phy;
private static final Box box;
private RigidBodyControl ball_phy;
private static final Sphere sphere;
private RigidBodyControl floor_phy;
private static final Box floor;
private RigidBodyControl player_phy;
private Spatial player;
private static final float brickLength = 0.48f;
private static final float brickWidth = 0.24f;
private static final float brickHeight = 0.12f;
public static void main(String[] args) {
AppSettings settings = new AppSettings(true);
settings.setRenderer(AppSettings.LWJGL_OPENGL1);
settings.setTitle("My3DGame");
settings.setResolution(800, 600);
BrickGame app = new BrickGame();
app.setShowSettings(false);
app.setSettings(settings);
app.start();
}
static {
sphere = new Sphere(32, 32, 0.4f, true, false);
sphere.setTextureMode(TextureMode.Projected);
box = new Box(Vector3f.ZERO, brickLength, brickHeight, brickWidth);
box.scaleTextureCoordinates(new Vector2f(1f, .5f));
floor = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
floor.scaleTextureCoordinates(new Vector2f(3, 6));
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
flyCam.setMoveSpeed(4);
flyCam.setZoomSpeed(8);
cam.setLocation(new Vector3f(0, 4f, 6f));
cam.lookAt(new Vector3f(2, 2, 0), Vector3f.UNIT_Y);
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(shotListener, "shoot");
inputManager.addMapping("Walk", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(animationListener, "Walk");
initMaterials();
initWall();
initFloor();
initCrossHairs();
initPlayer();
}
private ActionListener shotListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("shoot") && !keyPressed) {
makeCannonBall();
}
}
};
private ActionListener animationListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
channel.setAnim("Walk", 0.50f);
channel.setLoopMode(LoopMode.Loop);
player.move(0, 0, 5);
}
};
public void initPlayer() {
player = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
control = player.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
player_phy = new RigidBodyControl();
player.scale(0.1f);
player.setLocalTranslation(0, 0.4f, 2);
rootNode.attachChild(player);
player.addControl(player_phy);
player.addControl(control);
bulletAppState.getPhysicsSpace().add(player_phy);
}
public void initMaterials() {
wall_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
key.setGenerateMips(true);
Texture tex = assetManager.loadTexture(key);
wall_mat.setTexture("ColorMap", tex);
stone_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
key2.setGenerateMips(true);
Texture tex2 = assetManager.loadTexture(key2);
stone_mat.setTexture("ColorMap", tex2);
floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
key3.setGenerateMips(true);
Texture tex3 = assetManager.loadTexture(key3);
tex3.setWrap(WrapMode.Repeat);
floor_mat.setTexture("ColorMap", tex3);
}
public void initFloor() {
Geometry floor_geo = new Geometry("Floor", floor);
floor_geo.setMaterial(floor_mat);
floor_geo.setLocalTranslation(0, -0.1f, 0);
this.rootNode.attachChild(floor_geo);
floor_phy = new RigidBodyControl(120);
floor_geo.addControl(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy);
floor_phy.setKinematic(true); // HERE
}
public void initWall() {
float startpt = brickLength / 4;
float height = 0;
for (int j = 0; j < 15; j++) {
for (int i = 0; i < 5; i++) {
Vector3f vt =
new Vector3f(i * brickLength * 2 + startpt, brickHeight + height, 0);
makeBrick(vt);
}
startpt = -startpt;
height += 2 * brickHeight;
}
}
public void makeBrick(Vector3f loc) {
Geometry brick_geo = new Geometry("brick", box);
brick_geo.setMaterial(wall_mat);
rootNode.attachChild(brick_geo);
brick_geo.setLocalTranslation(loc);
brick_phy = new RigidBodyControl(2f);
brick_geo.addControl(brick_phy);
bulletAppState.getPhysicsSpace().add(brick_phy);
}
public void makeCannonBall() {
Geometry ball_geo = new Geometry("cannon ball", sphere);
ball_geo.setMaterial(stone_mat);
rootNode.attachChild(ball_geo);
ball_geo.setLocalTranslation(cam.getLocation());
ball_phy = new RigidBodyControl(1f);
ball_geo.addControl(ball_phy);
bulletAppState.getPhysicsSpace().add(ball_phy);
ball_phy.setLinearVelocity(cam.getDirection().mult(25));
}
protected void initCrossHairs() {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
if (animName.equals("Walk")) {
channel.setAnim("Walk", 0.50f);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(1f);
}
}
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// Not used
}
}
As you can see, the method initFloor I put the ground like a Kinematic object so that it is not affected by any force …
When I run this code, I noticed that the objects on the ground do not go into “sleep” when not moving enough.
I already make the dough the floor 0, and comment on the setKinematic line (true) the objects on the ground go into “sleep” properly …