Hey is it possible to prevent a joint moving in an axis. For example if I make a PhysicsHingeJoint that can move on the Z axis and attach something heavy it will droop on its Y axis. I am also having a similar problem if I collide the joint can translate along an axis. This example kind of shows the problem i'm having otherwise I will try and simplify my code down to something I can post to show the problem.
/*
* Copyright (c) 2009 Normen Hansen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Normen Hansen' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package application;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.FastMath;
import java.util.concurrent.Callable;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.util.GameTaskQueueManager;
import com.jmex.editors.swing.settings.GameSettingsPanel;
import com.jmex.game.StandardGame;
import com.jmex.game.state.DebugGameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.jbullet.PhysicsSpace;
import com.jmex.jbullet.collision.shapes.CollisionShape;
import com.jmex.jbullet.joints.PhysicsHingeJoint;
import com.jmex.jbullet.nodes.PhysicsNode;
/**
* This is a basic Test of jbullet-jme hinge joint motors
*
* @author normenhansen
*/
public class TestHingeJointMotor {
private static PhysicsNode hammer;
private static PhysicsHingeJoint joint;
public static void setupGame(){
// creates and initializes the PhysicsSpace
final PhysicsSpace pSpace=PhysicsSpace.getPhysicsSpace();
// add some keybindings to control the vehicle
KeyBindingManager.getKeyBindingManager().set("key_accelerate",
KeyInput.KEY_U);
KeyBindingManager.getKeyBindingManager().set("key_brake",
KeyInput.KEY_J);
KeyBindingManager.getKeyBindingManager().set("key_steer_left",
KeyInput.KEY_H);
KeyBindingManager.getKeyBindingManager().set("key_steer_right",
KeyInput.KEY_K);
KeyBindingManager.getKeyBindingManager().set("key_action",
KeyInput.KEY_SPACE);
// Create a DebugGameState
// - override the update method to update/sync physics space
DebugGameState state = new DebugGameState(){
CollisionShape shape;
@Override
public void update(float tpf) {
pSpace.update(tpf);
super.update(tpf);
if (KeyBindingManager.getKeyBindingManager().isValidCommand(
"key_accelerate", false)) {
joint.enableMotor(false, 0, 0);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand(
"key_brake", false)) {
joint.enableMotor(false, 0, 0);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand(
"key_steer_left", true)) {
joint.enableMotor(true, 40, 1);
}
else if (KeyBindingManager.getKeyBindingManager().isValidCommand(
"key_steer_right", true)) {
joint.enableMotor(true, -40, 1);
}
else if (KeyBindingManager.getKeyBindingManager().isValidCommand(
"key_action", false)) {
Sphere sphere=new Sphere("physicsobstaclemesh",8,8,0.25f);
PhysicsNode ball;
if(shape==null){
ball=new PhysicsNode(sphere, CollisionShape.ShapeTypes.SPHERE);
shape=ball.getCollisionShape();
}
else{
ball=new PhysicsNode(sphere, shape);
}
ball.setLocalTranslation(0,8,FastMath.nextRandomFloat()/100.0f);
getRootNode().attachChild(ball);
ball.updateRenderState();
pSpace.add(ball);
}
}
};
state.setText("h,k = enable motor left/right / u,j = disable motor / space = spawn ball");
Sphere sphere=new Sphere("physicsobstaclemesh",8,8,0.25f);
PhysicsNode holder=new PhysicsNode(sphere, CollisionShape.ShapeTypes.SPHERE,0);
holder.setLocalTranslation(0,1,0);
state.getRootNode().attachChild(holder);
holder.updateRenderState();
pSpace.add(holder);
Box box=new Box("physicsobstaclemesh",Vector3f.ZERO,.5f,.5f,.5f);
hammer=new PhysicsNode(box, CollisionShape.ShapeTypes.BOX);
hammer.setLocalTranslation(0,-1,0);
state.getRootNode().attachChild(hammer);
hammer.updateRenderState();
pSpace.add(hammer);
Box box2=new Box("physicsobstaclemesh",Vector3f.ZERO,1f,.5f,1f);
PhysicsNode hammer2=new PhysicsNode(box2, CollisionShape.ShapeTypes.BOX);
hammer2.setLocalTranslation(0,-1,2);
state.getRootNode().attachChild(hammer2);
hammer2.updateRenderState();
// hammer2.setMass(50);
pSpace.add(hammer2);
Box box3=new Box("physicsobstaclemesh",Vector3f.ZERO,.5f,2f,.5f);
PhysicsNode hitBox=new PhysicsNode(box3, CollisionShape.ShapeTypes.BOX);
hitBox.setLocalTranslation(-2f,-1,2);
hitBox.setMass(0);
state.getRootNode().attachChild(hitBox);
hitBox.updateRenderState();
pSpace.add(hitBox);
joint=new PhysicsHingeJoint(holder, hammer, new Vector3f(), new Vector3f(0,2,0),
Vector3f.UNIT_Z, Vector3f.UNIT_Z);
pSpace.add(joint);
PhysicsHingeJoint joint2=new PhysicsHingeJoint(hammer, hammer2, new Vector3f(), new Vector3f(0.5f,0,-2),
Vector3f.UNIT_Z, Vector3f.UNIT_Z);
joint2.setLimit(-0.0001f, 0);
pSpace.add(joint2);
// Add the gamestate to the manager
GameStateManager.getInstance().attachChild(state);
// Activate the game state
state.setActive(true);
}
public static void main(String[] args) throws Exception {
// Enable statistics gathering
System.setProperty("jme.stats", "set");
// Instantiate StandardGame
StandardGame game = new StandardGame("A Simple Test");
// Show settings screen
if (GameSettingsPanel.prompt(game.getSettings())) {
// Start StandardGame, it will block until it has initialized successfully, then return
game.start();
GameTaskQueueManager.getManager().update(new Callable<Void>() {
public Void call() throws Exception {
setupGame();
return null;
}
});
}
}
}