Обновление физики в cocos2d-x v3

Я разрабатываю игру, использующую cocos2d-x 3.1.1 для Linux, которая будет иметь поддержку физики.
Я все еще нахожусь в основах, но у меня уже есть много головных болей.
Размещенный код — это, по сути, то, что я сделал до сих пор: шар под действием силы тяжести и случайного вектора скорости.

Проблема в том, что симуляция, кажется, никогда не запускается, даже если спрайт отображается правильно. Мяч не двигается, на самом деле положение тела всегда одинаково (я его регистрирую). Я попытался отладить приложение и переместить блоки кода в другие функции, но безрезультатно.

Menu.cpp

#include "Menu.h"#include "cocos2d.h"#include <time.h>
#include <math.h>

#define COCOS2D_DEBUG 1
USING_NS_CC;ScenaMenu *ScenaMenu::createWithPhysics() {

ScenaMenu *ret = new ScenaMenu();
if (ret && ret->initWithPhysics()) {
ret->autorelease();
return ret;
}
else {
CC_SAFE_DELETE(ret);
return nullptr;
}}ScenaMenu::ScenaMenu() {

frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();

}ScenaMenu::~ScenaMenu() {}void ScenaMenu::onEnter() {

Scene::onEnter();

fisica = this->getPhysicsWorld();
fisica->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
fisica->setGravity(Vec2(0, -10.0f));

Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
LayerColor *layerMenu = LayerColor::create(Color4B(255, 255, 255, 255));
layerMenu->setContentSize(frameSize);

textureSfera = Director::getInstance()->getTextureCache()->addImage("sfera2.png");
textureSfera->setTexParameters(&texParams);
textureSfera->generateMipmap();

float angolo;
srand(time(NULL));
PhysicsMaterial mat = PhysicsMaterial(1, 1, 0);
PhysicsBody *bodyFisica;

bodyFisica = PhysicsBody::createCircle(layerMenu->getContentSize().width * 0.1, mat, Vec2::ZERO);
angolo = (rand() % 628) / 100.0f;
bodyFisica->setVelocity(Vec2(10 * cos(angolo), 10 * sin(angolo)));

spriteSfera = Sprite::createWithTexture(textureSfera);
spriteSfera->setScale((layerMenu->getContentSize().width * 0.33) / spriteSfera->getContentSize().width);
spriteSfera->setPosition(layerMenu->getContentSize().width * 0.5, layerMenu->getContentSize().height * 0.5);
spriteSfera->setPhysicsBody(bodyFisica);this->addChild(layerMenu);

layerMenu->addChild(spriteSfera);

}void ScenaMenu::update(float dt) {

log("POS %f", spriteSfera->getPhysicsBody()->getPosition().y);

}

AppDelegate.cpp

#include "AppDelegate.h"#include "Menu.h"
USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {

auto director = Director::getInstance();
auto glview = director->getOpenGLView();

if(!glview) {
glview = GLView::create("Test");
director->setOpenGLView(glview);
}

Size frameSize = Size(540, 960);
cocos2d::Size designResolutionSize = Size(1080, 1920);

glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_WIDTH);
glview->setFrameSize(frameSize.width, frameSize.height);

director->setDisplayStats(true);

director->setAnimationInterval(1.0 / 60);

ScenaMenu *scenaMenu = ScenaMenu::createWithPhysics();
director->runWithScene(scenaMenu);

return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

1

Решение

Задача ещё не решена.

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector