Physical Engine: The physical code used to simulate a set of physical events.
#ifndef __helloworld_scene_h__
#define __helloworld_scene_h__
#include "Cocos2d.h"
#include <Box2D/Box2D.h>
#define RATIO 80.0f
Class Helloworld:public Cocos2d::layer,public B2contactlistener
{
Private
B2world *world;
B2body *groundbody;
Public
Static cocos2d::scene* Createscene ();
virtual BOOL init ();
void Menuclosecallback (cocos2d::ref* psender);
virtual void update (float DT);
virtual void Begincontact (b2contact* contact);
void Addrect (float x,float y,b2bodytype type);
void Addground ();
Create_func (HelloWorld);
};
#endif//__helloworld_scene_h__
#include "HelloWorldScene.h"
USING_NS_CC;
scene* Helloworld::createscene ()
{
Auto scene = Scene::create ();
Auto layer = Helloworld::create ();
Scene->addchild (layer);
return scene;
}
BOOL Helloworld::init ()
{
if (! Layer::init ())
{
return false;
}
Size visiblesize = director::getinstance ()->getvisiblesize ();
VEC2 origin = Director::getinstance ()->getvisibleorigin ();
Create a world
World=new B2world (B2VEC2 (0,-10));
World->setcontactlistener (this);
Addground ();
Addrect (5,5,b2_dynamicbody);
Addrect (1,5,b2_kinematicbody);//floating objects, not affected by gravity
Scheduleupdate ();
return true;
}
void Helloworld::update (float dt) {
World->step (dt,8,3);
Sprite *s;
For (B2body *b=world->getbodylist (); B;b=b->getnext ()) {
if (B->gettype () ==b2_dynamicbody) {
Log ("%f", B->getposition (). y);
if (B->getuserdata ()) {
S= (sprite*) b->getuserdata ();
S->setposition (B->getposition (). X*ratio,b->getposition (). Y*ratio);
}
//}
}
}
void Helloworld::begincontact (B2contact *contact) {
if (Contact->getfixturea ()->getbody () ==groundbody| | Contact->getfixtureb ()->getbody () ==groundbody) {
Log ("An object landed on the floor");
}
}
void Helloworld::addrect (float positionx,float positiony,b2bodytype type) {
Config box2d
B2bodydef def;
DEF.POSITION=B2VEC2 (Positionx,positiony);
DEF.LINEARVELOCITY=B2VEC2 (1,0);
DEF.LINEARVELOCITY=B2VEC2 (0,10);
Def.type=type;
Groundbody=world->createbody (&DEF);
B2polygonshape shape;
Shape. Setasbox (0.5,0.5);
B2fixturedef Fixturedef;
Fixturedef.density=1;
fixturedef.friction=0.3;
fixturedef.shape=&shape;
Groundbody->createfixture (&FIXTUREDEF);
Config Cocos shape
Auto S=sprite::create ();
S->settexturerect (Rect (0,0,0.5*2*ratio,0.5*2*ratio));
AddChild (s);
S->setposition (Point (Def.position.x*ratio,def.position.y*ratio));
Groundbody->setuserdata (s);
}
void Helloworld::addground () {
B2bodydef def;
DEF.POSITION=B2VEC2 (400/ratio,0);
Def.type=b2_staticbody;
B2body *body=world->createbody (&DEF);
B2polygonshape Groundshape;
Groundshape.setasbox (400/ratio,0.5);
B2fixturedef Fixuredef;
Fixuredef.density=1;
fixuredef.friction=0.3;
fixuredef.shape=&groundshape;
Body->createfixture (&FIXUREDEF);
}
void Helloworld::menuclosecallback (ref* psender)
{
Director::getinstance ()->end ();
#if (Cc_target_platform = = Cc_platform_ios)
Exit (0);
#endif
}
Cocos2d-x's box2d Preliminary examination