dot ue4_UE4 常用数学

求点积时的两个向量一定要是单位向量,否则计算结果会大于Pi(正常范围是0到Pi)。

三、基于某点向某方向上投射指定距离后的坐标计算

代码:

float Len = FMath::Sqrt(100 * 100 + 100 * 100);

FVector Loc= FVector(100.f, 100.f, 0.f) + FRotator(0.f, 45.f, 0.f).Vector() * Len;

结果:

Loc = {X=200.f, Y=200.f, Z=0.f}

FRotator::Vector()对应的蓝图函数为GetRotationXVector

=================================

例子2:

float Len = FMath::Sqrt(100 * 100 + 100 * 100);

FVector Loc1= FVector(0.f, 0.f, 0.f) + FRotator(0.f, 45.f, 0.f).Vector() *Len;

FVector Loc2= FVector(0.f, 0.f, 0.f) + FRotator(0.f, 135.f, 0.f).Vector() *Len;

FVector Loc3= FVector(0.f, 0.f, 0.f) + FRotator(0.f, 225.f, 0.f).Vector() *Len;

FVector Loc4= FVector(0.f, 0.f, 0.f) + FRotator(0.f, 315.f, 0.f).Vector() * Len;

结果:

Loc1 = {X=100.f, Y=100.f, Z=0.f}

Loc2= {X=-100.f, Y=100.f, Z=0.f}

Loc3= {X=-100.f, Y=-100.f, Z=0.f}

Loc4= {X=100.f, Y=-100.f, Z=0.f}

四、如何检测某个点是否在多边形内部或者直线上

点在多边形内的判别方法

int pnpoly(int nvert, float *vertx, float *verty, float testx, floattesty)

{int i, j, c = 0;for (i = 0, j = nvert-1; i < nvert; j = i++)

{if (((verty[i] > testy) != (verty[j] > testy)) &&(testx< (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) +vertx[i]) )

{

c= !c;

}

}returnc;

}

Arguments

nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end has been discussed in the article referred above.

vertx, verty: Arrays containing the x- and y-coordinates of the polygon’s vertices.

testx, testy: X- and y-coordinate of the test point.

点在两点之间的直线上的判别方法

boolisLieOnLine()

{

dxc= currPoint.x -point1.x;

dyc= currPoint.y -point1.y;

dxl= point2.x -point1.x;

dyl= point2.y -point1.y;//point lies on the line if and only if (dxc * dyl - dyc * dxl) is equal to zero.

bool isCrossLine = dxc * dyl - dyc * dxl == 0;if(isCrossLine)

{if (abs(dxl) >=abs(dyl))return dxl > 0 ?point1.x<= currPoint.x && currPoint.x <=point2.x :

point2.x<= currPoint.x && currPoint.x <=point1.x;else

return dyl > 0 ?point1.y<= currPoint.y && currPoint.y <=point2.y :

point2.y<= currPoint.y && currPoint.y <=point1.y;

}return false;

}

参考

五、两个旋转矩阵(Rotation Matrix)相乘(Multiply)的几何意义

讲之前,先说下如果两个Rotation相加的意义,比如:

FRotator Rot1(0.f, 90.f, 0.f);

FRotator Rot2(90.f,0.f, 0.f);

FRotator Result= Rot1 + Rot2;

得到的结果FRotator Result(90.f, 90.f, 0.f),其意义是: 物体相对空间坐标原点的Rotation为(90.f, 90.f, 0.f),很好理解。

如果两个Rotation转换为Martix并相乘,比如:

FRotator Rot1(0.f, 90.f, 0.f);

FRotator Rot2(90.f,0.f, 0.f);

FRotator Result=( FRotationMatrix(Rot1) * FRotationMatrix(Rot2)).Rotator();

得到的结果FRotator Result(0.f, 90.f, 90.f),其意义是: 先将物体作Rot1旋转,即:Yaw方向(水平平面)旋转90度,然后再假设该物体相对坐标轴原点的旋转量为(0, 0, 0),即没有作任何旋转,但实际Rotation相对坐标轴原点为(0, 90, 0);然后再将物体进行Rot2旋转,即Pitch方向(垂直于(90, 0, 0)方向的平面)侧翻90度,因为侧翻90度前假设物体的Rotation是(0, 0, 0),所以侧翻时所在的平面不再是Yaw=90的平面(垂直于(0, 90, 0)方向),而是Yaw=0的平面(垂直于(90, 0, 0)方向)。没做相关配图,这段话理解起来有点绕,最好用空间思维想象下,可以用手掌比划。

实际应用:

