Hi,
I would like to create a game in which spin a wheel with click. I tried with customcontrol and in update(), i had control speed but i don’t have degs of rotation so I have tried to use joint… It’s very hard for me.
I have copied and modified a program with only roll rotation.
but with this solution, my hammer node (dynamic box) don’t rotate of right degs. i don’t undestoond this behavior. Can you halp me pls
Other little question why picture don’t overlap boxshape but it stay on left low corner?? :S
thanks for attention
package mygame;
import java.util.logging.Logger;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.joints.SixDofJoint;
import com.jme3.bullet.joints.motors.RotationalLimitMotor;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import static com.jme3.math.FastMath.PI;
import static com.jme3.math.FastMath.TWO_PI;
import static com.jme3.math.FastMath.abs;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import com.jme3.ui.Picture;
import java.util.Arrays;
public class TestRot extends SimpleApplication implements ActionListener {
private static final Logger logger =
Logger.getLogger(TestRot.class.getName());
protected BulletAppState bulletAppState;
protected Geometry desiredArrow;
protected Geometry distanceArrow;
// current velocity for every axis
protected float desiredRollVelocity = 0;
/**
* Angles during last frame. Used for calculating velocity
*/
protected final float[] lastFrameAngles = {0, 0, 0};
// PD controller gain values
protected final float proportionalGain = 1;
protected final float derivativeGain = 1;
protected boolean motorEnabled = true;
protected SixDofJoint joint;
protected RotationalLimitMotor rollMotor;
private int i;
private boolean somma = true;
private boolean first = true;
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
setupKeys();
setupJoint();
setupArrows();
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
flyCam.setEnabled(false);
}
private void setupKeys() {
inputManager.addMapping("RollPlus", new KeyTrigger(KeyInput.KEY_L));
inputManager.addMapping("RollMinus", new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_R));
inputManager.addMapping("toggleEnableMotors", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "RollMinus", "RollPlus", "PitchMinus", "PitchPlus", "YawMinus", "YawPlus", "Reset", "toggleEnableMotors");
}
private void setupJoint() {
// Static nodes have zero mass
Node holderNode =
createPhysicsTestNode(new BoxCollisionShape(new Vector3f(.1f, .1f, .1f)), 0);
holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0, -2, 0));
rootNode.attachChild(holderNode);
bulletAppState.getPhysicsSpace().add(holderNode);
// dynamic node with mass of 1
Node weightNode =
createPhysicsTestNode2(new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 1);
RigidBodyControl weightNodeControl = weightNode.getControl(RigidBodyControl.class);
weightNodeControl.setPhysicsLocation(new Vector3f(0, -2, 1));
rootNode.attachChild(weightNode);
bulletAppState.getPhysicsSpace().add(weightNode);
// Joint with six degrees of freedom, we will only use 3 of them
joint =
new SixDofJoint(holderNode.getControl(RigidBodyControl.class),
weightNode.getControl(RigidBodyControl.class),
Vector3f.ZERO, new Vector3f(0f, 0, 1f), true);
bulletAppState.getPhysicsSpace().add(joint);
// fetch the reference for the rotational motors and enable them
rollMotor = joint.getRotationalLimitMotor(2);
setEnableMotors(motorEnabled);
rollMotor.setMaxMotorForce(1);
// physics simulation puts objects to sleep when moving slower than
// sleeping threshold. We do not want that.
weightNodeControl.setAngularSleepingThreshold(0);
weightNodeControl.setGravity(Vector3f.ZERO);
}
private void setupArrows() {
Arrow desiredArrowMesh = new Arrow(Vector3f.UNIT_Z);
desiredArrow = new Geometry("desired arrow", desiredArrowMesh);
Material pingMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
pingMat.setColor("Color", ColorRGBA.Pink);
desiredArrow.setMaterial(pingMat);
rootNode.attachChild(desiredArrow);
Arrow distanceArrowMesh = new Arrow(Vector3f.UNIT_Z);
distanceArrow = new Geometry("distance arrow", distanceArrowMesh);
Material yellowMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
yellowMat.setColor("Color", ColorRGBA.Yellow);
distanceArrow.setMaterial(yellowMat);
rootNode.attachChild(distanceArrow);
}
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("RollPlus") && isPressed) {
desiredRollVelocity += 0.5;
}
if (name.equals("RollMinus") && isPressed) {
desiredRollVelocity -= 0.5;
}
if (name.equals("Reset") && isPressed) {
desiredRollVelocity = 0;
desiredArrow.setLocalRotation(new Quaternion());
joint.getBodyB().clearForces();
joint.getBodyB().setPhysicsLocation(Vector3f.UNIT_Y);
joint.getBodyB().setPhysicsRotation(new Quaternion());
}
if (name.equals("toggleEnableMotors") && isPressed) {
setEnableMotors(!motorEnabled);
logger.info("motorEnabled = " + motorEnabled);
}
}
private void setEnableMotors(boolean enableMotors) {
motorEnabled = enableMotors;
rollMotor.setEnableMotor(enableMotors);
}
@Override
public void simpleUpdate(float tpf) {
// calculate rotation step for this frame from velocities
Quaternion rotationStep = new Quaternion().fromAngleNormalAxis(-desiredRollVelocity * tpf, Vector3f.UNIT_Z);
desiredArrow.rotate(rotationStep); //rotazione asse velocità V (radianti) (rosa)
// get current orientation from weight node
Quaternion currentRotation = joint.getBodyB().getPhysicsRotation();
float[] currentAngles = currentRotation.toAngles(null); //velocità angolare motore
if (FastMath.abs(currentAngles[2]) > FastMath.abs(FastMath.DEG_TO_RAD * 44)
&& FastMath.abs(currentAngles[2]) < FastMath.abs(FastMath.DEG_TO_RAD * 46)) {
if (somma == true) {
i += 45;
System.out.println(i + " => " + currentAngles[2] + " : " + currentAngles[2] * FastMath.RAD_TO_DEG);
somma = false;
}
}
if (FastMath.abs(currentAngles[2]) > FastMath.abs(FastMath.DEG_TO_RAD * 46)) {
somma = true;
}
// get desired angles from desired angle’s arrow rotation
Quaternion desiredRotation = desiredArrow.getLocalRotation();
float[] desiredAngles = desiredRotation.toAngles(null); //rotazione asse velocità V (angolare)
Quaternion distanceRotation = desiredRotation.inverse().mult(currentRotation);
distanceArrow.setLocalRotation(distanceRotation); //arrow distanza (giallo) inverso della velocità desiderata * rotazione motore
float[] distance = distanceRotation.toAngles(null);
rollMotor.setTargetVelocity(calcMotorTargetVelocity(2, currentAngles[2], distance[2], desiredRollVelocity));
// logger.info("des: " + Arrays.toString(desiredAngles) + "cur: " + Arrays.toString(currentAngles) + "dis: " + Arrays.toString(distance) + " sum: " + (abs(distance[0]) + abs(distance[1]) + abs(distance[2])));
}
protected float calcMotorTargetVelocity(int index, float currentAngle, float distance, float desiredVelocity) {
float angularVelocity = angleDistance(currentAngle, lastFrameAngles[index]);
lastFrameAngles[index] = currentAngle;
float val = proportionalGain * (distance) + derivativeGain * (desiredVelocity - angularVelocity);
//System.out.println(val);
return val;
}
/**
* Calculate distance between two angles taking the shortest way. Return
* value does not exceed PI and -PI.
*
* @param target
* @param source
* @return distance angle
*/
protected float angleDistance(float target, float source) {
float result = target - source;
if (result > PI) {
return result - TWO_PI;
} else if (result < -PI) {
return result + TWO_PI;
} else {
return result;
}
}
public Node createPhysicsTestNode(CollisionShape shape, float mass) {
Node node = new Node("PhysicsNode");
RigidBodyControl control = new RigidBodyControl(shape, mass);
node.addControl(control);
return node;
}
public Node createPhysicsTestNode2(CollisionShape shape, float mass) {
Node node = new Node("PhysicsNode");
RigidBodyControl control = new RigidBodyControl(shape, mass);
node.addControl(control);
Picture pic = new Picture("freccia");
pic.setImage(assetManager, "Interface/image/freccia.jpg", true);
pic.setWidth(settings.getWidth() / 5);
pic.setHeight(settings.getHeight() / 5);
node.attachChild(pic);
pic.center();
return node;
}
public static void main(String[] args) {
TestRot app = new TestRot();
app.start();
}
}