cocos2d-x 3.0 使用最新物理引擎的一个源码实例

本文介绍如何使用Cocos2d-x 3.1版本的最新物理引擎实现小猫与小车的碰撞效果。主要内容包括场景初始化、精灵创建及碰撞体设置等关键步骤,并提供了一个完整的工作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.碰撞函数参数由两个变成一个了

2.检测不到碰撞。需要设置那三个参数,同时还要设置成动态的。body进行设置。

3.初始入口文件也发生了改变。

附录上我最近调试好的cocos2d-x 3.1 使用最新物理引擎的一个源码实例,是一个小猫碰撞小车的例子。最新的引擎都可用。

资源文件和代码下载:http://download.youkuaiyun.com/detail/u014734779/7417009

部分代码参考:

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
	auto scene = Scene::createWithPhysics();

	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	Vect gravity = Vect(0.0f, 0.0f);
	scene->getPhysicsWorld()->setGravity(gravity);

    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

	layer->setPhyWorld(scene->getPhysicsWorld());

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
	
    

	_spriteSheet = SpriteBatchNode::create("sprites.png", 2);
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites.plist", "sprites.png");
	this->addChild(_spriteSheet);
	this->spawnCar();
	this->schedule(schedule_selector(HelloWorld::secondUpadte), 1.0f);
	this->scheduleUpdate();

    return true;
}

void HelloWorld::spawnCar()
{
	SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName("car.png");
	Sprite* car = Sprite::createWithSpriteFrame(frame);
	car->setPosition(Point(100, 100));
	addBoxBodyForSprite(car);
	car->setTag(2);
	car->runAction(MoveTo::create(1.0f, Point(300, 300)));
	car->runAction(RepeatForever::create(Sequence::create(MoveTo::create(1.0,Point(300, 100)),MoveTo::create(1.0,Point(200, 200)),MoveTo::create(1.0,Point(100, 100)),NULL)));
	_spriteSheet->addChild(car);
}

void HelloWorld::spawnCat()
{
	auto winSize = Director::getInstance()->getWinSize();

	auto cat = Sprite::createWithSpriteFrameName("cat.png");

	int minY = cat->getContentSize().height / 2;
	int maxY = winSize.height - (cat->getContentSize().height / 2);
	int rangeY = maxY - minY;
	int actualY = CCRANDOM_0_1() * rangeY;

	int startX = winSize.width + (cat->getContentSize().width / 2);
	int endX = -(cat->getContentSize().width / 2);

	Point startPos = Point(startX, actualY);
	Point endPos = Point(endX, actualY);

	cat->setPosition(startPos);
	addBoxBodyForSprite(cat);
	cat->setTag(1);
	cat->runAction(Sequence::create(MoveTo::create(1.0, endPos), CallFuncN::create(this, callfuncN_selector(HelloWorld::spriteDone)),NULL));

	_spriteSheet->addChild(cat);
	
}

void HelloWorld::addBoxBodyForSprite(Sprite* sprite)
{
	auto body = PhysicsBody::createBox(sprite->getContentSize());
	body->setDynamic(true);
	body->setCategoryBitmask(1);    // 0001
    body->setCollisionBitmask(-1);   // 0001
    body->setContactTestBitmask(-1); // 0001  必须要设置,而且设置为setDynamic(true);而且必须是0重力可以解决
	
	
	sprite->setPhysicsBody(body);
}

void HelloWorld::secondUpadte(float dt)
{
	this->spawnCat();
}

void HelloWorld::spriteDone(Node* sender)
{
	Sprite *sprite = (Sprite*)sender;
	_spriteSheet->removeChild(sprite,true);
}

void HelloWorld::onEnter()
{
	Layer::onEnter();

	auto contactListener = EventListenerPhysicsContact::create();
	contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);

	auto dispatcher = Director::getInstance()->getEventDispatcher();

	dispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
}

bool HelloWorld::onContactBegin( PhysicsContact& contact)
{
	auto spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode();
	auto spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode();

	if (spriteA->getTag() == 1)
	{
		spriteA->removeFromParentAndCleanup(true);
	}

	if (spriteB->getTag() == 1)
	{
		spriteB->removeFromParentAndCleanup(true);
	}

	return true;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值