Physics go weird:RigidBodyControl pass through the wall

Hi, I’m developing a FPS game like Counter-Striker and I’m stuck with the physics.

When I shoot an object (a red cylinder) towards a wall, it just pass through the wall

And I throw it towards the floor, it some times drop out of the world…

Here is the code.

I found that objects may easily pass through the floor around the vertexes of the collision mesh.

Some object pass through one face of the wall but stoped by another face of the wall. It seem that it go through the edge.

No idea how to solve this problem, I need your help :confused:

May be it is because control.setGravity(new Vector3f(0, -98f, 0));
-98 is to much It should be -9.8f also for your player player.setGravity(98f); → player.setGravity(9.8f);
also you better to use HullColisionShape for your terrain collision shape.

/**
      
      
        		 * Load models
      
      
        		 */
      
      
        		Spatial model = app.getAssetManager().loadModel("Models/Terrain/iceworld.j3o");
      
      
        		rootNode.attachChild(model);
      
      
        

      
      
        		/**
      
      
        		 * Initialize Bullet Physics
      
      
        		 */
      
      
        		// Terrain
      
      
        		this.terrain = new RigidBodyControl(0);
      
      
        		model.addControl(terrain);

This adds a mesh accurate collision shape to terrain, please change it to HullCollisionshape

see https://jmonkeyengine.github.io/wiki/jme3/advanced/physics.html#create-a-collisionshape

Thanks for your advice.

Change gravity seems useless. I’ll try HullCollisionShape.

I tried HullCollisionShape.

It works fine will simple Box mesh, dynamic rigid body don’t pass through it.
But when I tried to create a HullCollisionShape will complex mesh it didn’t work.

private List<RigidBodyControl> terrainHulls;
@Override
protected void initialize(Application app) {
        // the other code...
	// Terrain
	if (useHullCollision) {
		model.breadthFirstTraversal(new SceneGraphVisitor() {
			@Override
			public void visit(Spatial spatial) {
				if (spatial instanceof Geometry) {
					Mesh mesh = ((Geometry) spatial).getMesh();
					terrainHulls.add(new RigidBodyControl(new HullCollisionShape(mesh), 0));
				}
			}
		});
	} else {
		CollisionShape shape = CollisionShapeFactory.createMeshShape(model);
		terrain = new RigidBodyControl(shape, 0);
	}
}

@Override
protected void onEnable() {
	simpleApp.getRootNode().attachChild(rootNode);
	
	getStateManager().attach(bulletAppState);
	
	// terrain
	if (useHullCollision) {
		for(RigidBodyControl control : terrainHulls) {
			bulletAppState.getPhysicsSpace().add(control);
		}
	} else {
		bulletAppState.getPhysicsSpace().add(terrain);
	}
	// the other code..
}

It look like I didn’t generate HullCollisionShape properly. I lost the information of transform.

I read the source of CollisionShapeFactory.createDynamicMeshShape(Spatial), it generates HullCollisionShape for me.

	// Terrain
	if (useHullCollision) {
		CollisionShape shape = CollisionShapeFactory.createDynamicMeshShape(model);
		terrain = new RigidBodyControl(shape, 0);
	} else {
		CollisionShape shape = CollisionShapeFactory.createMeshShape(model);
		terrain = new RigidBodyControl(shape, 0);
	}

The problems is, this shape is

A less accurate shape for dynamic Spatials that cannot easily be represented by a CompoundShape. wiki:jme3/advanced/physics

My player can’t walk one the ground now.

This looks the same problem.

Trying this.

Okay, I could solve passing through the wall problem
So keep this boolean useHullCollision = false; as false
then add

 control.setCcdSweptSphereRadius(.1f); //see javadoc for more info
 control.setCcdMotionThreshold(0.001f); //see javadoc for more info

to

if (trigger) {
            if (time >= COOLDOWN_TIME) {
                time = 0f;
                
                camLoc.set(cam.getLocation());
                camLoc.addLocal(cam.getDirection().mult(20));

                Spatial bomb = createCylinder(ColorRGBA.Red);
                rootNode.attachChild(bomb);
                bomb.setLocalTranslation(camLoc);
                
                RigidBodyControl control = new RigidBodyControl(0.5f);
                bomb.addControl(control);
                bulletAppState.getPhysicsSpace().add(control);
                control.setCcdSweptSphereRadius(.1f);
                control.setCcdMotionThreshold(0.001f);
                
                control.setLinearVelocity(cam.getDirection().mult(100).add(walkDirection));
                control.setGravity(new Vector3f(0, -9.8f, 0));
            }
        }

P.S : Try to use native bullet because it is faster.

Thank you, it’s much better after reset the ccd. I think I need to spend more time on the doc.

But it feels that the bomb is floating in the air, so I tried to use larger gravity, then it looks likes this

Gravity=98f

Maybe I should try to make the model smaller.

I find it will always pass through the wall just if you find the right angle.:sweat:

Gravity=9.8f

1 Like

If you want it realistic, keep these things in mind:

  • gravity on earth is 9.81
  • a bullet falls rapidly because of aerodynamics (low friction)
  • bullets are have a small mass, but deal a lot of Force because of their speed

If you want to create a game, see what is easiest & fastest.
Also, setting gravity to 98 may result in weird falling.

1 Like

You will be happiest if your geometry is 1 unit = 1 meter. Then all of the other numbers like gravity will make more sense.

Quite often games will double the gravity, though, because for some reason in a game realistic gravity feels to floaty. Several game physics books I’ve read have mentioned this. 20 is an often used gravity value.

1 Like

Do not use real physical objects for anything actually fast!
Instead do a raycast each game tick, and move the bullet in code if it is clear (or hit whatever it did hit)

2 Likes

Thank you for your advice. I need more try.

It’s better to avoid ccd if possible, because it may slow down the simulation a lot. Try to make the collsion shape a little bigger and to use a simple shape like sphere instead of hull for the bombs.

1 Like