Hi. Why is the lengthSq()
method used in the update method instead of the length()
method in this project? If you look at the logic, there is no difference for calculating the new position between the lengthSq()
method and the length()
method. I don’t understand why the lengthSq()
method is needed. Does it work faster?
// update code called every frame
PointAndClick.prototype.update = function(dt) {
if (this.direction.lengthSq() > 0) {
// Move in the direction at a set speed
var d = this.playerSpeed * dt;
var newPosition = PointAndClick.newPosition;
newPosition.copy(this.direction).scale(d);
newPosition.add(this.playerEntity.getPosition());
this.playerEntity.setPosition(newPosition);
this.distanceToTravel -= d;
// If we have reached our destination, clamp the position
// and reset the direction
if (this.distanceToTravel <= 0) {
this.playerEntity.setPosition(this.targetPosition);
this.direction.set(0, 0, 0);
}
}
};