coco2dx 3.0以后最box2d和chipmunk这两个物理引擎进行了封装,使用起来非常的便利。

来源:互联网 发布:ios淘宝旧版本下载 编辑:程序博客网 时间:2024/06/05 02:15


3.0以后最box2d和chipmunk这两个物理引擎进行了封装,使用起来非常的便利。

官方链接地址:英文版

泰然网:中文教程

offset:重心点
velocity:速度

dadamping:阻尼

rerestitution:弹力

mamaterial:材质

mass:质量
moment:力矩,当他碰到另一个刚体时候 ,会产生一股扭转力,做旋转运动
body:刚体,表示物理世界中的抽象实体,附带有物理属性
shape:刚体的形状,同一个body可以附加多个shape 该shape们不会发生碰撞
joint:关节,可以连接>=2个刚体

1.physicsBody

view sourceprint?
001./** 创建一个body  mass和moment为默认值  */
002.   staticPhysicsBody* create();
003.   /** 创建一个质量为mass的body moment为默认值. */
004.   staticPhysicsBody* create(floatmass);
005.   /** 创建一个body 并为mass 和moment赋值 */
006.   staticPhysicsBody* create(floatmass, float moment);
007.   /**创建一个shape为圆形的body */
008.   staticPhysicsBody* createCircle(floatradius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, constPoint& offset = Point::ZERO);
009.   /** 创建一个shape为四边形的body. */
010.   staticPhysicsBody* createBox(constSize& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, constPoint& offset = Point::ZERO);
011.   /**创建一个动态多边形刚体,多边形的顶点存放在Point array[ ]中  示例:Point array[ ]={ point(1,1),point(2,2)}  注意:顶点必须按顺时针存放,并且图形为凸状,不能是凹的*/
012.   staticPhysicsBody* createPolygon(constPoint* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,const Point& offset = Point::ZERO);
013.    
014.   /** 创建一个静态的线状刚体. */
015.   staticPhysicsBody* createEdgeSegment(constPoint& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,float border = 1);
016.   /** 创建一个静态四边形刚体. */
017.   staticPhysicsBody* createEdgeBox(constSize& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, floatborder = 1,const Point& offset = Point::ZERO);
018.   /** 创建一个静态多边形刚体. */
019.   staticPhysicsBody* createEdgePolygon(constPoint* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,float border = 1);
020.   /** 创建一个链条状刚体 */
021.   staticPhysicsBody* createEdgeChain(constPoint* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,float border = 1);
022.    
023.   /*
024.  添加一个shape  mass和moment赋值true
025.    */
026.   virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment =true);
027.   /*通过shape移除shape*/
028.   voidremoveShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
029.   /*通过tag移除shape*/
030.   voidremoveShape(int tag, bool reduceMassAndMoment = true);
031.   /* 移除body的所有shape */
032.   voidremoveAllShapes(bool reduceMassAndMoment = true);
033.   /* 获取body的shapes */
034.   inlineconst Vector<PhysicsShape*>& getShapes()const { return_shapes; }
035.   /* 获取第一个shape. */
036.   inline PhysicsShape* getFirstShape()const { return_shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
037.   /* 通过tag从body中获取shape */
038.   PhysicsShape* getShape(inttag) const;
039.    
040.   /**给body施加一个循序渐进的力,物体会受加速度影响,越来越快,像火车一样*/
041.   virtualvoid applyForce(constVect& force);
042.   /** offset为偏移度 指碰到物体时 body旋转 偏移 一般设为默认值 值越大 旋转越快 偏移角度越大*/
043.   virtualvoid applyForce(constVect& force, const Point& offset);
044.   /** 重置施加在body上的力  清0了. */
045.   virtualvoid resetForces();
046.   /** 不会产生力,直接与body的速度叠加 产生新的速度. */
047.   virtualvoid applyImpulse(constVect& impulse);
048.   /** Applies a continuous force to body. */
049.   virtualvoid applyImpulse(constVect& impulse, const Point& offset);
050.   /**施加一个扭转力到刚体上  就像向前翻转一块大石头一样. */
051.   virtualvoid applyTorque(floattorque);
052.    
053.   /** 设置刚体的速度*/
054.   virtualvoid setVelocity(constVect& velocity);
055.   /** 获取刚体的速度 */
056.   virtual Point getVelocity();
057.   /** 设置刚体角速度 就是单位时间内转动的弧度*/
058.   virtualvoid setAngularVelocity(floatvelocity);
059.   /** 通过一个局部点获取刚体的角速度*/
060.   virtual Point getVelocityAtLocalPoint(constPoint& point);
061.   /** 通过世界点获取刚体的角速度*/
062.   virtual Point getVelocityAtWorldPoint(constPoint& point);
063.   /** 获取刚体的角速度 */
064.   virtualfloat getAngularVelocity();
065.   /** 设置速度的极限值*/
066.   virtualvoid setVelocityLimit(floatlimit);
067.   /**获取速度的极限值 */
068.   virtualfloat getVelocityLimit();
069.   /** 设置角速度极限值 */
070.   virtualfloat getAngularVelocityLimit();
071.    
072.   /** 从world中移除body */
073.   voidremoveFromWorld();
074.    
075.   /** 获取world */
076.   inline PhysicsWorld* getWorld()const { return_world; }
077.   /**获取body的所有关节 */
078.   inlineconst std::vector<PhysicsJoint*>& getJoints()const { return_joints; }
079.    
080.   /** 取得body设置的sprite. */
081.   inline Node* getNode()const { return_node; }
082.    
083.   /**
084.    * A mask that defines which categories this physics body belongs to.
085.    * Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
086.    * The default value is 0xFFFFFFFF (all bits set).
087.    */没搞懂
088.   voidsetCategoryBitmask(int bitmask);
089.   /**
090.    * A mask that defines which categories of bodies cause intersection notifications with this physics body.
091.    * When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
092.    * The default value is 0x00000000 (all bits cleared).
093.    */
094.   voidsetContactTestBitmask(intbitmask);
095.   /**
096.    * A mask that defines which categories of physics bodies can collide with this physics body.
097.    * When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
098.    * The default value is 0xFFFFFFFF (all bits set).
099.    */
100.   voidsetCollisionBitmask(int bitmask);
101.   /** get the category bit mask */
102.   inlineint getCategoryBitmask() const { return_categoryBitmask; }
103.   /** get the contact test bit mask */
104.   inlineint getContactTestBitmask()const { return_contactTestBitmask; }
105.   /** get the collision bit mask */
106.   inlineint getCollisionBitmask()const { return_collisionBitmask; }
107.    
108.   /**
109.    * set the group of body
110.    * Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)
111.    * it have high priority than bit masks
112.    */
113.   voidsetGroup(int group);
114.   /** get the group of body */
115.   inlineint getGroup() const{ return _group; }
116.    
117.   /** 获取body坐标 */
118.   Point getPosition()const;
119.   /** 获取body角度. */
120.   floatgetRotation() const;
121.    
122.   /**判断body是否静止*/
123.   inline bool isDynamic()const { return_dynamic; }
124.   /**设置body状态  false为静态 true为动态*/
125.   voidsetDynamic(bool dynamic);
126.    
127.   /**设置mass值 如果需要增加mass  有addmass方法  不要在这里做加减 */
128.   voidsetMass(float mass);
129.   /** 取得mass. */
130.   inlinefloat getMass() const{ return _mass; }
131.   /**
132.    * @brief add mass to body.
133.    * if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
134.    * if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
135.    * if mass == -PHYSICS_INFINITY, _mass will not change.
136.    * if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
137.    * other wise, mass = mass + _mass;
138.    */增加质量
139.   voidaddMass(float mass);
140.    
141.   /**
142.    * @brief set the body moment of inertia.
143.    * @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
144.    */设置力矩
145.   voidsetMoment(float moment);
146.   /** 获取惯性的力矩. */
147.   inlinefloat getMoment(floatmoment) const { return _moment; }
148.   /**
149.    * @brief add moment of inertia to body.
150.    * if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
151.    * if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
152.    * if moment == -PHYSICS_INFINITY, _moment will not change.
153.    * if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
154.    * other wise, moment = moment + _moment;
155.    */增加力矩
156.   voidaddMoment(float moment);
157.   /** 取得线性阻尼 */
158.   inlinefloat getLinearDamping() const { return_linearDamping; }
159.   /**
160.    * 设置阻尼值
161.    *它用来模拟body在气体或者液体中的摩擦力
162.    *取值范围是 0.0f to 1.0f.
163.    */
164.   inlinevoid setLinearDamping(floatdamping) { _linearDamping = damping; }
165.   /** 获取角阻尼 */
166.   inlinefloat getAngularDamping()const { return_angularDamping; }
167.   /**
168.    * 设置角阻尼
169.    * 它用来模拟body在气体或者液体中的角阻尼
170.    * the value is 0.0f to 1.0f.
171.    */
172.   inlinevoid setAngularDamping(floatdamping) { _angularDamping = damping; }
173.    
174.   /** 判断body是否是 休息状态 */
175.   bool isResting()const;
176.   /**
177.    *判断body能否在物理世界中模拟
178.    */
179.   inline bool isEnabled()const { return_enable; }
180.   /**
181. 设置body能否在物理世界中模拟
182.    */
183.   voidsetEnable(bool enable);
184.    
185.   /** whether the body can rotation */
186.   inline bool isRotationEnabled()const { return_rotationEnable; }
187.   /**设置能否旋转*/
188.   voidsetRotationEnable(bool enable);
189.    
190.   /** 判断body是否受引力影响 */
191.   inline bool isGravityEnabled()const { return_gravityEnable; }
192.   /** 设置body是否受引力影响 */
193.   voidsetGravityEnable(bool enable);
194.    
195.   /** 取得body 的tag值 */
196.   inlineint getTag() const{ return _tag; }
197.   /** 设置body tag值*/
198.   inlinevoid setTag(inttag) { _tag = tag; }
199.    
200.   /** 转换 世界点 到 局部点  类似 世界坐标和 局部坐标的转换*/
201.   Point world2Local(constPoint& point);
202.   /** 转换局部坐标到 世界坐标 */
203.   Point local2World(constPoint& point);

2.PhysicsShape

view sourceprint?
01./** 通过shape 取得body */
02.    inline PhysicsBody* getBody()const { return_body; }
03.    /** 返回shape的类型 */
04.    inline Type getType()const { return_type; }
05.    /** 返回shape的面积 */
06.    inlinefloat getArea() const{ return _area; }
07.    /** 取得moment 力矩 */
08.    inlinefloat getMoment() const { return_moment; }
09.    /** Set moment, it will change the body's moment this shape attaches */
10.    voidsetMoment(float moment);//设置力矩
11.    inlinevoid setTag(inttag) { _tag = tag; }//设置标签tag
12.    inlineint getTag() const{ return _tag; }//取得tag标签
13.     
14.    /**获取质量 */
15.    inlinefloat getMass() const{ return _mass; }
16.    /** Set mass, it will change the body's mass this shape attaches */                                        
17.    voidsetMass(float mass);//设置质量
18.    inlinefloat getDensity() const { return_material.density; }//density为密度
19.    voidsetDensity(float density);//获取密度
20.    inlinefloat getRestitution() const { return_material.restitution; }//获取弹性
21.    voidsetRestitution(float restitution);//设置弹性
22.    inlinefloat getFriction() const { return_material.friction; }//friction为摩擦力
23.    voidsetFriction(float friction);//设置摩擦力
24.    constPhysicsMaterial& getMaterial() const{ return _material; }//Material为材质
25.    voidsetMaterial(const PhysicsMaterial& material);设置材质
26.     
27.    /** 返回默认力矩  其值为0  */
28.    virtualfloat calculateDefaultMoment() {return 0.0f; }
29.    /** 取得重心 初始值为zero */
30.    virtual Point getOffset() {return Point::ZERO; }
31.    /** 获取shape的重心点  */
32.    virtual Point getCenter() {return getOffset(); }
33.    /**shape是否包含该点 */
34.    bool containsPoint(constPoint& point) const;
35.     
36.    /** 改变重心点 */
37.    staticvoid recenterPoints(Point* points,int count, constPoint& center = Point::ZERO);
38.    /** 取得多边形的重心点 */
39.    staticPoint getPolyonCenter(constPoint* points, int count);
40.     
41.    /**
42.     * A mask that defines which categories this physics body belongs to.
43.     * Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
44.     * The default value is 0xFFFFFFFF (all bits set).
45.     */
46.    inlinevoid setCategoryBitmask(intbitmask) { _categoryBitmask = bitmask; }
47.    inlineint getCategoryBitmask() const { return_categoryBitmask; }
48.    /**
49.     * A mask that defines which categories of bodies cause intersection notifications with this physics body.
50.     * When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
51.     * The default value is 0x00000000 (all bits cleared).
52.     */
53.    inlinevoid setContactTestBitmask(intbitmask) { _contactTestBitmask = bitmask; }
54.    inlineint getContactTestBitmask()const { return_contactTestBitmask; }
55.    /**
56.     * A mask that defines which categories of physics bodies can collide with this physics body.
57.     * When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
58.     * The default value is 0xFFFFFFFF (all bits set).
59.     */
60.    inlinevoid setCollisionBitmask(intbitmask) { _collisionBitmask = bitmask; }
61.    inlineint getCollisionBitmask()const { return_collisionBitmask; }
62.     
63.    voidsetGroup(int group);
64.    inlineint getGroup() { return_group; }
0 0
原创粉丝点击