我正在尝试通过施加力在 C++/Objective C 上的 Box2D 中制作螺旋涡流。 我想实现的是一个将物体从一个点推开或吸引它们的漩涡。 我想我必须施加不止一种力量。
我解决这个问题的切入点是:
我认为我必须施加三种力量: - 吸引或排斥 body 中心的冲动。 - 想要让它沿着螺旋线横向移动的冲动,但是……怎么做? - 如果冲动不适合我,则旋转 body 本身的扭矩。
最佳答案
我曾经通过制作圆形传感器制作了一个旋转平台,该传感器将圆形 vector 场给定的切向速度变化应用于物体。我使用的圆形 vector 场是这样的:
V = (y, -x)
可以在此处找到该 vector 场的直观表示: http://kasadkad.files.wordpress.com/2009/09/derham1.png?w=450&h=427
y 和 x 是 body 与传感器中心的相对位置,因此您可以执行以下操作:
Vector getTangentVector(Vector relativePosition, bool invert)
{
Vector vec;
if(invert) //if it's cw or ccw
{
vec.setY(relativePosition.x());
vec.setX(-relativePosition.y());
}
else
{
vec.setY(-relativePosition.x());
vec.setX(relativePosition.y());
}
return vec;
}
然后在我的程序的更新方法上我做了这样的事情:
for (b2ContactEdge* ce = platformBody->GetContactList(); ce; ce = ce->next)
{
b2Contact* c = ce->contact;
if(c->IsTouching())
{
const b2Body* bodyA = c->GetFixtureA()->GetBody();
const b2Body* bodyB = c->GetFixtureB()->GetBody();
const b2Body* targetBody = (bodyA == platformBody)?bodyB:bodyA;
Vector speed = getTangentImpulse(
getRelativePosition(platformBody, targetBody),
true);
speed *= CONSTANT; // CONSTANT = 1.8,
// this is to account for the targetBody attrition,
// so it doesn't slip on the platform
Vector currentSpeed;
currentSpeed.setX(targetBody->GetLinearVelocity().x);
currentSpeed.setY(targetBody->GetLinearVelocity().y);
Vector diff = speed - currentSpeed;
diff *= 0.01; //should depend on time, but this worked nicely. It makes the
//body change its linear velocity to be the same as "speed" at a
//0.01 change rate.
currentSpeed += diff;
targetBody->SetLinearVelocity(
b2Vec2(currentSpeed.x(),
currentSpeed.y()));
}
}
这个解决方案有很多解决方法,例如,我不使用脉冲并手动更改速度,它对我来说效果更好。另外,我使用一个常数来解释损耗。
它仍然产生了我需要的效果,所以我希望它对你有用。对于漩涡,我想你只需要连接一个关节,比如鼠标关节,连接到中心并捕获 body 。如果仅使用关节,则很难使其强度足以捕获 body ,但又不足以使其绕中心旋转。与我的代码一起,通过使用常量可能会更容易实现这一点。
我希望这会有所帮助。
编辑:我刚刚记得,使用平台代码还可以确保旋转方向,而仅使用关节是不可能的。
关于c++ - 在 Box2D 中制作漩涡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8565637/