Hello everyone =)
I have a problem regarding physics.
The basic idea:
You control a ship that flys along a straight track, and dodge incoming boxes.
I’m currently working on the track… or rather just the ground.
As the player moves further down the z-axis, visual ground-tiles are beeing appended
to the track. However, to prevent the player from falling trough the track, i’m putting an invisible
RigidBodyControl with a BoxCollisionShape ans with 0 mass and 0 friction under the ship.
in the update-loop, im constantly setting the z-position of the rigidbody to the player’s z-position.
The Problem:
After a secound or so, the rigidBody falls asleep (turns blue in debug mode)
and refuses to collide with the ship, causing it to fall through the ground.
I’m calling .activate(); on the ground-rigidbody every update… but no luck =/
InGameAppState.java:
[java]
package com.phzero.cuberacer;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.InputManager;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
*
-
@author robin
*/
public class InGameAppState extends AbstractAppState{private Main app;
private BulletAppState bulletPhysics;
private PlayerControll playerControll;
private RigidBodyControl ground;
private int distance;
private Node groundNode;@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);this.app = (Main) app; bulletPhysics = new BulletAppState(); stateManager.attach(bulletPhysics); bulletPhysics.setDebugEnabled(true); ground = new RigidBodyControl(new BoxCollisionShape(new Vector3f(30, 1, 30)), 0); ground.setFriction(0); physicsSpace().add(ground); ground.setPhysicsLocation(new Vector3f(0, -2, 0)); groundNode = new Node(); Spatial player = this.app.getPlayerModel(); playerControll = new PlayerControll(this, app.getCamera()); player.addControl(playerControll); DirectionalLight sun = new DirectionalLight(); sun.setColor(ColorRGBA.White); sun.setDirection(Vector3f.UNIT_XYZ.negate()); this.app.getRootNode().attachChild(player); this.app.getRootNode().attachChild(groundNode); this.app.getRootNode().addLight(sun); initKeys();
}
private void initKeys(){
InputManager input = app.getInputManager();
input.addMapping(“jump”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
input.addMapping(“right”, new MouseAxisTrigger(MouseInput.AXIS_X, false));
input.addMapping(“left”, new MouseAxisTrigger(MouseInput.AXIS_X, true));input.addListener(actionListener, "jump"); input.addListener(analogListener, "right", "left");
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals(“jump”)){
playerControll.jump();
}
}
};private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if(name.equals(“right”)){
playerControll.steer(value * 3000);
}else if(name.equals(“left”)){
playerControll.steer(value * -3000);
}
}
};@Override
public void update(float tpf) {
ground.setPhysicsLocation(new Vector3f(ground.getPhysicsLocation()).setZ(playerControll.rigidBody().getPhysicsLocation().z));
ground.activate();while(playerControll.getSpatial().getLocalTranslation().z > (distance * 60) - 300){ Spatial floor = app.getGroundModel(); groundNode.attachChild(floor); floor.setLocalTranslation(Main.r.nextInt(20), -2, distance*60); distance++; } for(Spatial groundTile: groundNode.getChildren()){ if(groundTile.getLocalTranslation().z < playerControll.getSpatial().getLocalTranslation().z - 100){ groundNode.detachChild(groundTile); } }
}
public PhysicsSpace physicsSpace(){
return bulletPhysics.getPhysicsSpace();
}
}
[/java]
PlayerControll.java:
[java]
package com.phzero.cuberacer;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;
/**
*
-
@author robin
*/
public class PlayerControll extends AbstractControl {private static final Vector3f camPos = new Vector3f(0, 10, -30);
private static final int windowWidth = 2330;private InGameAppState appState;
private float targetPos;
private Camera cam;public PlayerControll(InGameAppState appSate, Camera cam){
this.appState = appSate;
this.cam = cam;
cam.lookAtDirection(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
targetPos = 0;
}@Override
public void setSpatial(Spatial spatial){
super.setSpatial(spatial);RigidBodyControl rigidBody = new RigidBodyControl(1); this.spatial.addControl(rigidBody); appState.physicsSpace().add(rigidBody);
}
public void steer(float value){
targetPos += value;
}public void jump(){
rigidBody().applyImpulse(Vector3f.UNIT_Y.mult(4f), Vector3f.ZERO);
}protected RigidBodyControl rigidBody() {
return spatial.getControl(RigidBodyControl.class);
}@Override
protected void controlUpdate(float tpf) {
cam.setLocation(cam.getLocation().add(spatial.getLocalTranslation().add(camPos).subtract(cam.getLocation()).divide(30)));
rigidBody().applyImpulse(new Vector3f(((((targetPos-(windowWidth/2)) / -50) - spatial.getLocalTranslation().x) - (rigidBody().getLinearVelocity().x / 8)) * 1.7f, 0, 0), Vector3f.ZERO);
if(rigidBody().getLinearVelocity().z < 40){
rigidBody().applyImpulse(Vector3f.UNIT_Z.divide(50), Vector3f.ZERO);
}
}@Override
public Control cloneForSpatial(Spatial spatial) {
PlayerControll control = new PlayerControll(appState, cam);
//TODO: copy parameters to new Control
return control;
}@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule in = im.getCapsule(this);
//TODO: load properties of this Control, e.g.
//this.value = in.readFloat(“name”, defaultValue);
}@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule out = ex.getCapsule(this);
//TODO: save properties of this Control, e.g.
//out.write(this.value, “name”, defaultValue);
}@Override
protected void controlRender(RenderManager rm, ViewPort vp) {}
}
[/java]
Thanks in advance =)