Hi, I am studying the inputs and I see that they work very well, however I do not understand why there is a delay when throwing the first ball … As you will see in this video, when you press the key, you spend 1 second before throwing the first ball, however they throw themselves well …
Someone knows what the problem is … I leave a video and the code.
//funcion inicial
public void simpleInitApp() { //funcion principal para iniciarlizar los objetos en una escena
camaraConfiguracion(); // propiedades de la camara
cargarMateriales(); //cargo los materiales de los archivos
cargarModelos(); //cargo los modelos de los archivos
FisicaEnLaEscena(); //agrega propiedades físicas a los nodos
sueloConFisica(); //agrega fisica al suelo
cubosConFisica(); //agrega fisica a los cubos
Teclas();
}
private void Teclas(){
// Puede asignar una o varias entradas a una acción con nombre
inputManager.addMapping("disparar", new MouseButtonTrigger(0)); //cuando presiono space "disparar" se guarda en add mapping
// Añadir los nombres al oyente de acción analogico,("clase AnalogListener").
inputManager.addListener(procesarTeclas, "disparar");//agrego los eventos a la funcion addListener es muy impotante para que funcionen las teclas
} //funcion para crear las teclas
private final ActionListener procesarTeclas = new ActionListener() {
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals("disparar") && isPressed){
System.out.println("se va a lanzar un proyectil");
lanzarProyectil();
}
}
}; //procesa las teclas presionadas
private void lanzarProyectil(){
//cargo la escena sphera
Escena_esfera = assetManager.loadModel("Models/piedra1/piedra1.j3o");
//-------------------------------------------------------------------------------//
proyectil = ((Node)Escena_esfera).getChild("esfera");
rootNode.attachChild(Escena_esfera);
Vector3f posicion_camara = cam.getLocation();
Escena_esfera.setLocalTranslation(posicion_camara);
System.out.println(posicion_camara);
////creo un rigidbody control a las CAJAS le agrego las físicas
//colisiones
CollisionShape cajasColision = CollisionShapeFactory.createBoxShape(proyectil);//creo la colision
//cuerpo rigido
RigidBodyControl cuerpoRigidoCajas = new RigidBodyControl(cajasColision, 5.0f);//creo el control del cuerpo y por parametro agrego la colision y la masa
proyectil.addControl(cuerpoRigidoCajas);//agrego un controlador de fisica a ese objeto
fisica.getPhysicsSpace().add(cuerpoRigidoCajas);//agrego a las fisicas del space
proyectil.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(500.0f));
} //crea un proyectil con forma de esfera
I think the problem is here, but there is something I do not understand … Could it be that it takes 1 second to add a node to the rootNode?
//cargo la escena sphera
Escena_esfera = assetManager.loadModel("Models/piedra1/piedra1.j3o");// The problem will be here
//-------------------------------------------------------------------------------//
proyectil = ((Node)Escena_esfera).getChild("esfera"); //The problem will be here
rootNode.attachChild(Escena_esfera); //The problem will be here
Vector3f posicion_camara = cam.getLocation(); //The problem will be here
Escena_esfera.setLocalTranslation(posicion_camara);//The problem will be here
System.out.println(posicion_camara); //The problem will be here
SOLUTION
Thanks was that, I have to load the scenes in the INIT before I can work with them …
@Override
//funcion inicial
public void simpleInitApp() { //funcion principal para iniciarlizar los objetos en una escena
//cargo la escena sphera
Escena_esfera = assetManager.loadModel("Models/piedra1/piedra1.j3o");//The problem was that I had to start this variable in the init
//-------------------------------------------------------------------------------//
camaraConfiguracion(); // propiedades de la camara
cargarMateriales(); //cargo los materiales de los archivos
cargarModelos(); //cargo los modelos de los archivos
FisicaEnLaEscena(); //agrega propiedades físicas a los nodos
sueloConFisica(); //agrega fisica al suelo
cubosConFisica(); //agrega fisica a los cubos
Teclas();
}
And if I want to use that scene again in a function I call it back … I’m not sure, but I imagine that the scene in the public variable is loaded in memory. Then if I want to use it in a function, it does not take long to start. …It makes a noise that I have to write 2 times the call to the scene so that it works every time I shoot, BUT ALREADY WORKS A LOT OF THANKS !!!
private void lanzarProyectil(){
//cargo la escena sphera
Escena_esfera = assetManager.loadModel("Models/piedra1/piedra1.j3o");// The problem was that I had to start this variable in the init before starting it here be here
//-------------------------------------------------------------------------------//
proyectil = ((Node)Escena_esfera).getChild("esfera");
rootNode.attachChild(Escena_esfera);
Vector3f posicion_camara = cam.getLocation();
Escena_esfera.setLocalTranslation(posicion_camara);
Vector3f posicion = new Vector3f(Vector3f.ZERO);//posicion 0,0,0 no la uso pero la tengo por las dudas para hacer pruebas
System.out.println(posicion_camara);
////creo un rigidbody control a las CAJAS le agrego las físicas
//colisiones
CollisionShape cajasColision = CollisionShapeFactory.createBoxShape(proyectil);//creo la colision
//cuerpo rigido
RigidBodyControl cuerpoRigidoCajas = new RigidBodyControl(cajasColision, 5.0f);//creo el control del cuerpo y por parametro agrego la colision y la masa
proyectil.addControl(cuerpoRigidoCajas);//agrego un controlador de fisica a ese objeto
proyectil.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(500.0f));
fisica.getPhysicsSpace().add(cuerpoRigidoCajas);//agrego a las fisicas del space
} //crea un proyectil con forma de esfera