-1
\$\begingroup\$

Please check out my game here: http://still-escarpment-3701.herokuapp.com

The problem is with shooting. The bullets keep on moving with the First person controller instead of moving from the same position and direction from which they were shot.

///////////////////code for shooting from fps///////////////////////////////////
         ///////////////////////////////////////////////////////////////////////////////////
        // the bullet to be thrown and shoot ......

        bulletTemplate = new J3D.Transform();
        bulletTemplate.geometry = J3D.Primitive.Sphere(0.5, 4, 4);
        bulletTemplate.renderer = J3D.BuiltinShaders.fetch("Normal2Color");


        // bullet holder bullets ko apni gun main rakhe ga ....
        bulletHolder = new J3D.Transform();


         engine.scene.add(bulletHolder);
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////

document.onmousemove = onMouseMove;

        document.onmouseup = function(e) {
        var b = bulletTemplate.clone();

            var fz = first_person_controller.forward();
            b.direction = new v3();
            b.direction.fromArray(fz);

            b.position.fromArray(fz).mult(3);
            b.progress = 0;
            b.ttl = 300;

            b.animate = function() {
                b.position = b.direction.cp().mult(3 + b.progress);
                b.progress += 1;
                b.ttl--;



                if(b.ttl == 0) {
                    bulletHolder.remove(b);
                }
            }

            bulletHolder.add(b);

        };

The code below is being called in the game loop function's draw().

bulletHolder.position.x =first_person_controller.position.x;
bulletHolder.position.y =first_person_controller.position.y;
bulletHolder.position.z =first_person_controller.position.z;

first_person_controller.rotation.y = mx;

for(var i = 0; i < bulletHolder.numChildren; i++) {
    bulletHolder.childAt(i).animate();
}
\$\endgroup\$
0
\$\begingroup\$

Assuming you are using J3D.js.

Add your bullets to the bulletHolder at the position of the first_person_controller.

// Instead of:
b.position.fromArray(fz).mult(3);
// Do:
b.position.fromArray(first_person_controller.position);
// Or, if you want the bullet to start a few units in front of the player:
b.position = v3.add(first_person_controller.position, fz.mul(3));

And don't move your bulletHolder around in draw(). Remove these lines:

bulletHolder.position.x =first_person_controller.position.x;
bulletHolder.position.y =first_person_controller.position.y;
bulletHolder.position.z =first_person_controller.position.z;

You'll save yourself a lot of time if you keep each scene graph "holder" object like this at the world coordinate system origin.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.