敌人精灵朝着玩家走了一条奇怪的道路
Enemy sprite takes a strange path towards the player
我试图让我的画布游戏中的精灵不断向玩家移动,直到它发生碰撞。相关的函数是update()
函数:
Enemy.prototype.update = function(playerX, playerY) {
// Rotate the enemy to face the player
this.rotation = Math.atan2(this.y - playerY, this.x - playerX) - 2.35;
// Move in the direction we're facing
this.x += Math.sin(this.rotation) * this.speed;
this.y += Math.cos(this.rotation) * this.speed;
}
this.x
、this.y
、this.rotation
、this.speed
分别为敌人的X位置、Y位置、旋转和速度。
这是可行的,但是敌人离玩家大约300像素,然后开始转向左边并远离玩家,与它朝向玩家的方向成90度角。
由于这有点难以解释,我录制了一个快速视频来帮助展示问题:http://www.screenr.com/AGz7
敌人是橙色精灵,玩家是白色精灵。
我所做的让敌人向玩家移动的计算有什么问题?
从之前编写的角度/移动代码来看,这些可能是错误的:
代替this.rotation = Math.atan2(this.y - playerY, this.x - playerX) - 2.35;
this.rotation = Math.atan2(playerY - this.y, playerX - this.x);
给你正确的旋转?
推理:不要使用神奇的常数,试着找出为什么你的公式是错误的。
不是this.x += Math.sin(this.rotation) * this.speed;
this.y += Math.cos(this.rotation) * this.speed;
试
this.x += Math.cos(this.rotation) * this.speed;
this.y += Math.sin(this.rotation) * this.speed;
推理:如果你的角度是0 =基于东的(如果你使用数学库函数,它们是默认的),那么对于一个角度0,你想要最大的水平移动而不是垂直移动- cos(0) = 1和sin(0) = 0。
相关文章:
- 在地图上画一条路线
- 在d3中拖动一条线
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- 我正在创建一个聊天,但每次我发送消息时,它都不会让我再发送另一条消息,就像表格一样;不起作用
- 当我的条件为true时,显示一条弹出消息
- 如何在javascript中的if或else块中运行一条jsp语句
- 加载页面每10秒显示一条不同的消息
- 更改路线时,在react.js中得到一条非常crypric的消息
- 当用户单击保存按钮时,标签会显示一条消息
- 仅显示一条通用消息,而不是每个输入显示一条
- 在检索到最后一条记录后从 PostgreSQL 中提取记录
- 有没有一条清晰的路径,我可以实现一个在 ScalaJS + React 中广泛使用上下文的 React 组件
- 如果用户的浏览器早于以下版本,则显示一条消息:IE 10、Firefox 39、Chrome 39、Opera 8
- 子网格:限制用户仅选择一条记录
- 删除高图中的第一条和最后一条网格线
- 我的代码给了我一条错误消息:GetElementById()为null或不是对象
- 我可以用jQuery画一条线吗
- Highcharts为BoxPlot图表添加了一条Mean线
- 当最后一条消息溢出时删除它
- 敌人精灵朝着玩家走了一条奇怪的道路