Terrain adjust height problem in physics space

Hi,



I am busy with a small project where I am trying to adjust the height of my terrain and then see the effects in the physics space in runtime.

So here is my problem, everything works nicely even the adjusting of the height of the terrain I can see, the problem is that the terrain in the physics space is not updated. Therefor I ask if someone can please give me a hint regarding this. Thanks.



Here is the code to load terrain physics:

[java]

protected void loadTerrainPhysics() {

terrain.setLocked(false);

terrainBodyControl = new RigidBodyControl(0);

terrain.addControl(terrainBodyControl);

bulletAppState.getPhysicsSpace().addAll(terrain);

}

[/java]



And here is the code to adjust the height of the terrain:

[java]

public void changeHeight(float x, float z) {

terrain.adjustHeight(new Vector2f(x, z), 2f);

}

[/java]



Any help, please…?

You have to update the collision shape, e.g. terrainRigidBody.setCollisionShape(CollisionShapeFactory.createMeshShape(terrain));

Just adding that doesn’t work.

Do I need to add the newly created physics shape to the physics space and remove the old one?

No, you do something wrog if this doesn’t work.

That’s what I thought, but I can’t find anything wrong.

Could it be a bug in jME3?

Please help or give some example



Thanks for the help.

I gave an example already, show me your code first.

Thanks,

I can try to rip out something from my code, but everything is a bit integrated.

It will be difficult.

What I can tell you is that I created a scene with a terrain in jMP and then load that in my code.

I then get the terrain from the scene using this method:

[java]

//Load from file

sceneNode = (Node)assetManager.loadModel("Scenes/level1.j3o");

rootNode.attachChild(sceneNode);



//Get the terrain

terrain = (TerrainQuad) sceneNode.getChild("terrain-"+sceneName);



//Load terrain physics

terrain.setLocked(false);

terrainBodyControl = new RigidBodyControl(0);

terrain.addControl(terrainBodyControl);

bulletAppState.getPhysicsSpace().addAll(terrain);



[/java]



This is the code I use to load the scene.

Then I have code that loads the player:

[java]

//Load the physics shape

playerCharacter = new PhysicsCharacter(new CapsuleCollisionShape(bb.getXExtent()/2, bb.getYExtent(), 1), .01f);

playerCharacter.setJumpSpeed(24);

playerCharacter.setFallSpeed(30);

playerCharacter.setGravity(60);



Vector3f pos = position.clone();

pos.y += bb.getYExtent();

playerCharacter.setPhysicsLocation(pos);

worldFactory.getBulletAppState().getPhysicsSpace().add(playerCharacter);

[/java]

Then I change the height of my terrain using these methods:

[java]

public void doTerrainHeightChange(Vector3f location) {

float height = 2f;

float size = 2f;

float x = location.x;

float z = location.z;

terrain.adjustHeight(new Vector2f(x, z), height);

terrain.adjustHeight(new Vector2f(x-size, z-size), height);

terrain.adjustHeight(new Vector2f(x-size, z+size), height);

terrain.adjustHeight(new Vector2f(x+size, z-size), height);

terrain.adjustHeight(new Vector2f(x+size, z+size), height);

terrainBodyControl.setCollisionShape(CollisionShapeFactory.createMeshShape(terrain));



}

[/java]

As soon I move over that changed terrain height, my character just walks strait through the terrain.



Please help.

Hi Normen,



This is what I found.



seems the the raising of the terrain works, but when I try to lower the terrain with a negative delta it doesn’t update the physics.



So if I call this : terrain.adjustHeight(new Vector2f(x, z), -delta); it doesn’t work.

Thats indeed strange, only thing I can imagine is that the main terrain float buffer isn’t updated somehow… When the collision shape is regenerated it should definitely be accounted for in the physics… @Sploreg got an idea here?

Yes I agree. I seriously don’t know what is going on with this.

If it was the terrain float buffer why does it work when I raise the terrain?

Anyways, if anybody knows of a solution it will be much appreciated.

Thanks.

Make a test case for tis, its the best and quickest way to get a fix if you can reproduce this at all.

Thanks for the help. Will try and upload a test if still getting the problem.

Again, thank you normen.

Hello ndebruyn!



This should help you :slight_smile:







private RigidBodyControl rigControl = new RigidBodyControl(0);



public void updatePhysics(BulletAppState bulletAppState){

bulletAppState.getPhysicsSpace().remove(terrain);

terrain.removeControl(rigControl);

rigControl = new RigidBodyControl(0);

terrain.addControl(rigControl);

bulletAppState.getPhysicsSpace().addAll(terrain);

}

Thanks maertuerer,

I have tried that but it is still not doing the job.

try to replace this



public void doTerrainHeightChange(Vector3f location) {

float height = 2f;

float size = 2f;

float x = location.x;

float z = location.z;

terrain.adjustHeight(new Vector2f(x, z), height);

terrain.adjustHeight(new Vector2f(x-size, z-size), height);

terrain.adjustHeight(new Vector2f(x-size, z+size), height);

terrain.adjustHeight(new Vector2f(x+size, z-size), height);

terrain.adjustHeight(new Vector2f(x+size, z+size), height);

terrainBodyControl.setCollisionShape(CollisionShapeFactory.createMeshShape(terrain));

}



with



public void doTerrainHeightChange(Vector3f location) {

float height = 2f;

float size = 2f;

float x = location.x;

float z = location.z;

terrain.adjustHeight(new Vector2f(x, z), height);

terrain.adjustHeight(new Vector2f(x-size, z-size), height);

terrain.adjustHeight(new Vector2f(x-size, z+size), height);

terrain.adjustHeight(new Vector2f(x+size, z-size), height);

terrain.adjustHeight(new Vector2f(x+size, z+size), height);

updatePhysics(bulletAppState);

}

Again, it should not be necessary to add a completely new rigid body.

Ok will try, but I do have one question. What does updatePhysics() do?

I do not have a method like that.

He gave you the body of that method a post before :roll:

Okay, now I give up.

I tried what you guys suggested above and it still does not work.

Looks like I will have to create a test class.

Thanks for the help, I will get back to you.