计算正确的脉冲或力以将Box2D体移动到特定位置 - Box2D [英] Calculate correct impluse or force to move a Box2D body to a specific position - Box2D

查看:292
本文介绍了计算正确的脉冲或力以将Box2D体移动到特定位置 - Box2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将Box2D机身移动到特定位置的问题,而不使用它。

i have a question about moving a Box2D body to a specific position without using this for example.

body->SetTransform(targetVector,body->GetAngle())

我有一些适用于applyForce的代码( here

I have some code which works for applyForce (here):

const float destinationControl = 0.3f;
b2Vec2 missilePosition = _physicalBody->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();

if (dist > 0)
{

// compute the aiming direction
b2Vec2 direction = b2Vec2(diff.x / dist, diff.y / dist);

// get the current missile velocity because we will apply a force to compensate this.
b2Vec2 currentVelocity = _physicalBody->GetLinearVelocity();

// the missile ideal velocity is the direction to the target multiplied by the max speed
b2Vec2 desireVelocity = b2Vec2(direction.x * maxSpeed, direction.y * maxSpeed);

// compensate the current missile velocity by the desired velocity, based on the control factor

b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);

// transform our velocity into an impulse (get rid of the time and mass factor)
float temp = (_physicalBody->GetMass() / normalDelta);

b2Vec2 finalForce = b2Vec2(finalVelocity.x * temp, finalVelocity.y * temp);

_physicalBody->ApplyForce(finalForce, _physicalBody->GetWorldCenter());

}

但是 maxSpeed 是高点,身体移动点到快。

But the when the maxSpeed is to high the body move over the point to fast.

所以有人知道如何计算力( ApplyForce )或一个impluse( ApplyLinearImpulse )将身体移动到特定时间内的目标位置(非常精确)。

So does anyone know how to calculate a force (ApplyForce) or an impluse (ApplyLinearImpulse) to move the body to a target position (very exactly) in a specific time.

或包含上述代码的解决方案。我的意思是计算 maxSpeed 将身体在特定时间内移动到目标位置。

Or a solution with the code above. I mean calculate the maxSpeed to move the body in a specific time to the target position.

在我的谷歌搜索中我从iforce找到了关于预计轨迹的有趣文章
这里)。也许这也有帮助吗?

In my google search i found the interesting article from iforce about projected trajectory (here). Maybe this could be help too?

提前谢谢

推荐答案

我认为你大多数都是正确的,但你没有检查身体是否会在下一个时间步骤中超过目标。这对我有用:

I think you have it mostly correct, but you are not checking to see if the body will overshoot the target in the next time step. Here is what works for me:

b2Vec2 targetPosition = ...;
float targetSpeed = ...;

b2Vec2 direction = targetPosition - body->GetPosition();
float distanceToTravel = direction.Normalize();

// For most of the movement, the target speed is ok
float speedToUse = targetSpeed;

// Check if this speed will cause overshoot in the next time step.
// If so, we need to scale the speed down to just enough to reach
// the target point. (Assuming here a step length based on 60 fps)
float distancePerTimestep = speedToUse / 60.0f;
if ( distancePerTimestep > distanceToTravel )
    speedToUse *= ( distanceToTravel / distancePerTimestep );

// The rest is pretty much what you had already:
b2Vec2 desiredVelocity = speedToUse * direction;
b2Vec2 changeInVelocity = desiredVelocity - body->GetLinearVelocity();

b2Vec2 force = body->GetMass() * 60.0f * changeInVelocity;
body->ApplyForce( force, body->GetWorldCenter(), true );

这篇关于计算正确的脉冲或力以将Box2D体移动到特定位置 - Box2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