1

저는 cocos2d-x v3 (here 참조)을 사용하여 간단한 게임을 개발하려고합니다.Cocos2d-x v3의 교차점 충돌

목표는 무지개 모양의 플레이어를 녹색 아래쪽 영역에서 위쪽으로 이동하는 것입니다. 조이스틱은 왼쪽에있는 흰색 엄지 손가락입니다. 손가락으로 움직여 플레이어의 방향을 바꿀 수 있습니다.

지금까지 iCos2d-x v3에 내장 된 충돌 감지기 (클래스 PhysicalBody 통해)를 사용하고 있습니다. 그림의 빨간색 테두리는 플레이어의 모양을 나타내며 자유롭게 이동할 수있는 경기장의 테두리입니다.

사용자가 조이스틱의 엄지 손가락을 움직이면 방향이 플레이어의 속도를 설정하는 데 사용됩니다. 그러나 육체적 인 세계에 빠지면 속도를 설정하는 것이 물리적 인 법칙을 깨뜨리기 때문에 최선의 선택이 아닙니다. 즉 신체에만 힘을 가할 수 있습니다. 따라서, 원하는 속도에서, I은 임펄스를 달성하기 위해 적용되는 계산 :

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); 

문제는 플레이어가 표시 here로 가장자리를 통과 할 수 있다는 것이다.

이것은 플레이어가 경기장 가장자리에 닿았을 때 충격이 가해질 때 발생합니다.

내가 같이 플레이어의 몸을 설정 PhysicsMaterial의 세 가지 매개 변수는 밀도, 손해 배상 및 마찰이다

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); 

. 같은 방식으로,지도의 몸은 다음과 같습니다 polygonPoints이 모양을 정의 Vec2 's의 벡터가

auto mapBody = PhysicsBody::createEdgeChain(polygonPoints.data(), polygonPoints.size(), PhysicsMaterial(100.0f, 0.0f, 0.0f)); 
mapBody->setDynamic(false); 
mapBody->setGravityEnable(false); 
_tileMap->setPhysicsBody(mapBody); 

. 플레이어는 Sprite이고지도는 TMXTiledMap입니다.

밀도, 마찰 및 복원의 값을 변경하지 않고 변경하려고했습니다.

같은 문제가 발생 했습니까?

감사합니다.

답변

1

Physics 용 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; } 
+0

덕분에, 문제는 실제로 물리학의 세계가 플레이어의 속도에 비해 너무 느리게 업데이트되는 관련되었다. 그러나 UpdateRate는 1보다 작게 설정할 수 없습니다 (int> 0). 따라서 플레이어의 속도를 두 번 연속 업데이트하는 시간 간격을 늘려야했습니다 (1/60에서 0.1까지). 이로 인해 플레이어의 움직임이 눈에 띄지 않게 지연되고 문제가 해결됩니다. 잘 잡으세요! –