I’m hoping someone can point me in the right direction on an issue I’m having.
To illustrate what I’m going for, I want to have the character face the direction of the point where my the mouse cursor collides with geometry on the screen, and use this direction as a source for the player’s walk direction. Everything works except, apparently, the getPhysicsLocation of the player.
I’m updating everything in the simpleUpdate() function. I can set the initial player position fine using: “setPhysicsLocation(new Vector3f(somePositionX, somePositionY, somePositionZ));” and the logic for making the player face the point where the mouse cursor is, and I can walk toward or away from that point. However, the first component of the ray that is used to determine the direction the player should face seems to always be (0, 0, 0). It’s tough to explain, because I’ve been drinking a good deal now and I’m not the brightest crayon to being with. Let me show you:
…see how the direction the player is facing makes it seem as though the player is still at (0, 0, 0)? What am I doing wrong? Here is the code I’m using in simpleUpdate:
[Edit: Wrapped in java tags to avoid a possible throat punch.]
playerWalkDirection.set(0, 0, 0);
CollisionResults results = new CollisionResults();
Vector2f screenPos = new Vector2f();
screenPos.set(inputManager.getCursorPosition().x, inputManager.getCursorPosition().y);
Vector3f worldCoords = cam.getWorldCoordinates(screenPos, 0);
Vector3f worldCoords2 = cam.getWorldCoordinates(screenPos, 1);
Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
worldNode.collideWith(mouseRay, results);
if(results.size() > 0){
Vector3f pt = results.getCollision(0).getContactPoint();
Vector3f playerLocation = playerControl.getPhysicsLocation(); //<<<<<<<<<<<<<<<<<<<<<<<<<<< I suspect this is the issue, but don't see how
Ray playerFaceCursor = new Ray(playerLocation, pt);
playerControl.setViewDirection(playerFaceCursor.direction.normalize().divide(10));
}
if(playerForward == true) {
playerWalkDirection.addLocal(playerControl.getViewDirection());
playerForward = false;
}
if(playerBackward == true) {
playerWalkDirection.addLocal(playerControl.getViewDirection().negate());
playerBackward = false;
}
Any help is certainly appreciated!