Jump to content

How to set the direction for bullets


Recommended Posts

I want to scope after clicking the mouse flew in the direction of movement of the camera, thus emulated shot.

Possible example on three.js:    http://pauluskp.com/cannot

My attempt to do so at Babylon:    http://www.babylonjs-playground.com/#VWXHP#1


Here is the basic code, then I created a sphere when clicked, and then later in


the set coordinates camera adding a constant but probably incorrectly specified coordinates.

window.addEventListener("click", function (e) {     var bullet = new BABYLON.Mesh.CreateSphere('bullet', 3, 0.3, scene);     var pos = camera.position;          bullet.position = new BABYLON.Vector3( pos.x, pos.y, pos.z);     bullet.material =  new BABYLON.StandardMaterial('texture1', scene);     bullet.material.diffuseColor = new BABYLON.Color3(3, 2, 0);     var alpha = 0;     scene.registerBeforeRender(function () {         bullet.position = new BABYLON.Vector3( alpha*pos.x, alpha*pos.y, alpha*pos.z );       alpha += 0.01;     });


Link to comment
Share on other sites

  • 2 weeks later...

I have another question. I'm trying to make a game for multiplayers on websocket and I have a problem with the motion vector bullets still appeared.

1) When clicked I call a function selfBullet  from class bullet and sent to the server the camera position and the player id.

window.addEventListener("click", function() {    var bul = new Bullet();    bul.selfBullet();    ws.send( JSON.stringify( { 'e':'shoot', 'id':player.id, 'pos':camera.position } )});

2) Server for all other players return these values and calls function remoteBullet from class bullet

ws.onmessage = function(event){    var bullet = new Bullet();    bullet.remoteBullet(msg.id, msg.pos );}

3) And then there is a problem. in function this.selfBullet  in a global variable is assigned to the direction

trueDirection = direction;

but there is a problem for all players bullet flies in the opposite direction from the direction stelby and how to modify the direction I do not have experience

var trueDirection;function Bullet(){    var bullet = new BABYLON.Mesh.CreateSphere('bullet', 3, 0.3, scene);    bullet.material = new BABYLON.StandardMaterial('texture1', scene);    bullet.material.diffuseColor = new BABYLON.Color3(3, 2, 0);    this.selfBullet = function(){         var startPos = camera.position;         bullet.position = new BABYLON.Vector3(startPos.x, startPos.y, startPos.z);         var invView = new BABYLON.Matrix();         camera.getViewMatrix().invertToRef(invView);         var direction = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, 1), invView);         direction.normalize();         trueDirection = direction;         scene.registerBeforeRender(function () {              bullet.position.addInPlace(direction);         });     };     this.remoteBullet = function(player, pos){          bullet.position = new BABYLON.Vector3(pos.x, pos.y, pos.z);          scene.registerBeforeRender(function () {              bullet.position.addInPlace(trueDirection);          });      }; }

example http://pauluskp.com/babylon
The video shows that when a player shoots the right then the left overlooking pine shoots in the opposite

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.

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.


  • Recently Browsing   0 members

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