2016-06-10 2 views
0

Я получил этот код:Transform средней точки с переменным масштабом

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); 

Он работает просто отлично :)

Смежные вопросы