Hey guys,
First time here, so I just trying to get my bearings with JMonkey. I thought I’d give myself some practice my developing a simple side scrolling game; however, my first problem lies in the player hitting the floor. I’ve created a simple box as the player and a box as the floor. Problem is, even after fooling around with the stepheight and all, the best I could get was the “player” bobbing up and down inside the floor for about 6 seconds, then falling through.
Now, according to past threads with this problem, it could be because I’m using a box as the floor. But with a setup this small, I’d like to know if that really is the problem? And if so, how should I use a plane as the floor? I’ve tried to setup the plane, but I’m not understanding how to make it a rigidbody because it’s not a geometry. Or am I missing something?
Oh, and I’ve switched on the physicsSpace debugger, and the collision meshes seem to wrap around both objects nicely
Anyway, here is the main piece of code:
[java]public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//First, create geometry
Box playerShape = new Box(Vector3f.ZERO, 1,1,1);
Spatial p1 = new Geometry(“Player1”,playerShape);
//Next, attach material
Material p1mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
p1mat.setColor(“Color”,ColorRGBA.Green);
p1.setMaterial(p1mat);
//Then, create physics
BoxCollisionShape character = new BoxCollisionShape(new Vector3f(1f,1f,1f));
player = new CharacterControl(character,0.7f);
p1.addControl(player);
player.setPhysicsLocation(new Vector3f(0f,5f,0f));
//Then, attach physics and make visible
rootNode.attachChild(p1);
bulletAppState.getPhysicsSpace().add(player);
//First, create geometry
Box floor = new Box(Vector3f.ZERO,3f,0.1f,3f);
Geometry floor_geo = new Geometry(“Floor”, floor);
//Then, attach material
Material floor_mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
floor_mat.setColor(“Color”,ColorRGBA.Gray);
floor_geo.setMaterial(floor_mat);
//Then, create physics
RigidBodyControl box_phy = new RigidBodyControl(0);
floor_geo.addControl(box_phy);
//Then, attach physics and make visible
bulletAppState.getPhysicsSpace().add(box_phy);
rootNode.attachChild(floor_geo);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
/* flyCam.setEnabled(false);
ChaseCamera chasecam = new ChaseCamera(cam,p1,inputManager);
chasecam.setDefaultVerticalRotation(-FastMath.PI/0.5f);*/
//initKeys();
}[/java]
I’m not a physics expert but, yes the fact that you use a thin box is the problem.
did you try to make your floor more than 0.1 thick?
Also don’t use a Plane class, it a math class, it’s not a mesh class. You should have used a quad and make a geometry from it.
But still for your issue, don’t use a quad, because you’ll have the same issue.
Your floor has extents of 3m and ur character has 1. So the triangles will be ~6m for the floor, that’s too big. Try decrease them both by a factor of 10 and see if u have the same issue.
There has been quite a few of these threads recently
Thanks for the replies guys.
First, I tried nehon’s suggestion, and increased the thickness to 0.5. Didn’t work. But thanks for the tip about the plane stuff.
wezrule, when you said “decrease them both by a factor of 10” did you mean this:
[java]Box playerShape = new Box(Vector3f.ZERO, 0.1f,0.1f,0.1f);
BoxCollisionShape character = new BoxCollisionShape(new Vector3f(0.1f,0.1f,0.1f));[/java]
for the player and:
[java]Box floor = new Box(Vector3f.ZERO,.3f,.01f,.3f);[/java]
for the floor?
I tried that, and it still didn’t work.
But I noticed something weird when I was testing it out. When the object falls through/sinks into the floor, the mesh of the player sometimes gets thrown off and appears, say, 2 or 3 units lower than than where the player is, like this:
Any ideas why this is happening, and is it related to my problem?
try a capsule collision shape they are easier to move
Sweet, it works now. I changed it to a capsule collider and fiddled around with the settings. Even with the same floor settings of 3f, 0.1f, and 3f, the character with a capsule collider didn’t fall/sink in.
Here was the “fix” : [java]CapsuleCollisionShape character = new CapsuleCollisionShape(1.5f,0f);[/java]
Awesome. I love the fact that I got such quick responses.
Thanks guys.