Physic look at problem

Hi,

what I try to do, is to implement a very simple system, that orientates an rigidbody to a given rotation.

I try to follow this one here: https://gamedev.stackexchange.com/questions/81301/what-torque-should-i-apply-in-bullet-to-maintain-a-vertical-orientation

And have this ‘solution’

My problem is now, that the rotation does not even seem to appear in the correct direction. I do not see where the error is based on the code in the stackexchange thread.

import java.util.Random;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.debug.Arrow;
import com.jme3.system.AppSettings;

public class PhysicTester extends SimpleApplication {
private static final float MASS = 1000;
private static final float SPEED_LIMIT = 10;

private BulletAppState bapp;
private Geometry geom;
boolean direct = true;
private float time;
private Quaternion targetR;

private RigidBodyControl rbdy;
private float ttime;
private Material mat;;

@Override
public void simpleInitApp() {
getFlyByCamera().setDragToRotate(true);
bapp = new BulletAppState();
getStateManager().attach(bapp);
bapp.getPhysicsSpace().setGravity(new Vector3f(0, 0, 0));
bapp.getPhysicsSpace().setMaxSubSteps(100000);
createPseudoBox();
createRealBox();
createNewTarget();
cam.setLocation(new Vector3f(10, 10, 10));
cam.lookAt(new Vector3f(0, 0, 0), new Vector3f(0, 1, 0));
}

private void createRealBox() {
Vector3f boxSize = new Vector3f(0.5f, 0.5f, 3f);
Arrow b = new Arrow(boxSize); // create cube shape
Geometry rbox = new Geometry(“Target”, b); // create cube geometry from
// the shape
mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); // create
// a
// simple
// material
mat.setColor(“Color”, ColorRGBA.Yellow);

  mat.getAdditionalRenderState().setLineWidth(2f);
  rbox.setMaterial(mat); // set the cube's material
  rbox.setQueueBucket(Bucket.Opaque);
  rootNode.attachChild(rbox); // make the cube appear in the scene
  rbdy = new RigidBodyControl();
  rbdy.setMass(MASS);
  rbox.addControl(rbdy);
  bapp.getPhysicsSpace().add(rbdy);

}

private void createPseudoBox() {
Arrow b = new Arrow(new Vector3f(0, 0, 10)); // create cube shape
geom = new Geometry(“Target”, b); // create cube geometry from the shape
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); // create
// a
// simple
// material
mat.setColor(“Color”, new ColorRGBA(255, 0, 0, 50)); // set color of
// material to
// blue
mat.getAdditionalRenderState().setLineWidth(2f);
geom.setMaterial(mat); // set the cube’s material
geom.setQueueBucket(Bucket.Translucent);

  rootNode.attachChild(geom); // make the cube appear in the scene

}

@Override
public void simpleUpdate(float tpf) {
ttime += tpf;
time += tpf;
if (time > 20) {
time = 0;
createNewTarget();
}

  geom.setLocalRotation(targetR);
  Quaternion currentRotation = rbdy.getPhysicsRotation();
  Quaternion correction =targetR.mult(currentRotation).inverse();
  float[] angles = correction.toAngles(null);
  Vector3f change = new Vector3f(-angles[0],-angles[1],-angles[2]);
  change.multLocal(10);
  rbdy.applyTorque(change);
  try {
  	Thread.sleep(10);
  } catch (InterruptedException e) {
  	e.printStackTrace();
  }

}

private void createNewTarget() {
Random r = new Random();
Vector3f target = new Vector3f(r.nextFloat() - 0.5f, r.nextFloat() - 0.5f, r.nextFloat() - 0.5f);
Quaternion quat = new Quaternion();
quat.lookAt(target, new Vector3f(0, 1, 0));
targetR = quat;
}

public static void main(String[] args) {
PhysicTester pt = new PhysicTester();
pt.setShowSettings(false);
pt.settings = new AppSettings(true);
pt.settings.setRenderer(AppSettings.LWJGL_OPENGL3);
pt.start();
}
}

1 Like