热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AS3-Y速度6.123031769111886E-17-AS3-YVelocity6.123031769111886E-17

Whengiven0,0to0,5,theyvelocitybecomesthatnumberandbreaksmycode.IknowImusthavedon

When given 0,0 to 0,5, the y velocity becomes that number and breaks my code. I know I must have done something wrong as I just copy and pasted code (since I am horrible at maths)..

当给定0 0到0 5时,y速度就变成了这个数并破坏了我的代码。我知道我一定做错了什么,因为我只是复制和粘贴代码(因为我数学不好)。

This is how I calculate the numbers:

这就是我计算数字的方法:

var radian = Math.atan2(listOfNodes[j].y - listOfNodes[i].y,listOfNodes[j].x - listOfNodes[i].x);
var vy = Math.cos(radian);
var vx = Math.sin(radian);

Thanks

谢谢

1 个解决方案

#1


5  

There i am assuming the velocity vector is FROM 0,0 TO 0,5. And 0,0 is i and 0,5 is j.

我假设速度向量是从0到0 5。0 0是i 5是j。

In that case the velocity vector is only along y and the y component should be 5 and x component 0. It is coming as opposite because,

在这种情况下,速度矢量只沿y方向,y分量应该是5,x分量是0。因为,

cos(radian) whould be x velocity component and sin(radian) the y compunent.

因为(radian)应该是x速度分量,sin(radian)是y的compunent。

And the number 6.123031769111886E-17 is actually returned in place of 0.

而数字6.123031769111886E-17实际上返回的是0。

Look at the following figure:
enter image description here

请看下图:

Also as can be seen from the figure you do not need the trigonometric computations at all.
You can simply get the x and y components as follows:

同样,从图中可以看出,你根本不需要三角计算。你可以简单地得到x和y分量如下:

// y2 - y1
var vy = listOfNodes[j].y - listOfNodes[i].y;
// x2 - x1
var vx = listOfNodes[j].x - listOfNodes[i].x;

This will avoid the floating point inaccuracy caused by the trig finctions due to which you are seeing 6.123031769111886E-17 instead of 0.
You only need to use atan2 if you actually need the angle θ in your code.

这将避免由trig finctions引起的浮点不准确,因为您看到的是6.123031769111886E-17而不是0。你只需要使用量化如果你真的需要角θ在您的代码中。

Update: Well if you need only unit (normalized) vector's components you can divide the vx and vy with the length of the original vector. Like this:

更新:如果你只需要单位(归一化)向量的分量,你可以用原向量的长度除以vx和vy。是这样的:

// y2 - y1
var vy = listOfNodes[j].y - listOfNodes[i].y;
// x2 - x1
var vx = listOfNodes[j].x - listOfNodes[i].x;
// vector magnitude
var mag = Math.sqrt(vx * vx + vy * vy);

// get unit vector components
vy /= mag;
vx /= mag;

Using the above you will get the exactly the same results as you are getting from trig sin and cos functions.

用上面的方法你会得到和三角函数sin和cos函数一样的结果。

But if you still need to use the original code and want to make 6.12...E-17 compare to 0, you can use the epsilon technique for comparing floats. So you can compare any value within epsilon's range from 0, using flllowing code:

但是,如果您仍然需要使用原始代码,并希望制作6.12……E-17与0相比,你可以使用epsilon技术来比较浮点数。因此,你可以使用flllowing代码来比较epsilon范围内的任何值:

function floatCompare(a:Number, b:Number, epsilon:Number):Boolean{
    return (a >= (b - epsilon) && a <= (b + epsilon));
}
// To check for zero use this code, here i'm using 0.0001 as epsilon
if(floatCompare(vx, 0, 0.0001)){
    // code here
}

So any deviation in the range of [b-epsilon, b+epsilon] would successfully compare to b. This is essential in case of floating point arithmetic.

因此,在[b, b+]范围内的任何偏差都可以成功地与b进行比较,这在浮点运算中是很重要的。


推荐阅读
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
author-avatar
mobiledu2502935431
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有