比如空间中有两个物体:A和B,现在要将A旋转至与B相同的朝向,目前只知道A的相对世界坐标的Rotation Rw(90.f,0.f, 0.f)、B相对A(将A的Rotation当做(0, 0, 0))的Rotation Rr(0.f, 90.f, 0.f),求A旋转后的世界坐标Rotation。

此时的计算公式就是:

(FRotationMatrix(Rr) * FRotationMatrix(Rw)).Rotator()

注意:矩阵相乘时,两个乘数的前后位置不同则计算的结果也不同,比如上面例子,如果是( FRotationMatrix(Rot2) * FRotationMatrix(Rot1)).Rotator,则结果是Rotation(90, -90, -180)。

一个典型应用:以UE4为例,当角色移动时,不能将默认MoveForward的实参(比如(1.0, 0.f, 0.f))和MoveRight的实参(比如(0.0, 1.f, 0.f))传递给AddMovementInput,因为InputValue需要相对摄像机的朝向来计算,否则当按下W键,期望角色摄像机正对方向移动,但实际是侧向移动。此时就可以通过旋转矩阵相乘来获取当前摄像机朝向方向下的MoveForward和MoveRight方向。

FRotator AMyPlayerController::GetInputRotationInWorld()

{

FRotator Ret=FRotator::ZeroRotator;if (AMyCharacter* Player = Cast(GetPawn()))

{if(UCameraComponent* CameraComp = Player->GetFollowCamera())

{

FRotator InputRotLS= FVector(ForwardInputValue, RighInputInputValue, 0.f).Rotation();

FRotator CameraRotWS= FRotator(0.f, CameraComp->GetComponentRotation().Yaw, 0.f);

Ret= (FRotationMatrix(InputRotLS) *FRotationMatrix(CameraRotWS)).Rotator();

}

}returnRet;

}

Video Tutorials

Linear transformations and matrices | Essence of linear algebra, chapter 3

https://www.youtube.com/watch?v=kYB8IZa5AuE

六、抛物线Parabola movement

header

AStaticMeshActor* TestCube =nullptr;//throw speed

UPROPERTY(EditDefaultsOnly)

FVector StartForce= FVector(100.f, 100.f, 2000.f);//gravitational acceleration

float GravityAcclerator = -980.f;//accumulated movtion time

float AccumulateTime = 0.f;

cpp

voidATestTPGameMode::StartPlay()

{

Super::StartPlay();//finding the Actor in scene.

for (TActorIterator Iter(GetWorld()); Iter; ++Iter)

{if (Iter->GetName() == TEXT("Cube_2"))

{

TestCube= *Iter;break;

}

}

}void ATestTPGameMode::Tick(floatDeltaSeconds)

{

Super::Tick(DeltaSeconds);

AccumulateTime+=DeltaSeconds;//calculate gravitational speed in real time.

float ZSpeed = GravityAcclerator *AccumulateTime;//calculate summation speed of gravitational speed and throw speed.

FVector CurrSpeed = StartForce + FVector(0.f, 0.f, ZSpeed) ;//calculate movement distance in real time.

FVector MoveDist = CurrSpeed *DeltaSeconds;//set relative location.

TestCube->AddActorWorldOffset(MoveDist, true);

}

七、空间点到直线垂足坐标的解算方法

算法1

假设空间某点O的坐标为(Xo,Yo,Zo),空间某条直线上两点A和B的坐标为:(X1,Y1,Z1),(X2,Y2,Z2),设点O在直线AB上的垂足为点N,坐标为(Xn,Yn,Zn)。点N坐标解算过程如下: 首先求出下列向量:

由向量垂直关系(公式1)

点N在直线AB上,根据向量共线(公式2):

由公式2得(公式3):

把公式3式代入公式1式,式中只有一个未知数k,整理化简解出k(公式4):

把公式4式代入公式3式即得到垂足N的坐标。

二维空间

//二维空间点到直线的垂足

structPoint

{doublex,y;

}

Point GetFootOfPerpendicular(const Point &pt, //直线外一点

const Point &begin, //直线开始点

const Point &end) //直线结束点

{

Point retVal;double dx = begin.x -end.x;double dy = begin.y -end.y;if(abs(dx) < 0.00000001 && abs(dy) < 0.00000001)

{

retVal=begin;returnretVal;

}double u = (pt.x - begin.x)*(begin.x - end.x) +(pt.y- begin.y)*(begin.y -end.y);

u= u/((dx*dx)+(dy*dy));

retVal.x= begin.x + u*dx;

retVal.y= begin.y + u*dy;returnretVal;

}

三维空间

//三维空间点到直线的垂足

structPoint

{doublex,y,z;

}

Point GetFootOfPerpendicular(const Point &pt, //直线外一点

const Point &begin, //直线开始点

const Point &end) //直线结束点

{

Point retVal;double dx = begin.x -end.x;double dy = begin.y -end.y;double dz = begin.z -end.z;if(abs(dx) < 0.00000001 && abs(dy) < 0.00000001 && abs(dz) < 0.00000001)

{

retVal=begin;returnretVal;

}double u = (pt.x - begin.x)*(begin.x - end.x) +(pt.y- begin.y)*(begin.y - end.y) + (pt.z - begin.z)*(begin.z -end.z);

u= u/((dx*dx)+(dy*dy)+(dz*dz));

retVal.x= begin.x + u*dx;

retVal.y= begin.y + u*dy;

retVal.y= begin.z + u*dz;returnretVal;

}

算法2

计算p1、p2连成的直线上的离 q 点最近的点 f(即 q 点到直线 p1、p2的垂足坐标):

XNA实现

Vector3 p1 = newVector3(x1, y1, z1);

Vector3 p2= newVector3(x2, y2, z2);

Vector3 q= newVector3(x3, y3, z3);

Vector3 u= p2 -p1;

Vector3 pq= q -p1;

Vector3 w2= pq - Vector3.Multiply(u, Vector3.Dot(pq, u) /u.LengthSquared());

Vector3 f= q - w2;

UE4实现

FVector GetPerpendicularPointToLine(const FVector& PointStart, const FVector& PointEnd, const FVector&PointPerpendicular)

{

FVector Line= PointEnd -PointStart;

FVector PS= PointPerpendicular -PointStart;

FVector W2= PS - (Line * (PS | Line) /Line.SizeSquared());

FVector FootPoint= PointPerpendicular -W2;returnFootPoint;

}

UE4引擎提供的工具函数:

FVector UKismetMathLibrary::FindClosestPointOnLine(FVector Point, FVector LineOrigin, FVector LineDirection)

{const FVector SafeDir =LineDirection.GetSafeNormal();const FVector ClosestPoint = LineOrigin + (SafeDir * ((Point-LineOrigin) |SafeDir));returnClosestPoint;

}

参考

八、已知三点求平面方程、平面法向量和点到平面的距离

已知三点p1(x1,y1,z1),p2(x2,y2,z2),p3(x3,y3,z3),要求确定的平面方程。

关键在于求出平面的一个法向量,为此做向量p1p2(x2-x1,y2-y1,z2-z1), p1p3(x3-x1,y3-y1,z3-z1),平面法线和这两个向量垂直,因此法向量n:

平面方程:

a * (x - x1) + b * (y - y1) + c * (z - z1) = 0;

d= -a * x1 - b * y1 - c * z1;

平面方程2:

a * x + b * y + c * z + d=0;

代码:

//已知3点坐标,求平面ax+by+cz+d=0;

void get_panel(Point p1, Point p2, Point p3, double &a, double &b, double &c, double &d)

{

a= ( (p2.y - p1.y) * (p3.z - p1.z) - (p2.z - p1.z) * (p3.y -p1.y) );

b= ( (p2.z - p1.z) * (p3.x - p1.x) - (p2.x - p1.x) * (p3.z -p1.z) );

c= ( (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x -p1.x) );

d= ( 0 - (a * p1.x + b * p1.y + c *p1.z) );

}//已知三点坐标,求法向量

Vec3 get_Normal(Point p1, Point p2, Point p3)

{double a = ( (p2.y - p1.y) * (p3.z - p1.z) - (p2.z - p1.z) * (p3.y -p1.y) );double b = ( (p2.z - p1.z) * (p3.x - p1.x) - (p2.x - p1.x) * (p3.z -p1.z) );double c = ( (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x -p1.x) );returnVec3(a, b, c);

}//点到平面距离

double dis_pt2panel(Point pt, double a, double b, double c, doubled)

{return f_abs(a * pt.x + b * pt.y + c * pt.z + d) / sqrt(a * a + b * b + c *c);

}

UE4提供的工具函数:

/**

* Calculate the projection of a point on the plane defined by PlaneBase and PlaneNormal.

*

* @param Point The point to project onto the plane

* @param PlaneBase Point on the plane

* @param PlaneNorm Normal of the plane (assumed to be unit length).

* @return Projection of Point onto plane*/

static FVector FVector::PointPlaneProject(const FVector& Point, const FVector& PlaneBase, const FVector& PlaneNormal);

参考

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值