cocos2dでsetDebugDrawMaskが一部表示されない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
tayuxa

cocos2dでsetDebugDrawMaskが一部表示されない

#1

投稿記事 by tayuxa » 8年前

お世話になっていますyuxaと申します.今回はcocos2d-xについてなのですが
http://www.drjiro.com/game-engine/cocos ... tutorial5/
こちらを参考にしてコードを作成したのですがランナー側のsetDebugDrawMaskが表示されません
そして,ランナーはなぜか床に半分めり込みます(当たり判定が真ん中?).
一部ソースを変えてはいますが影響はないはずなのですが原因を教えていただけませんでしょうか?

コード:

//PlayScene.cpp
#include "PlayScene.h"
#include "GameplayLayer.h"
#include "BackgroundLayer.h"
#include "StatusLayer.h"

USING_NS_CC;

Scene* PlayScene::createScene()
{
	auto scene = Scene::createWithPhysics();
	auto world = scene -> getPhysicsWorld();
	world -> setGravity(Vec2(0,-900));
	world -> setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	auto layer = PlayScene::create();
	scene -> addChild(layer);
	return scene;
}


bool PlayScene::init()
{
	if(!Layer::init())
	{
		return false;
	}
	
	initPhysics();
	
	auto backgroundLayer = BackgroundLayer::create();
	addChild(backgroundLayer);
	auto gameplayerLayer = GameplayLayer::create();
	addChild(gameplayerLayer);
	auto statusLayer = StatusLayer::create();
	addChild(statusLayer);
	
	return true;
}

bool PlayScene::initPhysics()
{
	auto wallBottom = Node::create();
	addChild(wallBottom);
	wallBottom -> setAnchorPoint(Vec2(0,1));
	wallBottom -> setPosition(Vec2(0,500));
	wallBottom -> setContentSize(Size(3000,100));
	std::string str = StringUtils::format("%lf--%lf",wallBottom->getContentSize().width,wallBottom->getContentSize().height);
	
	auto material = PHYSICSBODY_MATERIAL_DEFAULT;
	material.density = 1.0f;
	material.restitution = 0.0f;
	material.friction = 0.0f;
	
	auto body = PhysicsBody::createBox(wallBottom->getContentSize(), material);
	body->setDynamic(false);
	body->setContactTestBitmask(true);
	wallBottom->setPhysicsBody(body);
 
	return true;
}

コード:

 
//PlayScene.h
#ifndef __MyGame03__PlayScene__
#define __MyGame03__PlayScene__

#include <stdio.h>
#include "cocos2d.h"

class PlayScene : public cocos2d::Layer
{
private:
public:
	static cocos2d::Scene* createScene();
	virtual bool initPhysics();
	virtual bool init();
	CREATE_FUNC(PlayScene);
	
};
#endif /* defined(__MyGame03__PlayScene__) */

コード:

 
//GameplayLayer.cpp
#include "GameplayLayer.h"
USING_NS_CC;

bool GameplayLayer::init()
{
	if(!Layer::init())
	{
		return false;
	}
	
	auto visibleSize = Director::getInstance() -> getVisibleSize();
	Vec2 origin = Director::getInstance() -> getVisibleOrigin();
	
	//auto w_ratio = visibleSize.width/640;
	auto h_ratio = visibleSize.height/960;
	
	auto cacher = SpriteFrameCache::getInstance();
	
	cacher -> addSpriteFramesWithFile("sprites.plist");
	
	Vector<SpriteFrame*> frames(3);
	char str[100] = {0};
	for (int i = 0; i < 2 ; i++){
		sprintf(str,"player%d.png",i+1);
		auto a_frames = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
		frames.pushBack(a_frames);
	}
	
	auto animation = Animation::createWithSpriteFrames(frames,0.1f);
	auto animate = Animate::create(animation);
	auto repeat = RepeatForever::create(animate);
	
	auto spritePlayer = Sprite::create();
	auto player_Size = spritePlayer -> getContentSize();
	spritePlayer -> runAction(repeat);
	//spritePlayer -> setAnchorPoint(Vec2(0.5,0));
	spritePlayer -> setPosition(origin.x,origin.y+480*h_ratio);
	
	auto material = PHYSICSBODY_MATERIAL_DEFAULT;
	material.density = 1.0f;
	material.restitution = 0.0f;
	material.friction = 0.0f;
	
	auto body = PhysicsBody::createBox(player_Size,material);
	
	body -> setMass(1.0f);
	body -> setRotationEnable(false);
	body -> setGravityEnable(true);
	body -> setContactTestBitmask(true);
	body -> setDynamic(true);
	body -> applyImpulse(Vect(50.0f,50.0f),Point(0,0));
	spritePlayer -> setPhysicsBody(body);
	
	addChild(spritePlayer);
	
	return true;
}

コード:

 
//GameplayLayer.h
#ifndef __MyGame03__GameplayLayer__
#define __MyGame03__GameplayLayer__

#include <stdio.h>
#include "cocos2d.h"

class GameplayLayer : public cocos2d::Layer
{
private:
public:
	static cocos2d::Scene* createScene();
	virtual bool init();
	CREATE_FUNC(GameplayLayer);
	
};

#endif /* defined(__MyGame03__gameplayLayer__) */
 

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 13年前
住所: 東海地方
連絡を取る:

Re: cocos2dでsetDebugDrawMaskが一部表示されない

#2

投稿記事 by softya(ソフト屋) » 8年前

私は物理演算をつかったことが無いのでsetDebugDrawMaskが動かない原因は分かりませんが、キャラがめり込んでいるのはsetAnchorPoint()が原因と思われます。
setAnchorPoint()を調べて理解してみてください。
リンク先の記事で setAnchorPoint( Vec2::ZERO )にしているのは大いに意味があると思います。

【訂正】 setAnchorPointで変えてないことに意味があるでしたね。失礼しました。

【さらに訂正】 コメントで消してありましたね。失礼しました。
あと思いつくのは、
wallBottom -> setAnchorPoint(Vec2(0,1));
あたりかなと思うのですが試していないので正解ではないかもしれません。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

閉鎖

“C言語何でも質問掲示板” へ戻る