我正在尝试根据三角形边计算角度,最好是用 sin 计算。前 2 个是辅助getDistance函数getPointsDifference
我有这些功能:
var getDistance = function(p1, p2){
var dx = p1.x - p2.x, dy = p1.y - p2.y;
return Math.sqrt(dx*dx + dy*dy);
}
var getPointsDifference = function(p1, p2){
return {
x: -1 * (p1.x - p2.x),
y: (p1.y - p2.y)
}
}
Run Code Online (Sandbox Code Playgroud)
最后:
var getMenuChoice = function(cx,cy, x, y){
var distance = getDistance({x:cx,y:cy}, {x:x,y:y});
if (distance <= 100) {
console.log(1)
} else {
console.log(2)
}
var diff = getPointsDifference({x:cx,y:cy}, {x:x,y:y});
var a = …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作台球游戏,我想计算主球(白球)击中另一个球后的移动方向。
正如你所看到的,我想计算射线击中球的角度/方向以及射线投射将改变其方向的角度/方向。我需要将角度显示为 Vector3 变量,以便我可以在linerenderer(3) 上使用它。
我已经计算出被击中的球将移动的方向。
如果你能在这方面帮助我那就太好了!
当前代码:
RaycastHit hitz;
if (Physics.SphereCast(transform.position, 0.8f, location - transform.position, out hitz, Mathf.Infinity, lmm2))
{
lineRenderer2 = hitz.collider.GetComponentInChildren<LineRenderer>();
lineRenderer2.SetVertexCount(2);
if (!Input.GetKey(KeyCode.Mouse0))
lineRenderer2.SetPosition(0, hitz.point);
if (!Input.GetKey(KeyCode.Mouse0))
{
Vector3 start = hitz.point;
Vector3 end = start + (-hitz.normal * 4);
if (lineRenderer2)
{
if (!Input.GetKey(KeyCode.Mouse0))
lineRenderer2.SetPosition(1, end);
}
if(lineRenderer3)
{
anglelel = Vector3.Angle(hitz.normal, hitz.point);
Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
if(cross.y > 0)
{
tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
}
if (cross.y < 0)
{
anglelel = …Run Code Online (Sandbox Code Playgroud) 在我正在开发的游戏中,我获得了游戏世界对象的速度,就像这样
void getObjectVelocity(int objectID, vec3_t *velocityOut);
Run Code Online (Sandbox Code Playgroud)
所以如果我像这样调用这个函数
vec3_t storeObjectVelocity;
getObjectVelocity(21/* just an example ID */, &storeObjectVelocity);
Run Code Online (Sandbox Code Playgroud)
ID 为 21 的对象的速度将存储在storeObjectVelocity.
出于测试目的,我试图根据游戏屏幕中间的速度打印该对象的速度。
这是一个示例,只是为了让您更好地了解我要完成的工作
int convertVelocityToSpeed(vec3_t velocity)
{
//This is where I am having issues.
//Converting the objects 3D velocity vector to a speed value
}
int testHUDS()
{
char velocityToSpeedBuffer[32] = { 0 };
vec3_t storeObjectVelocity;
getObjectVelocity(21, &storeObjectVelocity);
strcpy(velocityToSpeedBuffer, "Speed: ");
strcat(velocityToSpeedBuffer, system::itoa(convertVelocityToSpeed(storeObjectVelocity), 10));
render::text(SCREEN_CENTER_X, SCREEN_CENTER_Y, velocityToSpeedBuffer, FONT_SMALL);
}
Run Code Online (Sandbox Code Playgroud)
这是我的vec3_t结构,以防你想知道
struct vec3_t
{
float x, y, z; …Run Code Online (Sandbox Code Playgroud) 我正在使用蓝图在虚幻引擎 4 中开发 VR 游戏。
我想计算用户需要转动他/她的枪(其方向通过运动控制器的位置确定)以指向目标所需的(偏航)角度。
我认为这可能是这样做的方法:
除了我不太确定如何执行。我一直在试验(如下面的屏幕截图所示),但没有进行正确的操作。有什么想法吗?
谢谢!
vector angle unreal-engine4 virtual-reality unreal-blueprint
在计算两个向量之间的角度时,我传统上使用 acos,但这需要对两个向量进行归一化。atan2 可用于完成相同的(特别是atan2(b.y_, b.x_) - atan2(a.y_, a.x_)),这是否需要归一化向量?
如果 atan2 不需要归一化向量,这是否更好用,因为归一化可能成本高昂且“更多”容易出错,因为它需要 sqrt 操作?
然后我读到 atan2 本身可能比 acos 更昂贵,但更准确?然后我还阅读了其他建议相反的互联网:( 很多相互矛盾的信息,不确定使用 acos 或 atan 计算两个向量之间的角度有什么关系。
推荐哪个?以及每次使用的好处/问题是什么?
任何帮助将不胜感激,谢谢!
我有 3 个点 p1(x1, y1)、p2(x2, y2) 和 p3(x3, y3)。我正在尝试计算这三个点之间的角度(逆时针方向)。我正在使用多个博客和 SE 网站中提供的以下点积方法(如下所示)。
def angle_between(p1, p2, p3):
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
v21 = (x1 - x2, y1 - y2)
v23 = (x3 - x2, y3 - y2)
dot = v21[0] * v23[0] + v21[1] * v23[1]
det = v21[0] * v23[1] - v21[1] * v23[0]
theta = np.rad2deg(np.arctan2(det, dot))
print(theta)
Run Code Online (Sandbox Code Playgroud)
它为我提供了不在直线上的任何点的正确角度。例如
p1 = (0, 0)
p2 = (1, …Run Code Online (Sandbox Code Playgroud) 我一直在使用 numpy 的polyfit函数来获得一些数据的线性拟合。我已经得到了一些斜坡。然而,斜坡的想法让我很困惑。
我得到的斜率为 0.0142 和 391!很不一样。
391 的斜率实际上意味着什么?看看这个
import numpy as np
import matplotlib.pyplot as plt
xr=np.arange(100)
yr=0.0142*xr
yr2=391*xr
plt.plot(xr,yr,yr2)
print("The angle is:",np.degrees(np.arctan(391)))
Run Code Online (Sandbox Code Playgroud)
The angle is: 89.8534637990051
那角度不可能是89.8度!
我错了什么?
在处理sin/cos/tan/acos时,有没有人看到过这种奇怪的价值?数学的东西?
=== WEIRD VALUE ===
-1.#IND00
=====================
void inverse_pos(double x, double y, double& theta_one, double& theta_two)
{
// Assume that L1 = 350 and L2 = 250
double B = sqrt(x*x + y*y);
double angle_beta = atan2(y, x);
double angle_alpha = acos((L2*L2 - B*B - L1*L1) / (-2*B*L1));
theta_one = angle_beta + angle_alpha;
theta_two = atan2((y-L1*sin(theta_one)), (x-L1*cos(theta_one)));
}
Run Code Online (Sandbox Code Playgroud)
这是我正在处理的代码.
在特定条件下 - 例如当x和y为10和10时, 此代码将-1.#IND00存储到theta_one和theta_two中.
它看起来不像字符或数字:(
毫无疑问,atan2/acos/stuff是问题所在.
但问题是,try和catch不起作用,因为那些双变量已经成功地存储了一些值.
此外,以下计算永远不会抱怨它,永远不会破坏程序!
我正在考虑强制以某种方式使用此值并使整个程序崩溃...所以我可以捕获此错误..
除了这个想法,我不知道 如何检查这些theta_one和theta_two变量是否存储了这个疯狂的值.
有什么好主意吗?
先感谢您..
我有单位矢量形式的角度.我需要能够改变这个角度.假设矢量V =(x,y)的角度是当前的A.如何在不转换矢量的情况下将角度更改为A + 0.2(这只是一个示例,它可以是我正在添加的任何值)一个角度,修改它,然后再次计算适当的向量?
是否可以在C#中设置矩形角度属性?
我试过这个:
Rectangle r = new Rectangle();
r.Width = 5;
r.Height = 130;
r.Fill = Brushes.Black;
r.RotateTransform.AngleProperty = 30; // Here is the problem
canvas1.Children.Add(r);
Run Code Online (Sandbox Code Playgroud) angle ×10
c++ ×4
trigonometry ×4
vector ×3
c# ×2
math ×2
numpy ×2
3d ×1
atan ×1
exception ×1
game-physics ×1
geometry ×1
javascript ×1
python-3.x ×1
raycasting ×1
vector-space ×1
wpf ×1