2016-06-10 40 views
0

我有這樣的代碼:變換中點與可變規模

var Camera = function() { 
    this.x; 
    this.y; 
    this.scale = 1.0; 
    this.update = function(target, canvasWidth, canvasHeight, worldWidth, worldHeight) { 
    this.scale = target.originalWidth/target.width; 
    this.x = this.clamp(target.x - (canvasWidth/2) * this.scale, 0, worldWidth - canvasWidth); 
    this.y = this.clamp(target.y - (canvasHeight/2) * this.scale, 0, worldHeight - canvasHeight); 
    } 
    this.clamp = function(value, min, max) { 
    if (value < min) return min; 
    else if (value > max) return max; 
    return value; 
    } 
} 

它所做的是它使相機跟隨目標。它工作得很好,但如果比例發生變化,它就會離開該位置(更多並且離開屏幕中心)。

問題是,如何計算攝像機在尺度上的x和y?

回答

0

好了,所以與實驗,我發現,如果我這樣做:

this.x = this.clamp(target.x * this.scale - (canvasWidth/2), 0, worldWidth - canvasWidth); 
this.y = this.clamp(target.y * this.scale - (canvasHeight/2), 0, worldHeight - canvasHeight); 

它工作得很好:)