gpt4 book ai didi

javascript - Box2D 引擎 javascript 端口 Prismatic Joint 的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 06:34:02 42 4
gpt4 key购买 nike

即使提升 block 根本不动,上点棱柱关节 (b2PrismaticJointDef) 上的提升 block 也会对位于提升 block 表面的动态物体产生插入力。

可能是 javascript 端口的错误。但我想修复它,因为我的游戏中需要电梯。

更新 v1

我使用了 Box2DFlash 2.1a 的 box2dweb javascript 端口 https://code.google.com/p/box2dweb/ .

更新 v2

这是关于 Flash 的类似演示 http://hyzhak.github.com/darlingjs/performance/box2dweb/它有同样的问题,所以这个问题可能在闪存或原始 Box2D 引擎上。

http://jsfiddle.net/hyzhak/2kjDZ/

var b2Vec2 = Box2D.Common.Math.b2Vec2,
b2BodyDef = Box2D.Dynamics.b2BodyDef,
b2Body = Box2D.Dynamics.b2Body,
b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
b2World = Box2D.Dynamics.b2World,
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef;

(function() {
var world = buildWorld();

var leftBlock = buildBlock({world: world, x: 2, y: 8, width: 2, height: 12, static: true});
var rightBlock = buildBlock({world: world, x: 12, y: 8, width: 2, height: 12, static: true});
var bottomBlock = buildBlock({world: world, x: 7, y: 13, width: 8, height: 2, static: false});
var box = buildBlock({world: world, x: 7, y: 10, width: 2, height: 2, static: false});

var joint = buildPrismaticJoint({world: world,
anchorA: new b2Vec2(7, 13),
axis: new b2Vec2(0, 1),
bodyA: bottomBlock,
bodyB: world.GetGroundBody()});

var debugDraw = buildDebugDraw(world);

setInterval(function(){
world.Step(1 / 60, 10, 10);
world.DrawDebugData();
world.ClearForces();
},1000/60);
})();

function buildWorld() {
return new b2World(
new b2Vec2(0, 10), //gravity vector
true
);
}

function buildBlock(state) {
var fixDef = new b2FixtureDef;
fixDef.shape = new b2PolygonShape;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = .5;
fixDef.shape.SetAsBox(state.width / 2, state.height / 2);
var bodyDef = new b2BodyDef;
bodyDef.type = state.static?b2Body.b2_staticBody:b2Body.b2_dynamicBody;
bodyDef.position.Set(state.x, state.y);
var body = state.world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
return body;
}

//buildPrismaticJoint(world, 9, 15, 0, 1, bottomBlock, world.GetGroundBody());
function buildPrismaticJoint(state) {
var jointDef = new b2PrismaticJointDef();
jointDef.Initialize(state.bodyA, state.bodyB, state.anchorA, state.axis);
jointDef.collideConnected = false;
jointDef.lowerTranslation = 0.0;
jointDef.upperTranslation = 5.0;
jointDef.enableLimit = true;
jointDef.maxMotorForce = 400.0;
jointDef.motorSpeed = 3.0;
jointDef.enableMotor = true;
return state.world.CreateJoint(jointDef);
}

function buildDebugDraw(world) {
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("playground").getContext("2d"));
debugDraw.SetDrawScale(20.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(
b2DebugDraw.e_shapeBit |
b2DebugDraw.e_jointBit |
b2DebugDraw.e_aabbBit |
b2DebugDraw.e_pairBit |
b2DebugDraw.e_centerOfMassBit |
b2DebugDraw.e_controllerBit
);
world.SetDebugDraw(debugDraw);

return debugDraw;
}

最佳答案

我在使用 Box2D v2.2.1 C++ 代码时遇到了同样的问题。我在静态主体和移动(动态)主体之间使用 b2PrismaticJoint 来模拟“电梯”(或电梯)。

当升降机到达沿棱柱关节平移的上端时,它似乎停止了。然而,当另一个物体坐在升降机顶部并到达平移的上端时,升降机顶部的 body 开始“弹跳”,就好像升降机仍在施加力一样。 (我相信这正是正在发生的事情,即升降机的主体没有沿着棱柱关节进一步平移,但棱柱关节的电机仍在转动并向 body 施加向上的力)。

我最初通过在达到平移上限时手动将棱柱关节的电机速度设置为 0.0 来解决此问题。在我的主游戏循环中,即以帧速率调用的函数(Box2D 中的“tick:”或 Cocos2D 中的“update:”),我插入了以下内容(我在这里使用 Objective-C,kLiftJointValue 是一个任意整数常量):

