Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();
}
share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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