[SOLVED] How to make turrents shoot at a specific Spatial or direction

This Formula is the key that solves this problem ,
Code:

                       /**
                        * Load Laser Guns starting from the NPC & pointing to the player with speed of 100f
                        */
                      Load_LaserGun(new Sphere(10, 10, 0.2f),"man_smoke_1.png",spaceTurrent.getPhysicsLocation()
                            ,Vector3f.ZERO,0.2f,0.15f,player.getPhysicsLocation().subtract(spaceTurrent.getPhysicsLocation()).normalize().mult(100f));

but this must be considered as well but in terms of physics Rotation in my case , not the Spatial Rotation
Code :

     /**
     * Turns the NPC physics viewDirection to face the player
     * @param NPClookPosition Look direction vector  
     * @param upVector the Y-axis prependicular line
     */
    private void rotateNPCTOFacePlayer(Vector3f NPClookPosition,Vector3f upVector) {
          turrentNode.lookAt(NPClookPosition, upVector);
          spaceTurrent.setPhysicsRotation(turrentNode.getLocalRotation());
    }

Anyway, the problem is solved & Thanks for all !

Code :


package main_WorldMap;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;

/**
 *
 * @author twisted AKA Pavly
 */
public class SpaceTurrentAutoShoot extends AbstractControl {
    
    private final CharacterControl player;
    private final RigidBodyControl spaceTurrent;
    private final Node rootNode;
    private final AssetManager assetManager;
    private final BulletAppState physics;
    private final Spatial turrentNode;
    
    private float timeStack=0.0f;
    
  
    
    /**
     * An abstract Control that calls the auto-shooting (have a reassignable methods"controlUpdate(tpf)"
     * @param rootNode
     * @param assetManager
     * @param physics
     * @param player
     * @param spaceTurrent
     * @param turrentNode 
     */
    public SpaceTurrentAutoShoot(Node rootNode,AssetManager assetManager,BulletAppState physics,CharacterControl player, RigidBodyControl spaceTurrent,Spatial turrentNode) {
        this.rootNode=rootNode;
        this.assetManager=assetManager;
        this.physics=physics;
        this.player=player;
        this.spaceTurrent=spaceTurrent;
        this.turrentNode=turrentNode;
        
    }

    @Override
    protected void controlUpdate(float tpf) {        
         
            if(spaceTurrent.getPhysicsLocation().distance(player.getPhysicsLocation()) <= 100){
                    //setting the warning for your spaceShip
                GameApplication.dangertext.setText("Danger");
                GameApplication.xpText.setText("Approaching Galactic Turrent : "+turrentNode.getName());
                
                timeStack+=tpf;
                
                    if(timeStack >= 0.5f ){          
                       /**
                        * Load Laser Guns starting from the NPC & pointing to the player with speed of 100f
                        */
                      Load_LaserGun(new Sphere(10, 10, 0.2f),"man_smoke_1.png",spaceTurrent.getPhysicsLocation()
                            ,Vector3f.ZERO,0.2f,0.15f,player.getPhysicsLocation().subtract(spaceTurrent.getPhysicsLocation()).normalize().mult(100f));
                      /**
                       * Rotate the NPC to face the player
                       */
                      rotateNPCTOFacePlayer(player.getPhysicsLocation(),Vector3f.UNIT_Y);
                      /**
                       * create an auidoNode & attaches it to the rootNode
                       */
                      new SoundEffects(assetManager, rootNode,"galacticGunShoots.wav", false).play();
                            /**
                             * Reset the coolDown time
                             */
                            timeStack=0.0f;

                     }
           }else{
                    /**
                     * Reset the rotation of the NPC
                     */
                    rotateNPCTOFacePlayer(new Vector3f(0f,0f,0f),Vector3f.UNIT_Y);

                 }
    }
    
    
     public void Load_LaserGun(Mesh mesh,String Tex,Vector3f startingPosition,Vector3f gravity,float startSizeOfEffect,float endSizeOfEffect,
            Vector3f directionOfShooting ) {
           
                /** Create a Laser Gun geometry and attach to scene graph. new Box(0.01f, 0.03f, 0.01f)*/
                Geometry laser_geo = new Geometry("Space Turrent Laser",mesh);
                Material lasermat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
                Texture monkeyTex = assetManager.loadTexture("Textures/"+Tex);
                lasermat.setTexture("ColorMap", monkeyTex);
                laser_geo.setMaterial(lasermat);
                Node LaserGunNode=new Node("Space Turrent Laser");
                LaserGunNode.attachChild(laser_geo);
                LaserGunNode.attachChild(effect(startSizeOfEffect, endSizeOfEffect, ColorRGBA.Red, ColorRGBA.Yellow));
                /** Position Laser gun above the camera location by 1f &  to the right by 0.008f & forward +0.2f */
                //   player.getPhysicsLocation().add(new Vector3f(1f,1f,0f))
                LaserGunNode.setLocalTranslation(startingPosition);
                /** Make the laser gun particles physical with a mass > 0.0f */
                RigidBodyControl ball_phy = new RigidBodyControl(CollisionShapeFactory.createDynamicMeshShape(laser_geo),0.1f);
                /** Add physical ball to physics space. */
                LaserGunNode.addControl(ball_phy);
                //add rock decay conrtol
                LaserGunNode.addControl(new LaserGunDecayControl(GameApplication.player,physics));
                physics.getPhysicsSpace().add(ball_phy);
                /** setting the gravity of the shoots to zero in all directions x,y,z */
                ball_phy.setGravity(gravity);
                /** Accelerate the physcial ball to shoot it in the direction of camera "see Vector3f multiplications" */
                ball_phy.setLinearVelocity(directionOfShooting);
                /**attach the laser shoots to our node */
                rootNode.attachChild(LaserGunNode);

               
    }

    /**
     * Create a Particle EMitter
     * @param startSize of emitter
     * @param Endsize of emitter
     * @param startColor of emitter
     * @param endcolor of emitter
     * @return ParticleEmitter
     */
     public ParticleEmitter effect(float startSize,float Endsize,ColorRGBA startColor,ColorRGBA endcolor){

         ParticleEmitter fireEffect = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 300);
           Material fireMat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
//           fireMat.setTexture("Texture", assetManager.loadTexture("Textures/Fire.png"));
           fireEffect.setMaterial(fireMat);
           fireEffect.setImagesX(1); 
           fireEffect.setImagesY(1); // 2x2 texture animation
           fireEffect.setEndColor(endcolor );   // red
           fireEffect.setStartColor( startColor ); // yellow
           fireEffect.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 0, 0));
           fireEffect.setStartSize(startSize);
           fireEffect.setEndSize(Endsize);
           fireEffect.setLowLife(0.5f);
           fireEffect.setHighLife(1f);
           fireEffect.getParticleInfluencer().setVelocityVariation(3f);

        return fireEffect;
    }
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {

    }

    /**
     * Turns the NPC physics viewDirection to face the player
     * @param NPClookPosition Look direction vector  
     * @param upVector the Y-axis prependicular line
     */
    private void rotateNPCTOFacePlayer(Vector3f NPClookPosition,Vector3f upVector) {
          turrentNode.lookAt(NPClookPosition, upVector);
          spaceTurrent.setPhysicsRotation(turrentNode.getLocalRotation());
    }
    
}

[EDIT] :

2 Likes