// Iterate over all joints (used to prevent upper end oscillations from psimaticJoints on lifts)
for (b2Joint* j = world->GetJointList(); j; j = j->GetNext())
{
if ((int)(j->GetUserData()) == kLiftJointValue) // check to see if lift is at upper end of translation
{
CGFloat currentTranslation = ((b2PrismaticJoint*)j)->GetJointTranslation();
CGFloat lowerLimit = ((b2PrismaticJoint*)j)->GetLowerLimit();
CGFloat upperLimit = ((b2PrismaticJoint*)j)->GetUpperLimit();
b2Body *bodyA = j->GetBodyA();
b2Body *bodyB = j->GetBodyB();
BOOL notStopped = (bodyA->GetType() == b2_dynamicBody || bodyB->GetType() == b2_dynamicBody);

if (fabsf(currentTranslation - lowerLimit) <= 0.01*(upperLimit - lowerLimit) && notStopped )
{
CCLOG(@"Stopping platform");
(j->GetBodyA())->SetType(b2_staticBody);
(j->GetBodyB())->SetType(b2_staticBody);
((b2PrismaticJoint*)j)->SetMotorSpeed(0.0);
}
}
}

上面的代码简单地遍历了世界上的所有关节并寻找那些被适当标记的关节,如果 body 已经平移到允许的最大平移的 1% 以内,那么移动的 body 就会变成静止的,并且电机被禁用将电机速度设置为 0。您还可以使用 Box2D 碰撞检测并将关节的电机速度设置为 0 ...

然后我意识到实现此升降机的最佳方法(不干扰升降机顶部的任何东西)是逐渐降低棱柱关节的电机速度。我是这样做的:

// Iterate over all joints (used to prevent upper end oscillations from psimaticJoints on lifts)
for (b2Joint* j = world->GetJointList(); j; j = j->GetNext())
{
if ((int)(j->GetUserData()) == kLiftJointValue) // check to see if lift is at upper end of translation
{
CGFloat currentTranslation = ((b2PrismaticJoint*)j)->GetJointTranslation();
CGFloat lowerLimit = ((b2PrismaticJoint*)j)->GetLowerLimit();
CGFloat upperLimit = ((b2PrismaticJoint*)j)->GetUpperLimit();
b2Body *bodyA = j->GetBodyA();
b2Body *bodyB = j->GetBodyB();
BOOL notStopped = (bodyA->GetType() == b2_dynamicBody || bodyB->GetType() == b2_dynamicBody);

if (fabsf(currentTranslation - lowerLimit) <= 0.25*(upperLimit - lowerLimit) && notStopped)
{
// Get current motor speed and update
CGFloat currentSpeed = ((b2PrismaticJoint*)j)->GetMotorSpeed();
CGFloat newSpeed;

if (currentSpeed < 0.0)
{
if (fabsf(currentTranslation - lowerLimit) <= 0.01*(upperLimit - lowerLimit) && notStopped )
{
CCLOG(@"Stopping platform");
(j->GetBodyA())->SetType(b2_staticBody);
(j->GetBodyB())->SetType(b2_staticBody);
((b2PrismaticJoint*)j)->SetMotorSpeed(0.0);
}
else
{
CCLOG(@"Decelerating: speed=%f", currentSpeed);
newSpeed = 0.95*currentSpeed;
}
}
else if (currentSpeed > 0.0)
{
CCLOG(@"Accelerating: speed=%f", currentSpeed);

newSpeed = 1.05*currentSpeed;

if (newSpeed > 20.0)
newSpeed = 20.0;
}

// update motor speed
((b2PrismaticJoint*)j)->SetMotorSpeed(newSpeed);
}
}
}

您需要使用各种常数(尤其是常数 0.95 和 1.05),因为它们针对特定的电机属性进行了微调(在我的例子中为 maxMotorForce = 1000 和 motorSpeed = 20)。

当然,在我的项目的其他地方,我有定时器运行,它根据需要重置升降体和关节电机属性(使其向上移动,然后通过将关节的电机速度反转为正值来返回)。

当电梯在另一个(底部)限制附近平移时,我没有实现加速/减速技术,因为我不关心电梯停止时乘坐电梯的东西在这个方向上被施加力。然而,当电梯离开或返回底部时,可以使用与上述相同的方法平稳地加速或减速......

关于javascript - Box2D 引擎 javascript 端口 Prismatic Joint 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760919/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com