Cocos2d-xv3中的交叉边碰撞

Cocos2d-xv3中的交叉边碰撞,cocos2d-x,collision-detection,game-physics,cocos2d-x-3.0,Cocos2d X,Collision Detection,Game Physics,Cocos2d X 3.0,我正在(尝试)使用cocos2d-xv3开发一个简单的游戏(请参阅) 目的是将彩虹般的玩家从底部的绿色区域移动到顶部的绿色区域。 操纵杆是左边的白色拇指:它可以用手指移动来改变玩家的方向 到目前为止,我正在使用cocos2d-xv3中的内置碰撞检测器(通过类PhysicalBody)。图中的红色边框代表玩家的形状,是玩家可以自由移动的竞技场的边框 当用户移动操纵杆的拇指时,其方向用于设置玩家的速度。然而,在物理世界中,设置速度并不是最好的选择,因为它违反了物理定律,也就是说,只有力可以施加到物体

我正在(尝试)使用cocos2d-xv3开发一个简单的游戏(请参阅)

目的是将彩虹般的玩家从底部的绿色区域移动到顶部的绿色区域。 操纵杆是左边的白色拇指:它可以用手指移动来改变玩家的方向

到目前为止,我正在使用cocos2d-xv3中的内置碰撞检测器(通过类
PhysicalBody
)。图中的红色边框代表玩家的形状,是玩家可以自由移动的竞技场的边框

当用户移动操纵杆的拇指时,其方向用于设置玩家的速度。然而,在物理世界中,设置速度并不是最好的选择,因为它违反了物理定律,也就是说,只有力可以施加到物体上。因此,根据所需速度,我计算实现该速度所需的冲量:

Vec2 currentVel = _player->getPhysicsBody()->getVelocity();
Vec2 desiredVel = 80*_joystick->getDirection(); // a normalized Vec2
Vec2 deltaVel = desiredVel - currentVel;
Vec2 impulse = _player->getPhysicsBody()->getMass() * deltaVel;
_player->getPhysicsBody()->applyImpulse(impulse);
问题是,玩家可以如图所示越过边缘

当玩家与竞技场边缘接触并施加脉冲时,就会发生这种情况

我将玩家的身体设置为:

auto playerBody = PhysicsBody::createBox(_player->getContentSize(), PhysicsMaterial(100.0f, 0.0f, 0.0f));
playerBody->setDynamic(true);
playerBody->setRotationEnable(false);
playerBody->setGravityEnable(false);
_player->setPhysicsBody(playerBody);
其中,
PhysicsMaterial
的三个参数是密度、恢复和摩擦。同样,地图的主体是:

auto mapBody = PhysicsBody::createEdgeChain(polygonPoints.data(), polygonPoints.size(), PhysicsMaterial(100.0f, 0.0f, 0.0f));
mapBody->setDynamic(false);
mapBody->setGravityEnable(false);
_tileMap->setPhysicsBody(mapBody);
其中,
polygonPoints
是定义形状的
Vec2
的向量。播放器是一个
Sprite
,地图是一个
TMXTiledMap

我试图改变密度、摩擦和恢复值,但没有成功

你有没有经历过同样的问题


谢谢

您似乎正在使用cocos2d-x物理实现。所以,我对此不太了解。 但通常情况下,当更新物理世界更新速率周期较低时(通过设置或低帧速率),会发生这种情况。 检查,更新你的世界

从文件:

/** Set the speed of physics world, speed is the rate at which the simulation executes. default value is 1.0 */
inline void setSpeed(float speed) { if(speed >= 0.0f) { _speed = speed; } }
/** get the speed of physics world */
inline float getSpeed() { return _speed; }
/** 
 * set the update rate of physics world, update rate is the value of EngineUpdateTimes/PhysicsWorldUpdateTimes.
 * set it higher can improve performance, set it lower can improve accuracy of physics world simulation.
 * default value is 1.0
 */
inline void setUpdateRate(int rate) { if(rate > 0) { _updateRate = rate; } }
/** get the update rate */
inline int getUpdateRate() { return _updateRate; }

谢谢,这个问题实际上与物理世界的更新速度与玩家的速度相比太慢有关。但是,UpdateRate不能设置为小于1(它是一个int>0),因此我必须增加两次连续更新玩家速度之间的时间间隔(从1/60秒增加到0.1秒)。这会导致玩家的移动出现明显的延迟,从而解决问题。抢手货