I’m having trouble getting basic collisions to work in my game. I had them working previously, and now I’ve broken something but I’m not sure what.
The way it worked previously was quite similar to the 9th tutorial - the player variables were encapsulated within the main class, and the main update loop was what did the moving according to the input bindings. That worked but not well. Plus I wanted to move everything into custom application controls.
So I created an AbstractActorControl, and from that a PlayerManualControl. The AbstractActorControl creates its own physics control, a character control. It adds itself and the physics control to the spatial’s controls collection, and adds the physics control to the bullet app state’s physics space. The code for the level itself is unchanged.
What happens now is that I can move around exactly as per the flycamera. I go right through walls, and no gravity appears to be in effect. Is there something missed out? Posted below is the AbstractActorControl constructor.
[java]public AbstractActorControl(Spatial _spatial, Vector3f location, BulletAppState _bulletAppState, boolean isNode)
spatial = _spatial;
shape = new CapsuleCollisionShape(1.5f, 6f, 1);
physicsControl = new CharacterControl(shape, 0.1f);
spatialIsNode = isNode;
bulletAppState = _bulletAppState;
Its hardcoded to use a CapsuleCollisionShape of a fixed size at the moment.
And here is the loadLevel code:
[java]private void loadLevel()
Level newLevel = levelManager.loadLevel(“test”);
Node levelNode = newLevel.getLevelNode();
CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(levelNode);
RigidBodyControl levelPhysicsControl = new RigidBodyControl(collisionShape, 0);
System.out.println(“Added level physics to bulletappstate”);
Any help would be appreciated.
EDIT: I do add the playerNode to the rootNode after creating the level, so its not the rootNode.detachAllChildren call. I do that because in future I really will need to detach all children, obviously after saving any pertinent information though.
Uh, why should the camera move any way else? You just added a character to the world… If you want to move the cam with it you have to place it to the location of the character control each frame.
I was already doing that, in the update method which is not shown. What I had forgot to do was enable my own AbstractCharacterControl. I still dont know why that prevented the physics from working though.
EDIT: Nevermind, I got it worked. Was the fact that the AbstractCharacterControl was disabled. Not sure why that affected the bullet CharacterControl.
It didnt, it just did not move your camera anymore because it was disabled.
I think you’re right. I didnt realize there was a distinction between the flyCam and cam, the Camera object. Now, I have added a button that disables my control and the physics control for testing purposes. Turning it off, I can fly around and look at the structure of the maze. When I turn it back on again, I snap back to my original position, not where I was flying.
I could fix it by passing the flyCam into my control and updating the physics location when the controls are re-enabled, but its not important at this point.
FlyCam is also a control, you can disable it too, not just override the value it set