Two Boxes, one falling box with mass, one stationary box without mass

Hi,

why is the falling box with mass not stopping on top of the other stationary box without mass? I thought when using CollisionShape and RigidBody all nodes would become nodes with collision. Here’s the code, try also commenting out the init stationary massless box part to see that then the falling of the box with mass is occuring correctly. (and vice versa)

[java]



import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.PhysicsSpace;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;



public class Post extends SimpleApplication{



@Override

public void simpleInitApp() {



//cam settings

cam.setLocation(new Vector3f(0, 0, 50));

cam.lookAt(Vector3f.ZERO,Vector3f.UNIT_Y);



//add physics

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

PhysicsSpace ph_space = bulletAppState.getPhysicsSpace();



//init falling box with mass

Box quad = new Box(new Vector3f(0, 20, 0), 1,1,1);

Geometry g_quad = new Geometry(“g_quad”, quad);

Material m_quad = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m_quad.setColor(“Color”, ColorRGBA.Red);

g_quad.setMaterial(m_quad);

Node n_quad = new Node();

n_quad.attachChild(g_quad);

CollisionShape c_quad = CollisionShapeFactory.createBoxShape(n_quad);

RigidBodyControl r_quad = new RigidBodyControl(c_quad, 0.001f);

n_quad.addControl(r_quad);

ph_space.add(r_quad);

rootNode.attachChild(n_quad);



//init sationary massless box

Box quad2 = new Box(new Vector3f(0, 0, 0), 1,1,1);

Geometry g_quad2 = new Geometry(“g_quad”, quad2);

Material m_quad2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m_quad2.setColor(“Color”, ColorRGBA.Red);

g_quad2.setMaterial(m_quad2);

Node n_quad2 = new Node();

n_quad2.attachChild(g_quad2);

CollisionShape c_quad2 = CollisionShapeFactory.createBoxShape(n_quad2);

RigidBodyControl r_quad2 = new RigidBodyControl(c_quad2, 0f);

n_quad2.addControl(r_quad2);

ph_space.add(r_quad2);

rootNode.attachChild(n_quad2);

}



public static void main(String[] args) {

Post app = new Post();

app.start();

}



}



[/java]

Why does the box not stop? I dont want exact calcualtions of resulting collision-path, but i want that at least the box just stops there due to the CollisionShape and RigidBody.

Nice post, like the sample class …so I’ve actually tried it out :wink:



Okay, something I’d found useful is to render the physics with this line:



[java]

ph_space.enableDebug( getAssetManager() );

[/java]

Put that in an you’ll see that both the physics boxes start off at the same coordinates, despite the geometry of one being offset by Y of 20.



Next, drop in this code before you add to the physics space:

[java]

r_quad.setPhysicsLocation( new Vector3f(0f, 20f, 0f) );

[/java]



Vola, you get the box falling down correctly.



Here’s the full class for you :slight_smile:



[java]

package temp;



import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.PhysicsSpace;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;





public class Post extends SimpleApplication{



@Override

public void simpleInitApp() {



//cam settings

cam.setLocation(new Vector3f(0, 10, 50));

cam.lookAt(Vector3f.ZERO,Vector3f.UNIT_Y);



//add physics

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

PhysicsSpace ph_space = bulletAppState.getPhysicsSpace();



ph_space.enableDebug( getAssetManager() );



//init falling box with mass

Box quad = new Box(new Vector3f(0, 0, 0), 1,1,1);

Geometry g_quad = new Geometry(“g_quad”, quad);

Material m_quad = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m_quad.setColor(“Color”, ColorRGBA.Red);

g_quad.setMaterial(m_quad);

Node n_quad = new Node();

n_quad.attachChild(g_quad);

CollisionShape c_quad = CollisionShapeFactory.createBoxShape(n_quad);

RigidBodyControl r_quad = new RigidBodyControl(c_quad, 0.001f);

n_quad.addControl(r_quad);



r_quad.setPhysicsLocation( new Vector3f(0f, 20f, 0f) );



ph_space.add(r_quad);

rootNode.attachChild(n_quad);



//init sationary massless box

Box quad2 = new Box(new Vector3f(0, 0, 0), 1,1,1);

Geometry g_quad2 = new Geometry(“g_quad”, quad2);

Material m_quad2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m_quad2.setColor(“Color”, ColorRGBA.Red);

g_quad2.setMaterial(m_quad2);

Node n_quad2 = new Node();

n_quad2.attachChild(g_quad2);

CollisionShape c_quad2 = CollisionShapeFactory.createBoxShape(n_quad2);

RigidBodyControl r_quad2 = new RigidBodyControl(c_quad2, 0f);

n_quad2.addControl(r_quad2);

ph_space.add(r_quad2);

rootNode.attachChild(n_quad2);

}



public static void main(String[] args) {

Post app = new Post();

app.start();

}



}

[/java]

