- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Javascript 制作 HTML5 Canvas 游戏。
我想让一个物体平滑地转向某个方向。
我将方向存储为变量,并使用弧度。代码的工作原理如下:
window.setInterval(loop,25);
var dir = 0;
var targetDir = Math.PI/2;
function loop() {
dir += (targetDir - dir) / 5;
document.getElementById('text').innerHTML = dir; //To display the direction
}
<p id='text'></p>
它在大多数情况下都有效,但如果方向和目标方向在相反的左侧/右侧,计算机将假设到达目标的最快方法是一路绕过去,如下所示: (Image)
有人知道我可以使用一种算法来使其正常工作吗?
最佳答案
希望有人能给出一个优雅的解决方案来回答,但目前还没有,所以这就是我的做法。
我一直觉得这个问题的解决方案很不优雅,也就是说,我不知道是否有更好的方法,我也没有努力寻找。下面的两个函数将返回从函数 Math.atan2(yDif, xDif)
派生的两个 Angular 之间的最短方向,该函数返回 -Math.PI
范围内的 Angular > 到 Math.PI
获取最短方向。
// returns true if shortest direction is clockwise else false
function isShortestDirClockwise(ang1, ang2){
if (ang1 < 0) {
if ( (ang2 < 0 && ang1 > ang2) || (ang2 >= 0 && ang1 + Math.PI < ang2) ) {
return false;
}
} else {
if ( (ang2 > 0 && ang1 > ang2) || (ang2 <= 0 && ang1 - Math.PI < ang2) ) {
return false;
}
}
return true;
}
获取最短 Angular
// returns the shortest angle neg angles are counterClockwise positive are clockwise
function getShortestAngle(ang1, ang2){
var cw = true; // clockwise
var ang;
if (ang1 < 0) {
if( (ang2 < 0 && ang1 > ang2) || (ang2 >= 0 && ang1 + Math.PI < ang2) ) {
cw = false;
}
} else {
if ( (ang2 > 0 && ang1 > ang2) || (ang2 <= 0 && ang1 - Math.PI < ang2) ) {
cw = false;
}
}
if (cw) {
var ang = ang2 - ang1;
if (ang < 0) {
ang = ang2 + Math.PI * 2 - ang1;
}
return ang;
}
var ang = ang1 - ang2;
if (ang < 0) {
ang = ang1 + Math.PI * 2 - ang2;
}
return -ang;
}
由于我讨厌使用这些函数,所以我更喜欢下面稍微复杂但更优雅的解决方案。这会找到一组 3 点的最短 Angular 。中心点,以及我希望找到之间 Angular 两点,它总是返回任何两条线之间的最短 Angular 。
// Warning may return NAN if there is no solution (ie one or both points (p1,p2) are at center)
// Also uses Math.hypot check browser compatibility if you wish to use this function or use Math.sqrt(v1.x * v1.x + v1.y * v1.y) instead
function getAngleBetween(center, p1, p2){
var d;
var ang;
// get vectors from center to both points
var v1 = { x : p1.x - center.x, y : p1.y - center.y};
var v2 = { x : p2.x - center.x, y : p2.y - center.y};
// normalise both vectors
d = Math.hypot(v1.x, v1.y);
v1.x /= d;
v1.y /= d;
d = Math.hypot(v2.x, v2.y);
v2.x /= d;
v2.y /= d;
// cross product gets the angle in range -Math.PI / 2 to Math.PI / 2
ang = Math.asin(v1.x * v2.y - v1.y * v2.x);
// use the cross product of the line perpendicular to the first to find the quadrant
if(-v1.y * v2.y - v1.x * v2.x > 0){
if(ang < 0){
ang = -Math.PI - ang;
}else{
ang = Math.PI - ang;
}
}
return ang;
}
关于javascript - 平滑 'turning'脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37780814/
我一直在探索 Mapbox Map SDK 和 Navigation SDK,但遇到了瓶颈,如有任何帮助,我们将不胜感激。 我在 GeoJSON 中定义了一些路由。是否可以将 GeoJSON 中的这条
我在 C 中有两个静态可变变量,我想在逻辑语句中检查它们。但是,当我这样做时,我收到警告“未定义的行为:此语句 1037 中未定义 volatile 访问的顺序”是否有可能在很短的时间内暂停 C 变量
有没有像 googlemaps 应用程序和 mapkit 那样做 turn-by-turn mapping 的方法?就像下面的图片。谢谢 最佳答案 我猜你想在你的应用程序中使用精细导航?在 MapKi
我正在构建一个 Android 应用程序,帮助用户在两点之间导航。这是 doc 的链接.这正是我想要提供给用户的功能,但是我希望导航在我的应用程序中进行,而不是通过以这种方式启动 Intent Uri
我正在开发从当前位置到目的地的转弯导航。我使用以下代码完成了此操作 Uri gmmIntentUri = Uri.parse("google.navigation:q="+address); Inte
假设我有一个控制 android 服务的开关:当Switch处于“on”状态时,表示服务正在运行,当开关处于“关闭”位置时,表示服务未运行。此外,当用户点击 Switch 时,它应该切换服务(因此切换
我正在尝试实现一种算法来填充一个区域,该特定算法在以下 Wikipedia 中给出“固定内存法(右手填充法)”标题下的文档。下面给出算法中发生左转和右转的特定部分... START: set
我正在使用 HERE NOKIA SDK。我已经在 2 点和语音指令之间创建了一条路线。现在我想显示图像箭头和操作的特定文本。 最佳答案 SDK不提供回合图标。请创建一些图像以将它们与机动转弯类型配对
我的应用程序需要像 OLA、UBER 和出租车出租应用程序一样包含转弯 GPS 导航。我使用 Google Direction API 来显示路径和持续时间。我检查了以下链接,但对我没有帮助。 gps
我的应用需要包含转弯 gps 导航。任何人都可以告诉如何通过转弯 gps 导航集成谷歌地图。 我可以在android开发中找到它。但是我无法从谷歌地图的 iOS 开发文档中找到导航。 https://
构建时出现以下错误: ...has undefined behavior [-Werror,-Wundefined-reinterpret-cast] Bazel 构建完全停止,因为 clang (l
如何关闭 Rmd HTML 文档中的标题? --- title: "" output: html_document --- rmarkdown::render("index.Rmd") [WARNIN
我试图使用 部署一个简单的 TURN 服务器coturn . 当我在 Trickle ICE 上测试时( turn:rtc.jackxujh.me:3478 [webrtc:mighty] ),Tri
我正在使用一个任性的库,不幸的是,它会将信息打印到 System.out(或偶尔打印到 System.err)。防止这种情况的最简单方法是什么? 我一直在考虑创建一个输出流到内存,在每次调用其中一个麻
比如说我搭建了WebRTC视频聊天网站,有的握手后连接(ICE Candidates)会直接p2p,有的会使用STUN服务器,有的会使用“不得已”的TURN服务器建立连接.与直接连接和 STUN 连接
我在查找有关如何“打开”mysqli 的确切文档时遇到了麻烦。我正在运行 OS X SL,据我所知,由于安装了 php5,mysqli 扩展也应该已经存在。 这是否像在 php.ini 中添加 Loa
在我的 Ubuntu 引擎上设计了一个简单的基于 shell/bash 的备份脚本并使其工作后,我将它上传到了我的 Debian 服务器,它在执行它时会输出一些错误。 我该怎么做才能在我的 Ubunt
我正在使用 Twilio Network Traversal Service作为 native 应用程序的一部分,我是 working on执行对等远程桌面连接。我们实现了 WebRTC 协议(pro
我正在尝试将负载均衡器放在 Turn 服务器前面以与 WebRTC 一起使用。我在下面的示例中使用了一个轮流服务器,直到负载平衡器正常工作。 Turn 服务器需要多个端口,包括一个 UDP,如下所示:
在哪些情况下我应该使用 TURN 服务器? 现在我有一个完美运行的 WebRTC 应用程序。该应用程序即将发布。 我是否需要设置自己的 TURN 服务器,或者这可能仅适用于主要应用程序? 最佳答案 已
我是一名优秀的程序员,十分优秀!