【问题标题】:Move an object in the direction its facing using 2d html5Canvas and ctx.rotate function使用 2d html5Canvas 和 ctx.rotate 函数沿其朝向的方向移动对象
【发布时间】:2013-10-21 17:04:00
【问题描述】:

标题说明了一切,我试图根据物体所处的角度向前移动一个物体。

这是我的相关代码:

xView =  this.x-this.width/2;
yView =  this.y-this.height/2; 
playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, xView, yView, this.width, this.height); 
      playerCtx.save();     
      playerCtx.clearRect(0, 0, game.width, game.height);
      playerCtx.translate(xView, yView);
      playerCtx.rotate(angle *Math.PI/180);
      playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, -xView, -yView, this.width, this.height); 
      playerCtx.restore();
    }
        if(Game.controls.left) {
    angle-=1; 
        if(Game.controls.up){
        this.x +=   this.speed * Math.cos(angle * Math.PI / 180);
        this.y -=   this.speed * Math.sin(angle * Math.PI / 180);
        }

对象不移动对应var angle

编辑

我不知道为什么我的代码不起作用,所以我改用了一个包含 36 个不同角度的精灵表。这行得通,但是旋转太快了。如果有人能告诉我为什么以上不能正常工作,或者我将如何使以下功能变慢:

if(Game.controls.right) {
    currentFrame++;
    angle+=10;
 }

“慢”是指按住左键时,angle+10; currentFrame++; 会升高到快,添加更多帧可能需要很长时间。

编辑

为我原来的问题添加了一个Jfiddle,角度变量随着旋转而移动,例如如果对象面向右,角度将等于90,但对象仍然没有看起来它在向右移动,尽管相机确实如此。

【问题讨论】:

    标签: javascript canvas rotation html5-canvas 2d


    【解决方案1】:

    尝试改变 cos 和 sin 以及符号:

    this.x += this.speed * Math.cos(angle * Math.PI / 180);
    this.y += this.speed * Math.sin(angle * Math.PI / 180);
    

    为了使您的旋转(在问题的编辑部分),您需要基本上减少步骤并提供更多精灵。更多精灵不会变慢,但会使用更多内存并增加初始加载时间。

    更新

    好的,您需要更正一些事情(上面是其中之一,它们在小提琴中得到了更正)。

    Modified fiddle

    其他内容在:

    在您的update() 方法中:

    // Add both here as angle with correctly use neg/pos
    if (Game.controls.up) {
        this.x += this.speed * Math.cos(angle * Math.PI / 180);
        this.y += this.speed * Math.sin(angle * Math.PI / 180);
    }
    
    /// Sub both here as angle with correctly use neg/pos
    if (Game.controls.down) {
        this.x -= this.speed * Math.cos(angle * Math.PI / 180);
        this.y -= this.speed * Math.sin(angle * Math.PI / 180);
    }
    

    由于角度将确定负值或正值,您只需要根据意图添加或减去,因此向上添加两个值,向下减去两个值。角度将确保增量正确签名。

    在您的 draw 函数中有几件事:

    Player.prototype.draw = function (context, xView, yView) {
    
        // Add the half width instead of subtract it
        xView = this.x + this.width / 2;
        yView = this.y + this.height / 2;
    
        // not needed as you clear the canvas in the next step
        //playerCtx.drawImage(imgSprite, 0, 0, ....
        playerCtx.save();
        playerCtx.clearRect(0, 0, game.width, game.height);
    
        // make sure pivot is moved to center before rotation
        playerCtx.translate(xView, yView);
    
        // rotate, you should make new sprite where direction
        // points to the right. I'm add 90 here to compensate
        playerCtx.rotate((angle + 90) * Math.PI / 180);
    
        // translate back before drawing the sprite as we draw
        // from the corner and not the center of the image
        playerCtx.translate(-xView, -yView);
    
        playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, this.x, this.y, this.width, this.height);
    
        playerCtx.restore();
    }
    

    现在您将看到一切正常。

    您的图像应始终指向。这是因为那始终是 0 度角。这将为您省去一些麻烦。我通过将角度添加 90 度来补偿这是绘制功能,但这应该在精灵本身中进行调整。

    此外,我修改了您的密钥处理程序(有关详细信息,请参阅演示)。

    【讨论】:

    • 我已经换了它们,我仍然遇到同样的问题,还有没有其他方法可以减慢if(Game.controls.right) 的速度?向精灵表添加更多内容需要一段时间。顺便说一句,感谢您的快速回复。
    • @user2582299 你能设置一个小提琴来测试吗?这样更容易查明错误。
    • @user2582299 谢谢,小提琴有帮助。请查看更新后的答案,并且您的玩家现在可以正确移动。
    • 感谢您的帮助!画布没有跟随你的小提琴中的玩家,但我现在明白了。很棒的东西非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2020-02-22
    • 2017-01-11
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多