But the Box is flying around now. I want it to just fall directly with no slopes onto the stationary one.



EDIT:

Sorry, didn’t change initVector3f of falling box to 0,0,0. Now it works even with a calculated collision. Do you know how to turn that off? I mean just let the Box be “like pinned” to the other due to gravity.



EDIT2:

I guess i have to search in bulletAppState…

@kaaltek said:
EDIT:
Sorry, didn't change initVector3f of falling box to 0,0,0. Now it works even with a calculated collision. Do you know how to turn that off? I mean just let the Box be "like pinned" to the other due to gravity.

EDIT2:
I guess i have to search in bulletAppState...


In the sample code I'd posted the effect was the top box falling onto the other box (mass of zero) and staying there, pinned so speak.

What is it you wanted turning off?

the falling box bounces off the stationary box. i want to turn that off, so to speak like there was some glue on the colliding surfaces that do not produce a “bounce off force”.

Better explanation ?



EDIT: Another question. I added an AnalogListener to move the falling box. Here’s the code:

[java]



import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.PhysicsSpace;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;



public class Post2 extends SimpleApplication {



RigidBodyControl r_quad;



@Override

public void simpleInitApp() {



// cam settings

cam.setLocation(new Vector3f(0, 10, 50));

cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);

flyCam.setMoveSpeed(100);



// add physics

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

PhysicsSpace ph_space = bulletAppState.getPhysicsSpace();

ph_space.enableDebug(getAssetManager());



// init falling box with mass

Box quad = new Box(new Vector3f(0, 0, 0), 1, 1, 1);

Geometry g_quad = new Geometry(“g_quad”, quad);

Material m_quad = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”);

m_quad.setColor(“Color”, ColorRGBA.Yellow);

g_quad.setMaterial(m_quad);

Node n_quad = new Node();

n_quad.attachChild(g_quad);

CollisionShape c_quad = CollisionShapeFactory.createBoxShape(n_quad);

r_quad = new RigidBodyControl(c_quad, 0.001f);

n_quad.addControl(r_quad);

r_quad.setPhysicsLocation(new Vector3f(0f, 20f, 0f));

ph_space.add(r_quad);

rootNode.attachChild(n_quad);



// init sationary massless box

Box quad2 = new Box(new Vector3f(0, 0, 0), 10, 0.1f, 10);

Geometry g_quad2 = new Geometry(“g_quad”, quad2);

Material m_quad2 = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”);

m_quad2.setColor(“Color”, ColorRGBA.Red);

g_quad2.setMaterial(m_quad2);

Node n_quad2 = new Node();

n_quad2.attachChild(g_quad2);

CollisionShape c_quad2 = CollisionShapeFactory.createBoxShape(n_quad2);

RigidBodyControl r_quad2 = new RigidBodyControl(c_quad2, 0f);

n_quad2.addControl(r_quad2);

ph_space.add(r_quad2);

rootNode.attachChild(n_quad2);



// set the t key for forward moving

inputManager.addMapping(“move”, new KeyTrigger(KeyInput.KEY_T));

inputManager.addListener(moveListener, “move”);

}



private AnalogListener moveListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

Vector3f v = r_quad.getPhysicsLocation();

if (name.equals(“move”)) {

Vector3f vec = new Vector3f(v.x + valuespeed20, v.y, v.z);

r_quad.setPhysicsLocation(vec);

}

}

};



public static void main(String[] args) {

Post2 app = new Post2();

app.start();

}

}



[/java]



Problem is:

case 1: Let the yellow box fall onto the red thin box, wait 5 seconds, then press t. —> the yellow box WONT slip over the edge.

case 2: Let the yellow box fall onto the red thin box, press immidiately t —> the yellow box SLIPS over the edge.

How can i make the box slip over the edge EVERY time.

Yes im asking a lot of question but i want to learn how to use the engine, so please be patient.

Why do you want physics if you don’t want physics behaviour?



(Having said that there is probably a setting you can set to say how “bouncy” things are.

@zarch said:
Why do you want physics if you don't want physics behaviour?

(Having said that there is probably a setting you can set to say how "bouncy" things are.

I want only a part of the physics namely the gravity.

If all you want is gravity then running a full physics simulation is massive overkill.



Just store a velocity and weight user data on each gravity-able object and then have a gravity system that applies the appropriate acceleration, stopping on collision.

A very good start at learning about the engine is reading the tutorials. Like this for example: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics



It mentions “restitution” which controls the bounciness. Also, you shouldn’t use setPhysicsLocation on rigid bodies like that (unless they are kinematic). Search these forums, there are a lot of discussions about this explaining that you should use forces in a PhysicsTickListener or you will interfere with the physics engine.



But the real question is the one zarch asked. Maybe it would be easier to calculate the gravity effect yourself if that is the only thing you want,

@jmaasing said:
A very good start at learning about the engine is reading the tutorials. Like this for example: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics


Didnt see it myself. Thanks