2016-06-10 16 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); 

它工作得很好:)