So, I made my game use a chase camera and WASD controls to roll a ball relative to the camera (see code below, and feel free to criticize because I know I can't be using best practices due to ignorance, and I learn by criticism). It works kinda like ThirdPersonHandler, but controls rotation rather than translation and I didn't see any built in way to do that, so maybe something like this can be added to the engine (that is if I didn't just recreate the wheel )
Now, I have 2 questions, if my camera is at vector 10,2,0 relative to the ball, and the ball is moving at a vector of 0,0,-10, how can I keep the camera at 10,2,0 relative to the ball unless I move the mouse? (currently, the camera pulls around to 0,0,10, or following behind the ball)
Second, is there any way to smooth out the camera movement via the mouse? It is quite jumpy when the camera minimum distance is high.
Here's my first code post, so criticize away. I don't take offense by someone helping me!
private class BallInputAction extends InputAction {
private final Vector3f appliedForce = new Vector3f();
private final int power = 1000;
private final Vector3f rotation = new Vector3f();
/**
* This method gets invoked upon key event
*
* @param evt more data about the event (used to get key pressed)
*/
public void performAction( InputActionEvent evt ) {
rotation.set( DisplaySystem.getDisplaySystem().getRenderer().getCamera().getDirection() );
rotation.multLocal( (evt.getTime() * power) );
if(evt.getTriggerCharacter() == ' ')
{
//TODO: Make sure we are in collision with something, and jump away from the collision, not necessarily straight up
// make the ball jump
physicsNode.addForce(new Vector3f(0,1000,0));
}
if( evt.getTriggerCharacter() == 'w' || evt.getTriggerCharacter() == 'W' )
{
// rotate forward relative to the camera
appliedForce.set( rotation.z, 0 , -rotation.x );
physicsNode.addTorque( appliedForce );
}
if( evt.getTriggerCharacter() == 'a' || evt.getTriggerCharacter() == 'A' )
{
// rotate left relative to the camera
appliedForce.set( -rotation.x, 0 , -rotation.z );
physicsNode.addTorque( appliedForce );
}
if( evt.getTriggerCharacter() == 's' || evt.getTriggerCharacter() == 'S' )
{
// rotate backwards relative to the camera
appliedForce.set( -rotation.z, 0 , rotation.x);
physicsNode.addTorque( appliedForce );
}
if( evt.getTriggerCharacter() == 'd' || evt.getTriggerCharacter() == 'D' )
{
// rotate right relative to the camera
appliedForce.set( rotation.x, 0 , rotation.z );
physicsNode.addTorque( appliedForce );
}
}
}