hydex86
September 4, 2015, 4:20pm
1
I’m new with Physics in game development, and I’m trying to order concepts in my mind. I wanted to create a scene like this:
http://cocos2d-x.org/programmersguide/12-img/CorrelationSprite.gif
I am following this guide: http://cocos2d-x.org/programmersguide/12/
But I don’t know how can I do to get my sprites bounce on touching the edges of the scene. Do I need to define 4 physics bodies around the scene? Do I need to control the collision and change the direction, or Chipmunk does it for me?
Thanks for your answers!
Well, today is your lucky day!
We have samples that go along with the Programmers Guide!
Here is that sample:
For how the bodies are bouncing off the edge of the Scene:
auto menu = Menu::create(backItem, restartItem, nextItem, nullptr);
menu->setPosition(Vec2::ZERO);
addChild(menu, 9999);
auto restartItemSize = restartItem->getContentSize();
backItem->setPosition(s_centre.x - restartItemSize.width * 2, s_origin.y + restartItemSize.height / 2);
restartItem->setPosition(s_centre.x, s_origin.y + restartItemSize.height / 2);
nextItem->setPosition(s_centre.x + restartItemSize.width * 2, s_origin.y + restartItemSize.height/2);
//create border
auto node = Node::create();
auto physicsBody = PhysicsBody::createEdgeBox(s_visibleSize,PhysicsMaterial(0.1f, 1, 0.0f));
node->setPhysicsBody(physicsBody);
node->setPosition(s_centre);
this->addChild(node);
//add touch event listener
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
Enjoy!
hydex86
September 4, 2015, 4:33pm
3
Thank you very much!
This is what I needed
auto node = Node::create();
auto physicsBody = PhysicsBody::createEdgeBox(visibleSize,PhysicsMaterial(0.1f, 1, 0.0f));
node->setPhysicsBody(physicsBody);
node->setPosition(s_centre);
this->addChild(node);