【发布时间】:2013-01-31 03:38:45
【问题描述】:
我的实体应该朝着鼠标直线移动。它很接近,但还没有完全到那里。这是working demo 向您展示我的意思。
这是一个屏幕截图: 红色代表鼠标经过的路径。如您所见,实体没有走相同的路径。
相关代码:
EntityPlayer = ig.Entity.extend({
movementspeed: 400,
update: function() {
this.parent();
this.move_toward_coord(ig.input.mouse.x, ig.input.mouse.y);
},
move_toward_coord: function(x, y) {
var distance_to_target_x = x - this.pos.x - this.size.x / 2;
var distance_to_target_y = y - this.pos.y - this.size.y / 2;
if(Math.abs(distance_to_target_x) > 1 || Math.abs(distance_to_target_y) > 1) {
this.vel.x = (distance_to_target_x > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_x) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
this.vel.y = (distance_to_target_y > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_y) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
} else {
this.vel.y = 0;
this.vel.x = 0;
}
}
});
我怀疑move_to_coord 方法有问题,但是在调整了太多小时后,我仍然不确定它是什么......
为什么船不是直线行驶?
【问题讨论】:
标签: javascript math geometry game-engine impactjs