Initial Rotation of a Physics Object

I use the PhysicsNode for roads in my jme3 project. I create a Box with the scale of the road, create a new Geometry with the road’s name and the Box, attach a material to the Geometry, and use that Geometry to make a PhysicsNode. I then use setLocalTranslation() on the PhysicsNode to move the road to its position, and use setLocalRotation() to rotate the road.



I have heard that setting the mass realistically and using setKinematic(true) (because I do not want the road to move once it is initially placed and rotated) is recommended, but if I do so then the rotation seems to be ignored. No matter what, the rotation is still (0, 0, 0), although the position changes without any problem.



Not setting the kinematic to be true fixes the rotation problem, but now the road is subject to gravity and other potential forces. Even setting the gravity to zero does not solve the problem since the weight of a car pushes the road down. I do not want to use an unrealistically large mass because I feel that with plenty of roads and other physics objects there would be more unnecessary calculations done than if the object was static.



Setting the mass to zero and leaving kinematic false (or true) brings back the rotation problem.



I want to set the initial position and rotation of the road and then not have it moved in the future. Every combination of mass, kinematics, or rotation code either has the rotation problem, forces acting on the road, or the physics object not aligning with the box (I have the debug shapes enabled).



Can someone help me to get the roads properly positioned and rotated? I am using the latest build of jme3.

First of all for static objects just setting mass to zero is the recommended way. Kinematic objects should be used if you want to move the object but have it not be affected by physics itself. Now I dont exactly know why your object behaves like you describe, can you please tell me what version of jME3 you use (alpha-3 or latest nightly) and post a test case for the problem (a single class reproducing the problem).

Thanks, fixed in svn, I forgot applying the rotation to the motionstate since the physics changes.

normen said:
First of all for static objects just setting mass to zero is the recommended way. Kinematic objects should be used if you want to move the object but have it not be affected by physics itself. Now I dont exactly know why your object behaves like you describe, can you please tell me what version of jME3 you use (alpha-3 or latest nightly) and post a test case for the problem (a single class reproducing the problem).


I am using the latest nightly build of jME3 (I am using jMonkeyPlatform Alpha-2 and have enabled nightly build updates, and I am up-to-date) and I have created this test code to demonstrate the problem:
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class PhysicsRotation extends SimpleApplication {

private BulletAppState bulletAppState;

public static void main(String[] args) {
PhysicsRotation app = new PhysicsRotation();
app.start();
}

@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);

flyCam.setMoveSpeed(5f);

Material grayMat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
grayMat.setColor("m_Color", ColorRGBA.Gray);

Material redMat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
redMat.setColor("m_Color", ColorRGBA.Red);

Box b1 = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b1);
geom.setMaterial(grayMat);

PhysicsNode b1Physics = new PhysicsNode(geom, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
b1Physics.setLocalTranslation(new Vector3f(0, -1, 0));
Quaternion q1 = new Quaternion().fromAngleAxis(Float.valueOf(FastMath.PI / 2f), Vector3f.UNIT_Y);
b1Physics.setLocalRotation(q1);
b1Physics.attachDebugShape(assetManager);
b1Physics.setKinematic(true);


Box b2 = new Box(1, 1, 1);
Geometry geom2 = new Geometry("Box 2", b2);
geom2.setMaterial(redMat);

PhysicsNode b2Physics = new PhysicsNode(geom2, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
b2Physics.setLocalTranslation(new Vector3f(0, 2, 0));
Quaternion q2 = new Quaternion().fromAngleAxis(Float.valueOf(FastMath.PI / 4f), Vector3f.UNIT_Y);
b2Physics.setLocalRotation(q2);
b2Physics.attachDebugShape(assetManager);
b2Physics.setKinematic(true);

rootNode.attachChild(b1Physics);
rootNode.attachChild(b2Physics);

bulletAppState.getPhysicsSpace().add(b1Physics);
bulletAppState.getPhysicsSpace().add(b2Physics);
}

@Override
public void simpleUpdate(float tpf) {
}

@Override
public void simpleRender(RenderManager rm) {
}
}
[/java]

I have tried searching but could not find a topic relevant enough to provide any answers. The code as provided will display two cubes, unrotated (but should be) and standing still (desired). Setting the cubes to not be kinematic will correct the rotation, but they will fall as I described earlier.