问题描述
我用过 Nick Vellios 的教程 使用 Box2D 对象创建径向重力.我知道 Make a Vortex 在这里,但我不知道如何在我的项目中实现它.
I've used Nick Vellios' tutorial to create radial gravity with a Box2D object. I am aware of Make a Vortex here on SO, but I couldn't figure out how to implement it in my project.
我制作了一个涡流对象,它是一个 Box2D 圆形传感器,它以一致的角速度旋转.当其他 Box2D 对象接触这个涡旋对象时,我希望它们以与涡旋相同的角速度旋转,逐渐靠近涡旋的中心.此刻物体被吸引到漩涡的中心,但它会直奔漩涡的中心,而不是像我想要的那样缓慢地绕着它旋转.它也将沿与涡旋相反的方向以及涡旋的旋转运动.
I have made a vortex object, which is a Box2D circleShape sensor that rotates with a consistent angular velocity. When other Box2D objects contact this vortex object I want them to rotate around at the same angular velocity as the vortex, gradually getting closer to the vortex's centre. At the moment the object is attracted to the vortex's centre but it will head straight for the centre of the vortex, rather than spinning around it slowly like I want it to. It will also travel in the opposite direction than the vortex as well as with the vortex's rotation.
给定一个涡流和一个 box2D 主体,我如何设置 box2d 主体在它被吸入"时随涡流旋转.
Given a vortex and a box2D body, how can I set the box2d body to rotate with the vortex as it gets 'sucked in'.
当我这样创建漩涡时,我设置了它的旋转:
I set the rotation of the vortex when I create it like this:
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.angle = 2.0f;
bodyDef.angularVelocity = 2.0f;
这是我如何应用径向重力,根据 Nick Vellios 的示例代码.
Here is how I'm applying the radial gravity, as per Nick Vellios' sample code.
-(void)applyVortexForcesOnSprite:(CCSpriteSubclass*)sprite spriteBody:(b2Body*)spriteBody withVortex:(Vortex*)vortex VortexBody:(b2Body*)vortexBody vortexCircleShape:(b2CircleShape*)vortexCircleShape{
//From RadialGravity.xcodeproj
b2Body* ground = vortexBody;
b2CircleShape* circle = vortexCircleShape;
// Get position of our "Planet" - Nick
b2Vec2 center = ground->GetWorldPoint(circle->m_p);
// Get position of our current body in the iteration - Nick
b2Vec2 position = spriteBody->GetPosition();
// Get the distance between the two objects. - Nick
b2Vec2 d = center - position;
// The further away the objects are, the weaker the gravitational force is - Nick
float force = 1 / d.LengthSquared(); // 150 can be changed to adjust the amount of force - Nick
d.Normalize();
b2Vec2 F = force * d;
// Finally apply a force on the body in the direction of the "Planet" - Nick
spriteBody->ApplyForce(F, position);
//end radialGravity.xcodeproj
}
更新我认为 iForce2d 已经为我提供了足够的信息来继续我的工作,现在它只是在调整.除了上面的代码之外,这就是我目前正在做的事情.正在发生的事情是身体获得了足够的速度来很好地退出涡旋的重力——我需要在某个地方检查速度是否保持在这个数字以下.我有点担心我目前没有考虑到物体的质量.
Update I think iForce2d has given me enough info to get on my way, now it's just tweaking. This is what I'm doing at the moment, in addition to the above code. What is happening is the body gains enough velocity to exit the vortex's gravity well - somewhere I'll need to check that the velocity stays below this figure. I'm a little concerned I'm not taking into account the object's mass at the moment.
b2Vec2 vortexVelocity = vortexBody->GetLinearVelocityFromWorldPoint(spriteBody->GetPosition() );
b2Vec2 vortexVelNormal = vortexVelocity;
vortexVelNormal.Normalize();
b2Vec2 bodyVelocity = b2Dot( vortexVelNormal, spriteBody->GetLinearVelocity() ) * vortexVelNormal;
//Using a force
b2Vec2 vel = bodyVelocity;
float forceCircleX = .6 * bodyVelocity.x;
float forceCircleY = .6 * bodyVelocity.y;
spriteBody->ApplyForce( b2Vec2(forceCircleX,forceCircleY), spriteBody->GetWorldCenter() );
推荐答案
听起来你只需要在body当前点根据漩涡的方向再施加一个力.您可以使用 b2Body::GetLinearVelocityFromWorldPoint 来查找世界上任何点的涡旋速度.来自 Box2D 来源:
It sounds like you just need to apply another force according to the direction of the vortex at the current point of the body. You can use b2Body::GetLinearVelocityFromWorldPoint to find the velocity of the vortex at any point in the world. From Box2D source:
/// Get the world linear velocity of a world point attached to this body.
/// @param a point in world coordinates.
/// @return the world velocity of a point.
b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
那就是:
b2Vec2 vortexVelocity = vortexBody->GetLinearVelocityFromWorldPoint( suckedInBody->GetPosition() );
一旦您知道您的目标速度,您就可以计算从当前速度到所需速度需要多少力.这可能会有所帮助:http://www.iforce2d.net/b2dtut/constant-speed
Once you know the velocity you're aiming for, you can calculate how much force is needed to go from the current velocity, to the desired velocity. This might be helpful: http://www.iforce2d.net/b2dtut/constant-speed
该链接中的主题仅讨论一维情况.对于您的情况,它本质上也是一维的,如果您将吸入体的当前速度投影到 vortexVelocity 向量上:
The topic in that link only discusses a 1-dimensional situation. For your case it is also essentially 1-dimensional, if you project the current velocity of the sucked-in body onto the vortexVelocity vector:
b2Vec2 vortexVelNormal = vortexVelocity;
vortexVelNormal.Normalize();
b2Vec2 bodyVelocity = b2Dot( vortexVelNormal, suckedInBody->GetLinearVelocity() ) * vortexVelNormal;
现在 bodyVelocity 和 vortexVelocity 将处于同一方向,您可以计算要施加多少力.但是,如果你只是施加足够的力来精确匹配涡流速度,被吸入的物体可能会进入涡旋周围的轨道,而不会真正被吸入.我认为你会想要使力比那个小一点,而且我也会根据重力的大小把它缩小,否则被吸入的身体一接触到漩涡的外缘就会被甩到一边.可能需要进行大量调整才能获得您想要的效果.
Now bodyVelocity and vortexVelocity will be in the same direction and you can calculate how much force to apply. However, if you simply apply enough force to match the vortex velocity exactly, the sucked in body will probably go into orbit around the vortex and never actually get sucked in. I think you would want to make the force quite a bit less than that, and I would scale it down according to the gravity strength as well, otherwise the sucked-in body will be flung away sideways as soon as it contacts the outer edge of the vortex. It could take a lot of tweaking to get the effect you want.
你施加的力应该基于当前速度(bodyVelocity)和期望速度(vortexVelocity)之间的差异,即.如果身体已经随着漩涡移动,那么你不需要施加任何力量.看看我上面给出的链接中标题为使用力量"的小节中的最后一个代码块.如果将 'vel' 和 'desiredVel' 替换为 bodyVelocity 和 vortexVelocity 向量的大小,最后三行几乎可以满足您的需求:
The force you apply should be based on the difference between the current velocity (bodyVelocity) and the desired velocity (vortexVelocity), ie. if the body is already moving with the vortex then you don't need to apply any force. Take a look at the last code block in the sub-section titled 'Using forces' in the link I gave above. The last three lines there do pretty much what you need if you replace 'vel' and 'desiredVel' with the sizes of your bodyVelocity and vortexVelocity vectors:
float desiredVel = vortexVelocity.Length();
float currentVel = bodyVelocity.Length();
float velChange = desiredVel - currentVel;
float force = body->GetMass() * velChange / (1/60.0); //for a 1/60 sec timestep
body->ApplyForce( b2Vec2(force,0), body->GetWorldCenter() );
但请记住,这可能会将物体送入轨道,因此在此过程中,您可能希望减小施加的力的大小,例如.将desiredVel"降低某个百分比,将force"降低某个百分比等.如果您还可以按比例缩小力,使其在涡流的外边缘为零,它可能看起来会更好.
But remember this would probably put the body into orbit, so somewhere along the way you would want to reduce the size of the force you apply, eg. reduce 'desiredVel' by some percentage, reduce 'force' by some percentage etc. It would probably look better if you could also scale the force down so that it was zero at the outer edge of the vortex.
这篇关于在 Box2d/Cocos2d for iPhone 中应用涡流/漩涡效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!