Hi,
I have a problem regarding the collision of a box and a sphere. The sphere collides with the box even if I move the box away before the sphere reaches it. I supsect that the position of the box doesnt get update correctly if i move the node it is attached to. I dont get why the sphere floats at the positon the box was before. I thought the box would just copy the movement of its parent node. I would very much appreciate if someone could help me with my problem or solve it.
my code
private void buildSphere() {
Material material = new Material(assetManager, âCommon/MatDefs/Misc/Unshaded.j3mdâ);
material.setColor(âColorâ, ColorRGBA.Blue);
Sphere sp = new Sphere(50, 50, 1);
Geometry geoSp = new Geometry(âsphereâ, sp);
geoSp.setMaterial(material);
geoSp.setLocalTranslation(0, 20, 0);
rootNode.attachChild(geoSp);
spPhy = new RigidBodyControl(1f);
geoSp.addControl(spPhy);
bulletAppState.getPhysicsSpace().add(spPhy);
}
private void buildBox() {
node = new Node(âboxâ);
Material material = new Material(assetManager, âCommon/MatDefs/Misc/Unshaded.j3mdâ);
material.setColor(âColorâ, ColorRGBA.Yellow);
Box b = new Box(boxSize, boxSize, 0.1f);
Geometry box = new Geometry(âtopâ, b);
box .setMaterial(material);
rootNode.attachChild(box );
box .setLocalTranslation(new Vector3f(0, 0, boxSize));
RigidBodyControl phy= new RigidBodyControl(0.0f);
box .addControl(phy);
bulletAppState.getPhysicsSpace().add(phy);
}
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals(âmouse_upâ)) {
Vector3f v = box.getLocalTranslation();
box.setLocalTranslation(v.x, v.y + value * 2, v.z);
}
if (name.equals(âmouse_downâ)) {
Vector3f v = box.getLocalTranslation();
box.setLocalTranslation(v.x, v.y - value * 2, v.z);
}
if (name.equals(âmouse_leftâ)) {
Vector3f v = box.getLocalTranslation();
box.setLocalTranslation(v.x + value * 2, v.y, v.z);
}
if (name.equals(âmouse_rightâ)) {
Vector3f v = box.getLocalTranslation();
box.setLocalTranslation(v.x - value * 2, v.y, v.z);
}
}
};
You dont move a physics object anywhere, you just move the geometry.
You need to be a little more precise there. The node is moved and therefore the box and the box has a collision shape. Shouldnt they move as one?
ps in the handler its node not box
No, read the tutorials and manual. You have to move the physics object, if you want to move the spatial and the physics object should move with it you have to set the control to kinematic mode.
I read the tutorials, I read the manual and I used the search. After setting the control to kinematic it doesnt collide anymore. Sorry but I just dont get this.
It doesnt fall anymore, thats what kinematic means. As I said you have to move the physics control instead of the node if you want normal physics objects.
Ah okay, we are not talking about the same thing. Now the sphere falls trough the box. The box was never falling.
And my problem from the start was that the sphere was floating where the box used to be before being moved because the physics object doesnt get moved as well.
I thought the control has the collision shape and that the shape needs to be moved. But i guess the shape doesnt get moved along with the geometry i added it to.
With a physics object you should not use the setTranslation or rotation methods. You should be âmovingâ the physics control. For instance, you have RigedBodyControls so you can move these by applying a force. With CharacterControls you can move it by setting a walkDirection.
If you do need to actually position the object use the setPhysicsLocation(). This will move the physics control to the specified place and also move the geometry that is connected to it. However, you should not do this for general movement otherwise you will not get the desired behaviour (the collisions will not know about the speed and distance moved etc).
A Kinematic object moves with the spatial. It will not experience any forces (including gravity) but will interact with objects around it.
Thank you. That solved my problem.