简体   繁体   English

HTML5 - JS:从面向方向射击子弹

[英]HTML5 - JS: Shoot Bullet From Facing Direction

I'm working on a top down shooter HTML5 game. 我正在制作一款自上而下的射击游戏HTML5游戏。 I'm currently trying to get bullets to fire from my characters facing angle. 我现在正试图从面对角度的角色中射出子弹。

My character always faces the mouse position (rotates). 我的角色总是面向鼠标位置(旋转)。 So, when I fire a bullet, I need to take the rotation into account. 所以,当我发射子弹时,我需要考虑轮换。

I'm almost there, the only problem is that the bullets initiate where my actual mouse position is, although - it moves in the correct facing direction. 我几乎就在那里,唯一的问题是子弹开始于我的实际鼠标位置,尽管 - 它以正确的方向移动。

I believe my math is off within my velocity variable: 我相信我的数学速度在我的速度变量范围内:

var b = new Rectangle( this.x + (this.width / 2) - 4, this.y + (this.height / 2) - 4, 8, 8);

var velocityInstance = new Vector2(0, 0);
velocityInstance.x = input.mouseX - (this.localX + this.width/2);
velocityInstance.y = input.mouseY - (this.localY + this.height/2);

var bulletInstance = new Bullet(velocityInstance, b, this.bullets.length + 1);

this.bullets.push(bulletInstance);
this.shotBullet = true;

'this' refers to my Player. 'this'指的是我的播放器。 localX/Y is the centre position of my character (he's always in the center of the screen, and the scene moves around him). localX / Y是我角色的中心位置(他总是在屏幕的中心,场景在他周围移动)。

Would appreciate if someone could check this for me. 如果有人可以帮我查一下,我将不胜感激。 Thanks! 谢谢!

------ EDIT ------ ------编辑------

Here is my Bullet function: 这是我的子弹功能:

Bullet = function(vel, rectangle, index){

    this.velocity = vel;
    this.rect = rectangle;
    this.index = index;

    this.Update = function(){
        this.rect.x += this.velocity.x;
        this.rect.y += this.velocity.y;

        //Collisions
        for(var c = 0; c < GameObjects.length; c++){
            if(this.rect.Intersects(GameObjects[c])){
                console.debug("Bullet Hit: " + GameObjects[c].tag);

                //Player.bullets.splice(index, 1);
            }
        }
    };

    this.Draw = function(ctx){

        this.rect.Draw(ctxMap);
    };

};

Actually, I would recommend not using trig functions, simply for efficiency. 实际上,我建议不要使用trig功能,只是为了提高效率。 Math.atan2, Math.cos, and Math.sin can get expensive. Math.atan2,Math.cos和Math.sin可能会变得昂贵。

You already have (slightly renamed) 你已经(稍微重命名)

var deltaX = input.mouseX - (this.localX + this.width/2);
var deltaY = input.mouseY - (this.localY + this.height/2);

Why not simply jump this way? 为什么不简单地这样跳?

var magnitude = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
var velocityScale = bulletSpeed / magnitude;
velocityInstance.x = deltaX * velocityScale;
velocityInstance.y = deltaY * velocityScale;

Its functionally the same, but uses only one Taylor series instead of 3, so should be much more efficient. 它的功能相同,但只使用一个泰勒系列而不是3个,所以应该效率更高。 Faster is better, right? 越快越好,对吧?

Figured it out. 弄清楚了。 I needed to use Math.cos and Math.sin: 我需要使用Math.cos和Math.sin:

var targetX = input.mouseX - (this.localX + this.width/2);
var targetY = input.mouseY - (this.localY + this.height/2);

this.rotation = Math.atan2(targetY, targetX);

velocityInstance.x = Math.cos(this.rotation) * bulletSpeed;
velocityInstance.y = Math.sin(this.rotation) * bulletSpeed;

Thanks anyway community! 非常感谢社区! Helped me get my head around it! 帮助我解决它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM