从位置向量获取速度向量 [英] Getting the velocity vector from position vectors
问题描述
我看了一堆类似的问题,但似乎找不到一个特别能回答我问题的问题.我正在编写一个简单的3d游戏,并且试图允许玩家拾取并在我的地图上移动实体.我本质上是想获得一个速度矢量,该矢量将物理"对象推"到与玩家眼睛无关的位置,无论他们在看什么.这是在另一个游戏中完成的示例(玩家将椅子实体举在眼前).
I looked at a bunch of similar questions, and I cannot seem to find one that particularly answers my question. I am coding a simple 3d game, and I am trying to allow the player to pick up and move entities around my map. I essentially want to get a velocity vector that will "push" the physics object a distance from the player's eyes, wherever they are looking. Here's an example of this being done in another game (the player is holding a chair entity in front of his eyes).
为此,我先找出玩家的眼睛角度,然后从这些角度获取前向矢量,然后计算物体的速度.这是我的工作代码:
To do this, I find out the player's eye angles, then get the forward vector from the angles, then calculate the velocity of the object. Here is my working code:
void Player::PickupOtherEntity( Entity& HoldingEntity )
{
QAngle eyeAngles = this->GetPlayerEyeAngles();
Vector3 vecPos = this->GetEyePosition();
Vector3 vecDir = eyeAngles.Forward();
Vector3 holdingEntPos = HoldingEntity.GetLocation();
// update object by holding it a distance away
vecPos.x += vecDir.x * DISTANCE_TO_HOLD;
vecPos.y += vecDir.y * DISTANCE_TO_HOLD;
vecPos.z += vecDir.z * DISTANCE_TO_HOLD;
Vector3 vecVel = vecPos - holdingEntPos;
vecVel = vecVel.Scale(OBJECT_SPEED_TO_MOVE);
// set the entity's velocity as to "push" it to be in front of the player's eyes
// at a distance of DISTANCE_TO_HOLD away
HoldingEntity.SetVelocity(vecVel);
}
所有这些都很棒,但是我想转换数学公式以便可以施加冲动.我不想为对象设置一个全新的速度,而是要向其现有速度添加"一些速度.因此,假设我具有当前速度,我需要什么样的数学运算来增加"速度?这本质上是一个游戏物理问题.谢谢!
All that is great, but I want to convert my math so that I can apply an impulse. Instead of setting a completely new velocity to the object, I want to "add" some velocity to its existing velocity. So supposing I have its current velocity, what kind of math do I need to "add" velocity? This is essentially a game physics question. Thank you!
推荐答案
基本牛顿理论/达朗贝尔物理学规定:
basic Newtonian/D'Alembert physics dictate:
derivate(position)=velocity
derivate(velocity)=acceleration
,也向后:
integrate(acceleration)=velocity
integrate(velocity)=position
因此对于您的引擎,您可以使用:
so for your engine you can use:
矩形求和而不是积分(积分的数值解).定义时间常数dt [seconds]
,它是两次更新之间的间隔(定时器或1/fps).因此,更新代码(必须每隔dt
定期调用一次:
rectangle summation instead of integration (numerical solution of integral). Define time constant dt [seconds]
which is the interval between updates (timer or 1/fps). So the update code (must be periodically called every dt
:
vx+=ax*dt;
vy+=ay*dt;
vz+=az*dt;
x+=vx*dt;
y+=vy*dt;
z+=vz*dt;
其中:
-
a{x,y,z} [m/s^2]
是实际加速度(在您的情况下,方向矢量缩放为a=Force/mass
) -
v{x,y,z} [m/s]
是实际速度 -
x,y,z [m]
是实际位置
a{x,y,z} [m/s^2]
is actual acceleration (in your case direction vector scaled toa=Force/mass
)v{x,y,z} [m/s]
is actual velocityx,y,z [m]
is actual position
- 必须将这些值初始化为
a,v
零,并且将x,y,z
初始化为初始位置 - 所有对象/玩家...都有自己的变量
- 完全停止由
v=0; a=0;
完成
- 仅通过更改
a
即可驱动对象
- 在通过碰撞
normal
碰撞的碰撞镜v
向量的情况下
- These values have to be initialized
a,v
to zero andx,y,z
to init position - all objects/players... have their own variables
- full stop is done by
v=0; a=0;
- driving of objects is done only by change of
a
- in case of collision mirror
v
vector by collisionnormal
,并可能乘以k<1.0
(例如0.95)来计算能量对冲击的损失
and maybe multiply by some k<1.0
(0.95 for example) to account energy loss on impact
您可以通过添加g
向量来添加重力或任何其他力场:
You can add gravity or any other force field by adding g
vector:
vx+=ax*dt+gx*dt;
vy+=ay*dt+gy*dt;
vz+=az*dt+gz*dt;
您还可以添加摩擦力和其他任何需要的东西
also You can add friction and anything else you need
PS.角度也一样,只是使用angle/omega/epsilon/I
而不是x/a/v/m
PS. the same goes for angles just use angle/omega/epsilon/I
instead of x/a/v/m
通过角度I
平均绕质量中心旋转(pitch,yaw,roll)
清晰
to be clear by angles I
mean rotation (pitch,yaw,roll)
around mass center
这篇关于从位置向量获取速度向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!