Box2dx:取消对身体的作用力?
我正在进行寻路,用力将身体推到路径点。然而,一旦他们足够接近航路点,我想抵消力量。我该怎么做?我是否需要单独维持施加到相关主体上的所有力?
我正在使用 Box2dx (C#/XNA)。
这是我的尝试,但它根本不起作用:
internal PathProgressionStatus MoveAlongPath(PositionUpdater posUpdater)
{
Vector2 nextGoal = posUpdater.Goals.Peek();
Vector2 currPos = posUpdater.Model.Body.Position;
float distanceToNextGoal = Vector2.Distance(currPos, nextGoal);
bool isAtGoal = distanceToNextGoal < PROXIMITY_THRESHOLD;
Vector2 forceToApply = new Vector2();
double angleToGoal = Math.Atan2(nextGoal.Y - currPos.Y, nextGoal.X - currPos.X);
forceToApply.X = (float)Math.Cos(angleToGoal) * posUpdater.Speed;
forceToApply.Y = (float)Math.Sin(angleToGoal) * posUpdater.Speed;
float rotation = (float)(angleToGoal + Math.PI / 2);
posUpdater.Model.Body.Rotation = rotation;
if (!isAtGoal)
{
posUpdater.Model.Body.ApplyForce(forceToApply, posUpdater.Model.Body.Position);
posUpdater.forcedTowardsGoal = true;
}
if (isAtGoal)
{
// how can the body be stopped?
posUpdater.forcedTowardsGoal = false;
//posUpdater.Model.Body.SetLinearVelocity(new Vector2(0, 0));
//posUpdater.Model.Body.ApplyForce(-forceToApply, posUpdater.Model.Body.GetPosition());
posUpdater.Goals.Dequeue();
if (posUpdater.Goals.Count == 0)
{
return PathProgressionStatus.COMPLETE;
}
}
更新
如果我确实跟踪我施加了多少力,它就无法考虑可能作用在其上的其他力。
我可以使用反射并将 _force
直接设置为零,但这感觉很脏。
I'm doing pathfinding where I use force to push body to waypoints. However, once they get close enough to the waypoint, I want to cancel out the force. How can I do this? Do I need to maintain separately all the forces I've applied to the body in question?
I'm using Box2dx (C#/XNA).
Here is my attempt, but it doesn't work at all:
internal PathProgressionStatus MoveAlongPath(PositionUpdater posUpdater)
{
Vector2 nextGoal = posUpdater.Goals.Peek();
Vector2 currPos = posUpdater.Model.Body.Position;
float distanceToNextGoal = Vector2.Distance(currPos, nextGoal);
bool isAtGoal = distanceToNextGoal < PROXIMITY_THRESHOLD;
Vector2 forceToApply = new Vector2();
double angleToGoal = Math.Atan2(nextGoal.Y - currPos.Y, nextGoal.X - currPos.X);
forceToApply.X = (float)Math.Cos(angleToGoal) * posUpdater.Speed;
forceToApply.Y = (float)Math.Sin(angleToGoal) * posUpdater.Speed;
float rotation = (float)(angleToGoal + Math.PI / 2);
posUpdater.Model.Body.Rotation = rotation;
if (!isAtGoal)
{
posUpdater.Model.Body.ApplyForce(forceToApply, posUpdater.Model.Body.Position);
posUpdater.forcedTowardsGoal = true;
}
if (isAtGoal)
{
// how can the body be stopped?
posUpdater.forcedTowardsGoal = false;
//posUpdater.Model.Body.SetLinearVelocity(new Vector2(0, 0));
//posUpdater.Model.Body.ApplyForce(-forceToApply, posUpdater.Model.Body.GetPosition());
posUpdater.Goals.Dequeue();
if (posUpdater.Goals.Count == 0)
{
return PathProgressionStatus.COMPLETE;
}
}
UPDATE
If I do keep track of how much force I've applied, it fails to account for other forces that may act on it.
I could use reflection and set _force
to zero directly, but that feels dirty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你只是想让身体停止死亡(消除作用在它身上的所有力),你可以让它进入睡眠状态:
这会清除身体的线性和角力和速度。睡眠物体也不会与其他睡眠物体发生碰撞,并且通常占用较少的 CPU 时间,因为它们的物理功能实际上已关闭。
如果您确实希望身体在停止死亡后参与物理活动,请立即唤醒它:
当然,如果身体正在与其他物体碰撞,这可能会导致“不自然”的结果。
If you just want to stop the body dead (cancelling out all forces acting on it) you can just put it to sleep:
This clears the body's linear and angular forces and velocities. Sleeping bodies also don't collide with other sleeping bodies and generally take up less CPU time, as effectively their physics is switched off.
If you actually want the body to take part in physics after it's stopped dead, just wake it immediately:
This may lead to "unnatural" looking results if the body was in the middle of colliding with other things of course.