Jump to content

Shooting


Bricktheworld
 Share

Recommended Posts

Hello there!

I am trying to make a 3rd person plane shooter using two models. What is the best way to make the Lazer model shoot out of the X-Wing model?

Here is the code:

var mouseX = 0;
var mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var XWing;
var XWings = [];
var Tie;
var Ties = [];
var Lazerorig;
var Lazers = [];
var fire = false;
window.addEventListener('DOMContentLoaded', function() {
  var canvas = document.getElementById('canvas');
  var engine = new BABYLON.Engine(canvas, true);
  engine.enableOfflineSupport = false;
  var domeRadius = 4000;
  var randomNumber = function(min, max) {
    if (min === max) {
      return (min);
    }
    var random = Math.random();
    return ((random * (max - min)) + min);
  };

  // Wingy's default positions
  var myStartPositionFunction = function(worldMatrix, positionToUpdate) {
    var v3 = getCart(domeRadius);
    BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(v3.x, v3.y, v3.z, worldMatrix, positionToUpdate);
  };

  // -------------------------------------------------------------------------------------------------
  var myUpdateFunction = function(particles) {
    for (var index = 0; index < particles.length; index++) {
      var particle = particles[index];
      particle.age += this._scaledUpdateSpeed;
      if (particle.age >= particle.lifeTime) {
        this.recycleParticle(particle);
        index--;
        continue;
      } else {

        particle.color = new BABYLON.Color3.White();
        particle.size = randomNumber(this.minSize, this.maxSize);

        if (particle.color.a < 0)
          particle.color.a = 0;
      }
    }
  };

  // -----------------------------------------------------------------------
  // a gruesome stolen func - thx to...
  // https://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/
  function getCart(radius) {
    // var lat = DegtoRad (latlong.Latitude);
    // var lon = DegtoRad (latlong.Longitude);
    var xy = plot1(); // just below
    var lat = xy[0];
    var lon = xy[1];
    var x = radius * Math.cos(lat) * Math.cos(lon);
    var y = radius * Math.cos(lat) * Math.sin(lon);
    var z = radius * Math.sin(lat);
    return new BABYLON.Vector3(x, y, z);
  }

  // -----------------------------------------------------------------------
  // a stolen formula - claims to eliminate polar bias (clustering near poles)
  // in use - thx to http://rectangleworld.com/blog/archives/298
  var plot1 = function() {
    var theta = Math.random() * 2 * Math.PI;
    var phi = Math.acos(Math.random() * 2 - 1);
    return [theta, phi];
  };

  // -----------------------------------------------------------------------
  var doubleColor4 = function(min, max) {
    return new BABYLON.Color4(Math.random() * 2, Math.random() * 2, Math.random() * 2, Math.random());
  }
  // -----------------------------------------------------------------------

  var createScene = function() {
    var scene = new BABYLON.Scene(engine);
    scene.clearColor = new BABYLON.Color3.Black();
    var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 0, 0), scene);
    var box1 = BABYLON.Mesh.CreateBox("box1", 25, scene);
    var box2 = BABYLON.Mesh.CreateBox("box2", 25, scene);
    //var cylinder = BABYLON.Mesh.CreateCylinder("cylinder", )
    box1.material = new BABYLON.StandardMaterial("b1mat", scene);
    box1.material.emissiveColor = BABYLON.Color3.White();
    box2.material = new BABYLON.StandardMaterial("b2mat", scene);
    box2.material.emissiveColor = BABYLON.Color3.White();
    box1.position.z = -100
    box2.parent = box1;
    box2.setPivotMatrix(BABYLON.Matrix.Translation(0, 10, -25));
    box1.visibility = 0;
    var box3 = BABYLON.Mesh.CreateBox("Box3", 25, scene);
    var material = new BABYLON.StandardMaterial("material1", scene);
    material.wireframe = true;
    box3.material = material;
    // BABYLON.SceneLoader.ImportMesh("", "Models/", "Tie.babylon",
    //   scene,
    //   function(newMeshes) {
    //     Tie = newMeshes[0];
    //     Tie.parent = box1;
    //
    //   });

    BABYLON.SceneLoader.ImportMesh("XWing", "Models/", "XWing.babylon",
      scene,
      function(newMeshes) {
        XWing = newMeshes[0];
        XWing.parent = box1;

      });
    BABYLON.SceneLoader.ImportMesh("Lazers", "Models/", "XWingLazers.babylon",
      scene,
      function(newMeshes) {
        Lazerorig = newMeshes[0];
        //Lazerorig.parent = box1;

        Lazerorig.visibility = 0;
      });

    var camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 0, -55), scene);
    scene.camera = camera;
    document.addEventListener('mousemove', onDocumentMouseMove, false);
    document.addEventListener('mousedown', function(){
    }, false);
    document.addEventListener('mouseup', onDocumentMouseUp, false);

    //camera.parent = box2;
    camera.target = box1;
    box2.visibility = 0;


    var ps = new BABYLON.ParticleSystem("particles", 1300, scene);
    ps.startPositionFunction = myStartPositionFunction;
    ps.updateFunction = myUpdateFunction;

    // Texture of each particle - set far below
    ps.particleTexture = new BABYLON.Texture("Models/star.png", scene);
    //ps.particleTexture = new BABYLON.Texture("https://cdn.rawgit.com/wingnutt/misc/master/star.jpg", scene);

    // Where the particles come from
    ps.emitter = new BABYLON.Vector3(0, 15, 0);

    ps.minEmitBox = new BABYLON.Vector3(0, 0, 0); // Starting all from
    ps.maxEmitBox = new BABYLON.Vector3(0, 0, 0); // To...


    // the sizing.
    ps.minSize = 20;
    ps.maxSize = 30;

    // Life time of each particle (random between...
    ps.minLifeTime = 40000.0;
    ps.maxLifeTime = 50000.0;

    // Emission rate
    ps.emitRate = 5000;

    // Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
    ps.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;

    // Angular speed, in radians
    ps.minAngularSpeed = 0;
    // ps.maxAngularSpeed = Math.PI*2;
    ps.maxAngularSpeed = 0;

    // Speed
    ps.minEmitPower = 0;
    ps.maxEmitPower = 0;
    ps.updateSpeed = 0.005;

    // Start the particle system
    ps.start();


    return scene;
  }
 
  function onDocumentMouseMove(event) {
    mouseX = (event.clientX - windowHalfX);
    mouseY = (event.clientY - windowHalfY);
  }

  function onDocumentMouseDown() {
    
  }

  function onDocumentMouseUp(event) {
    fire = false;
  }

  var scene = createScene();
  engine.runRenderLoop(function() {
    scene.getMeshByName("box1").rotate(BABYLON.Axis.Y, mouseX * 0.00007, BABYLON.Space.LOCAL);
    scene.getMeshByName("box1").rotate(BABYLON.Axis.X, mouseY * 0.0001, BABYLON.Space.LOCAL);

    scene.getMeshByName("box1").translate(BABYLON.Axis.Z, 6, BABYLON.Space.LOCAL);
   

    scene.render();
  });
});
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...