【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十七课:Cocos2D-X物理引擎之chipmunk

【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十七课:Cocos2D-X物理引擎之chipmunk,第1张

概述【麦可网】Cocos2d-X跨平台游戏开发---学习笔记 第二十六课:Cocos2D-X物理引擎之Box2D11-12 ============================================================================================================================================

【麦可网】Cocos2d-X跨平台游戏开发---学习笔记

第二十六课:Cocos2D-X物理引擎之Box2D11-12

=======================================================================================================================================================================

课程目标:

-学习chipmunk

课程重点:

-chipmuk概念

-chipmukBox2D

-chipmunk常用 *** 作

考核目标:

-能够使用chipmuk常用 *** 作,完成游戏需求

=======================================================================================================================================================================

一、chipmukBox2D对比

语音方面:

chipmunkC语言写的

Box2DC++写的

功能上:

Box2D更丰富

删除对象的方式:

chipmunk:调用函数cpspaceAddpoststepCallback

Box2D:设置标记


二、实例
HelloChipmunk.h--------------------------------------------#pragma once#include "cocos2d.h"#include "cocos-ext.h"#include "chipmunk.h"using namespace cocos2d::extension;using namespace cocos2d;class HelloChipmunk : public cocos2d::cclayer{public:	HelloChipmunk();	~HelloChipmunk();	// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone	virtual bool init();  	// there's no 'ID' in cpp,so we recommend returning the class instance pointer	static cocos2d::CCScene* scene();	// a selector callback	voID menuCallback(CCObject* pSender);	// implement the "static node()" method manually	CREATE_FUNC(HelloChipmunk);	voID onEnter();	voID initPhysics();	voID addNewSpriteAtposition(CCPoint p);	voID update(float dt);	virtual voID cctouchesEnded(CCSet *ptouches,CCEvent *pEvent);private:	CCTexture2D* m_pSpriteTexture;		//精灵贴图	CCPhysicsDeBUGNode* m_pDeBUGLayer;	//物理引擎DEBUG	cpspace* m_pSpace;					//物理空间	cpshape* m_pWalls[4];				//形状数组(四边)};HelloChipmunk.cpp--------------------------------#include "HelloChipmunk.h"USING_NS_CC;enum{	kTagparentNode = 1,};enum{	Z_PHYSICS_DEBUG = 100,};CCScene* HelloChipmunk::scene(){	// 'scene' is an autorelease object	CCScene *scene = CCScene::create();	// 'layer' is an autorelease object	HelloChipmunk *layer = HelloChipmunk::create();	// add layer as a child to scene	scene->addChild(layer);	// return the scene	return scene;}// on "init" you need to initialize your instanceHelloChipmunk::HelloChipmunk(){}HelloChipmunk::~HelloChipmunk(){	for (int i=0; i<4; i++)	{		cpshapeFree(m_pWalls[i]);	}	cpspaceFree( m_pSpace );}bool HelloChipmunk::init(){	//////////////////////////////	// 1. super init first	if ( !cclayer::init() )	{		return false;	}	//使能触摸	settouchEnabled(true);	setAccelerometerEnabled(true);	//初始化物理引擎	initPhysics();	CCSpriteBatchNode* parent = CCSpriteBatchNode::create("blocks.png");	m_pSpriteTexture = parent->getTexture();	addChild(parent,kTagparentNode);//添加新精灵	addNewSpriteAtposition(ccp(200,200));	//更新scheduleUpdate();	return true;}voID HelloChipmunk::menuCallback(CCObject* pSender){}static voID poststepRemove(cpspace* space,cpshape *shape,voID* unused){	int* spriteType = (int*)shape->data;	if (*spriteType == 1)	{		//delete	}}static int begin(cpArbiter* arb,cpspace* space,voID* unused){	cpshape* a;	cpshape* b;	cpArbiterGetShapes(arb,&a,&b);	cpspaceAddpoststepCallback(space,(cppoststepFunc)poststepRemove,a,NulL);	return 1;	//return 0,不发生碰撞}voID HelloChipmunk::initPhysics(){	cpInitChipmunk();	m_pSpace = cpspaceNew();	m_pSpace->gravity = cpv(0,-100);	m_pWalls[0] = cpsegmentShapeNew( m_pSpace->staticBody,//静态对象		cpv(0,0),cpv(480,0.0f);	m_pWalls[1] = cpsegmentShapeNew( m_pSpace->staticBody,320),0.0f);	m_pWalls[2] = cpsegmentShapeNew( m_pSpace->staticBody,cpv(0,0.0f);	m_pWalls[3] = cpsegmentShapeNew( m_pSpace->staticBody,//静态对象		cpv(480,0.0f);	for (int i=0; i<4; i++)	{		m_pWalls[i]->e = 1.0f;		//摩擦系数等属性		m_pWalls[i]->u = 1.0f;		cpspaceAddStaticShape(m_pSpace,m_pWalls[i]);	}	//创建deBUGlayer	m_pDeBUGLayer = CCPhysicsDeBUGNode::create(m_pSpace);	this->addChild(m_pDeBUGLayer,Z_PHYSICS_DEBUG);	//碰撞检测参数2和3指定碰撞类型	cpspaceAddCollisionHandler(m_pSpace,1,begin,NulL,NulL);}voID HelloChipmunk::update(float delta){	//不使用传进来的参数,因为传进来的参数会根据运行时间而变动	//采用固定每帧计算2次	int steps = 2;	float dt = CCDirector::sharedDirector()->getAnimationInterval()/(float)steps;		for (int i=0; i<steps; i++)	{		cpspaceStep(m_pSpace,dt);	}	}voID HelloChipmunk::addNewSpriteAtposition(CCPoint p){	int posx,posy;		CCNode* parent = getChildByTag(kTagparentNode);	posx = CCRANDOM_0_1() * 64.0f;	posy = CCRANDOM_0_1() * 64.0f;	posx = (posx % 2) * 32;	posy = (posy % 2) * 32;	int num = 4;	cpVect verts[] = {		cpv(-16,-16),cpv(-16,16),cpv( 16,};	//创建刚体	cpBody *body = cpBodyNew(1.0f,cpMomentForpoly(1.0f,num,verts,cpvzero));	body->p = cpv(p.x,p.y);	cpspaceAddBody(m_pSpace,body);	//创建形状	cpshape* shape = cppolyShapeNew(body,cpvzero);	shape->e =0.5f; shape->u = 0.5f;	int* tmp = new int;	*tmp = 1;	shape->data = tmp;	shape->collision_type = 1;	cpspaceAddShape(m_pSpace,shape);	CCPhysicssprite* sprite = CCPhysicssprite::createWithTexture(m_pSpriteTexture,CCRectMake(posx,posy,32,32));	parent->addChild(sprite);	sprite->setCPBody(body);	sprite->setposition(p);}voID HelloChipmunk::onEnter(){	cclayer::onEnter();}voID HelloChipmunk::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent){	CCSetIterator it;	CCtouch* touch;	for (it=ptouches->begin(); it!=ptouches->end	(); it++)	{		touch = (CCtouch*)(*it);		if (!touch)		{			break;		}				CCPoint location = touch->getLocation();		addNewSpriteAtposition( location );	}	}


===================================================================

总结:

总算把初级教程学习完了,接下来做个简单的游戏。

开心一刻:

毕业后七年,总算接了个大工程,造一根三十米烟囱,工期两个月,造价三十万,不过要垫资。总算在去年年底搞完了。今天人家去验收,被人骂得要死,还没有钱拿!图纸看反了,人家是要挖一口井!

【麦可网】Cocos2d-X跨平台游戏开发---教程下载:http://pan.baIDu.com/s/1kTio1Av

【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu

总结

以上是内存溢出为你收集整理的【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十七课:Cocos2D-X物理引擎之chipmunk全部内容,希望文章能够帮你解决【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十七课:Cocos2D-X物理引擎之chipmunk所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存