so i have created a hollow box using compound collision shape. i have also created a sphere which will be placed inside the box.My idea is to create a simple marble maze type game. but the problem i am currently facing is that i cannot rotate the physics object.
m = new Node("node");
CompoundCollisionShape compoundShape = new CompoundCollisionShape();
BoxCollisionShape base = new BoxCollisionShape(new Vector3f(3f, 0.1f, 3f));
BoxCollisionShape frontBox = new BoxCollisionShape(new Vector3f(3f, 0.4f, 0.1f));
BoxCollisionShape backBox = new BoxCollisionShape(new Vector3f(3f, 0.4f, 0.1f));
BoxCollisionShape leftBox = new BoxCollisionShape(new Vector3f(0.1f, 0.4f, 2.8f));
BoxCollisionShape rightBox = new BoxCollisionShape(new Vector3f(0.1f, 0.4f, 2.8f));
compoundShape.addChildShape(base, new Vector3f(0, 0, 0f));
compoundShape.addChildShape(frontBox, new Vector3f(0, 0.5f, 2.9f));
compoundShape.addChildShape(backBox, new Vector3f(0, 0.5f, -2.9f));
compoundShape.addChildShape(leftBox, new Vector3f(2.9f, 0.5f, 0f));
compoundShape.addChildShape(rightBox, new Vector3f(-2.9f, 0.5f, 0f));
maze_phy = new RigidBodyControl(compoundShape,0f);
// maze_phy.setAngularFactor(0);
m.addControl(maze_phy);
getPhysicsSpace().add(maze_phy);
rootNode.attachChild(m);
initSphere();
it will be really helpful is someone shows a way to rotate the object cause rotation is the essence oi my game.
public class RotateControl
extends SimpleControl {
// *************************************************************************
// constants
// *************************************************************************
// fields
/**
* axis of rotation (in local coordinates, length=1): set by constructor
*/
final private Vector3f axis;
/**
* rate of rotation (radians per second): set by constructor
*/
final private float rate;
// *************************************************************************
// constructors
/**
* Instantiate an enabled control with the specified vector.
*
* @param rate rate of rotation (in radians per second)
* @param axis axis of rotation (in local coordinates, not null, positive
* length, unaffected)
*/
public RotateControl(float rate, Vector3f axis) {
Validate.nonNull(axis, "axis");
if (MyVector3f.isZeroLength(axis)) {
//logger.log(Level.SEVERE, "axis={0}", axis);
throw new IllegalArgumentException(
"axis should have positive length");
}
this.axis = axis.normalize();
this.rate = rate;
assert isEnabled();
}
// *************************************************************************
// SimpleControl methods
/**
* Update the spatial's orientation. Invoked when the spatial's geometric
* state is about to be updated, once per frame while this control attached
* and enabled.
*
* @param updateInterval time interval between updates (in seconds, ≥0)
*/
@Override
protected void controlUpdate(float updateInterval) {
super.controlUpdate(updateInterval);
if (spatial == null) {
return;
}
Quaternion rotation = new Quaternion();
float angle = rate * updateInterval;
rotation.fromAngleNormalAxis(angle, axis);
spatial.rotate(rotation);
}
}
A physics control controls the spatials location and rotation (thats the point of physics controls) so you need to rotate the physics control instead. Note however that when you move a physics control directly the physics becomes invalid. You either have to move it by applying forces or you have to set it to kinematic mode while you move it.
@normen
Since we have this post at least 2 times a month, what do you think about making this an assertion?
Kinda like in the setters check if the physicscontroll exist.
Are assertions on in the sdk by default?
Else it probably wont help much, and doing a normal if seems like a waste for 99% of normal uses to me.
In the setters of Spatial check if a PhysicsControl exists? Not a good idea, that would make core dependent on bullet. Can’t avoid every noob error, thats what documentation is for.
Thats why we set up the build process like it is now, it will fail if theres unwanted cross-dependencies. Bullet depending on terrain is already not exactly beautiful… At least thats just a compile time dependency and can be avoided by not using the automatic collision shape generation via CollisionShapeGenerator.
first of all thank you for the quick reply.
so if i want to rotate the box based on some user control to guide the sphere through the maze i have to set it to kinematic mode and rotate the box and then undo kinematic mode as soon my rotation stops.Is this what you are saying…
If it is a sphere, why rotate it at all? just have your vector where your simulated forward is stored somewhere and only rotate this. Then use apply forces to move, and a high linear dampening to make it stop.